//String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");

}

function openPopUp(pUrl, w, h) {
    var l = getWindowPosition("width") - (w / 2);
    var t = getWindowPosition("height") - (h / 2);
    window.open(pUrl, 'popUp', 'width=' + w + ',height=' + h + ',left=' + l + ',top=' + t + ',resizable=yes,scrollbars=yes');
}

function getWindowPosition(dim) {
    var myWidth = 0, myHeight = 0;
    // a little check cos IE doens't support window innerWidth

    if (typeof (window.innerWidth) == 'number') {
        //Non-IE

        theTop = document.documentElement.scrollTop;
        theHeight = window.innerHeight / 2;

        theLeft = document.documentElement.scrollLeft;
        theWidth = window.innerWidth / 2;

        myWidth = theLeft + theWidth;
        myHeight = theTop + theHeight;

    }
    else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        theTop = document.documentElement.scrollTop;
        theHeight = document.documentElement.clientHeight / 2;

        theLeft = document.documentElement.scrollLeft;
        theWidth = document.documentElement.clientWidth / 2;

        myWidth = theLeft + theWidth;
        myHeight = theHeight + theTop;

    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    if (dim == 'width') {
        return myWidth;
    }
    else {
        return myHeight;
    }

}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}



// clear form fields
function clearDefault(el) {
    var defaultValue = "";
    if (defaultValue==el.value)
	{
		el.value = "";
	}
}

function checkEnter(e,caller) //e is event object passed from function invocation
{

	var characterCode //literal character code will be stored in this variable

	if(e && e.which ||e.which == 0)
	{ //if which property of event object is supported (NN4)
	
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)//if generated character code is equal to ascii 13 (if enter key)
	{ 
	    if(document.all){
		    e.returnValue=false;
            e.cancel = true;
        }
        else{
         e.preventDefault();
        }
        var obj = document.getElementById(caller);
        if(obj){        
            
            if(obj.click){
		        obj.click();
		    }
		}
		return false;
	}
	else
	{
		return true;
	}
		
}

function sendMail(e) //e is event object passed from function invocation
{
//alert("biuuuu");
	var characterCode //literal character code will be stored in this variable

	if(e && e.which ||e.which == 0)
	{ //if which property of event object is supported (NN4)
	
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	}
	else
	{
		e = event;
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13)//if generated character code is equal to ascii 13 (if enter key)
	{ 
	    if(document.all){
		    e.returnValue=false;
            e.cancel = true;
        }
        else{
         e.preventDefault();
        }
        
	//	var obj = document.getElementById(caller);
	var obj = $("input.btSubmit");
        if(obj){        
            
            if(obj.click){
		        obj.click();
		    }
		}
		onSubmit();
		return false;
	}
	else
	{
		return true;
	}
		
}


function quickSearch(query,type){
    var query = encodeURIComponent(query);
	
	//alert(query);
	
    if(query != ''){
		if (type == ''){
			location.href = '/Sitetools/search.aspx?q=' + query;
		}
		else{
			/*
			var loc = location.href;
			var arrayloc = loc.split('/');
			
			
			var url = '';
			for(var i = 0; i < 4; i++){
				url = url + arrayloc[i] +'/';
			}

			if (arrayloc.length > 5)
				url = url + arrayloc[4];
			else
				url = url + arrayloc[4].substring(0, arrayloc[4].length - 5);

				alert(url);
				*/
			location.href =  '/germanSite/Sitetools/search.aspx?q=' + query;
				
		}
    }   
}

function doSearch(type){
   var q = '?q=';
   if(qBox != null){
        q += encodeURI(qBox.value);
    }
   var urlq = '/search.aspx';

   var el = document.getElementById("advancedSearch");
   var display = el.style.display;
  
   
        var qBox = document.getElementById('searchf1Txt');
        if(qBox != null){
               q += encodeURI(qBox.value);
            }
        
  if(display == "none"){
        //simple search
        urlq += q;
        
		var loc = location.href;
		var arrayloc = loc.split('/');
			
		var url = '';
		for(var i = 0; i < 4; i++){
			url = url + arrayloc[i] +'/';
		}
		if (type == ''){
			location.href = url + urlq.substring(1, urlq.length);
			}
		else{
			if (arrayloc.length > 5)
				url = url + arrayloc[4];
			else
				url = url + arrayloc[4].substring(0, arrayloc[4].length - 5);
			location.href = url + '/ToolsMenu' + urlq;
		}
		
		/*document.location.href = '/ToolsMenu' + url;*/
        
		
		
		return;
  }
 //alert(Date.parse(document.getElementById('inputF1TextF').value));
  // alert(Date.parse(document.getElementById('inputF1TextT').value));
  
//         url += q;
//         document.location = url+"&isAdvace=true";
          return;
        
        
}

function profileSearch(){

	var isValue=false;

	var txtName=document.getElementById('txtName');
	var name=""; 

	 if(txtName != null)
	 {
	   name=encodeURIComponent(txtName.value);
	   if (trim(name)!='') {isValue=true;}
	 }
	 
	var txtPosition=document.getElementById('txtPosition');
	var position=""; 
	 if(txtPosition != null)
	 {
	   position= encodeURIComponent(txtPosition.value);
	   if (trim(position)!='') {isValue=true;}
	 }
	 
	var txtTelefon=document.getElementById('txtTelefon');
	var phone=""; 
	 if(txtTelefon != null)
	 {
	   phone= encodeURIComponent(txtTelefon.value);
	   if (trim(phone)!='') {isValue=true;}
	 }
	 
	var txtUserName=document.getElementById('txtUserName');
	var userName=""; 
	 if(txtUserName != null)
	 {
	   userName= encodeURIComponent(txtUserName.value);
	   if (trim(userName)!='') {isValue=true;}
	 }
	 
	var txtDepartment=document.getElementById('txtDepartment');
	var department=""; 
	 if(txtDepartment != null)
	 {
	   department=encodeURIComponent(txtDepartment.value);
	   if (trim(department)!='') {isValue=true;}
	 }
	 
	var txtQualification=document.getElementById('txtQualification');
	var qualification=""; 
	 if(txtQualification != null)
	 {
	   qualification= encodeURIComponent(txtQualification.value);
	   if (trim(qualification)!='') {isValue=true;}
	 }
	
	if (isValue)
	{
		var query ='?name='+name+'&position='+position+'&telephone='+phone+'&user='+userName+'&department='+department+'&qualification='+qualification+'&page=1';
		//alert(location.href);
		location.href= '/Sitetools/ProfileSearch.aspx' + query;
	}
	else 
	{
		location.href= '/Sitetools/ProfileSearch.aspx';
	}
	}

function toggleAdvancedSearch() {
    var el = document.getElementById("advancedSearch");
    
    var simple = document.getElementById('h2Simple');
    var advance = document.getElementById('h2Advance');
      
    var display = el.style.display;
    
    if (i != "2"){
       el.style.display="none";
       simple.className="h2Active";  
       advance.className="boxTitle1"; 
       
     }
    else{
        el.style.display="block";
        advance.className="h2Active";
        simple.className="boxTitle1"; 
	}
}

function bodyOnLoad(){

	var hiddjobsN=document.getElementById('hiddenJobsNames');
	var jobsN="";
	 if(hiddjobsN != null)
	 {
	   jobsN= hiddjobsN.value;
	 }
	 
	 var hiddjobsP=document.getElementById('hiddenJobsPaths');
	 var jobsP="";
	 if(hiddjobsP != null)
	 {
	   jobsP= hiddjobsP.value;
	 }
	 var divheightContent =0;
	 if(document.getElementById('content')!=null)
	 {
		divheightContent= document.getElementById('content').offsetHeight;
	 }
	 
	 var divheightJobs=0;
	 if (document.getElementById('jobs')!=null)
	 {
		divheightJobs= document.getElementById('jobs').offsetHeight;
	 }

	var i=0,size=0,offserFrontContent=0;
	var firstPart ,lastpart,innerText="";
	 firstPart="<li><a href='";
	 midle="'>";
	 lastPart="</a></li>";
	var stringNames=jobsN.split("|");
	var stringPaths=jobsP.split("|");
	
   if (document.getElementById('rightContent')!=null)
   {
	size=stringNames.length-1;
	offserContent=divheightContent-10;
	for (i=0; i<size; i++)
	{
		if (document.getElementById('rightContent').offsetHeight <= offserContent)
		{
			if ((stringNames[i]!="")&&(stringPaths[i]!=""))
			{
				innerText+=firstPart+stringPaths[i] +midle+stringNames[i]+lastPart+ " ";
				document.getElementById('lastFC').innerHTML=innerText;
			}
		}else 
		
		break;
	}
   }
//document.getElementById('lastFC').innerHTML=innerText;
//   }catch(err)
//  {}
}

function sendData() 
{
		//alert("bu");
	var hiddResponsable=document.getElementById('responsible');
	var responsible="";
	 if(hiddResponsable != null)
	 {
	   responsible= hiddResponsable.value;
	 }
	 
	 var hiddBackToThePage=document.getElementById('back');
	 var back="";
	 if(hiddBackToThePage != null)
	 {
	   back= hiddBackToThePage.value;
	 }
	 alert(responsible);
	 alert(back);
	 Session("responsible")=responsible;
	 Session("linkBack")=back;
	 
}

function wopen(url, w, h)
{
// Fudge factors for window decoration space.
 // In my tests these work well on all platforms & browsers.
w += 50;
h += 96;
//alert(w +" "+h);
 var win = window.open(url,'Flash Install','width=' + w + ', height=' + h + ', ' +'location=no, menubar=no, ' +'status=no, toolbar=no, scrollbars=yes, resizable=no');
 win.resizeTo(w, h);
 win.focus();
}
/*
function getKey(evt) {
 if (evt && ns6)
 typedKey=evt.which;
 else
 typedKey=event.keyCode;
 if (typedKey == 13) {
 if(document.all) {
 window.event.cancelBubble='true';
 window.event.returnValue = false;
 } else {
 evt.stopPropagation();
 evt.preventDefault();
 }

 document.getElementById("searchbutton").click();
 }
}
 */
 
 function getUrlForPrint()
{
	var printLink=document.getElementById('printLink');
	
	 if (printLink != null)
	 {
	// alert('bu');
	   hreflink= printLink.value;
		//alert(printlink);
		var oldurl = location.href + "?";
		var stringHref = oldurl.split("?");
		var url;
		if (stringHref[1] != "")
			url = location.href + "&p=1";
		else
			url = location.href + "?p=1";
//alert(url);			
		printLink.href = url;
	 }
}

function subscribeToNewsletter(CaptchaImageText){
	
	var txtName = document.getElementById('txtName');
	var txtEmail = document.getElementById('txtEmail');
	var txtCaptcha = document.getElementById('CodeNumberTextBox');
	var checkedCategories = document.getElementById('checkedCategories');
	var url = location.href.split('?')[0];
	var newUrl = url;
	var redirect = true;
	var error = '';
	
	if (txtCaptcha.value == CaptchaImageText){
		if (txtName.value != ''){
			newUrl += '?name=' + txtName.value;
		}
		else{
			redirect=false;
			error += '1';
		}
		
		
		if (txtEmail.value != ''){
			newUrl += '&email=' + txtEmail.value;
			if (echeck(txtEmail.value)==false){
				redirect=false;
				error += '3';
			}
		}
		else{
			redirect=false;
			error += '2';
		}
		
		if (checkedCategories.value != '|'){
			newUrl += '&checkedCategories=' + checkedCategories.value;
		}
		else{
			redirect=false;
			error += '4';
		}

	}
	else{
		redirect=false;
		error += '0';
	}
	
	if (redirect==true){
		location.href = newUrl;
	}
	else{
		location.href = url + '?error=' + error;
	}
	
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}

function addChecked(categoryChecked){
	var checkedCategories = document.getElementById('checkedCategories');
	var cc = document.getElementById(categoryChecked);

	if (cc.checked)
	{
		checkedCategories.value += categoryChecked + '|';
	}
	else{
		checkedCategories.value.replace(categoryChecked + '|', "")
	}
}

function unSubscribeToNewsletter(subscrID){
	
	var checkedCategories = document.getElementById('checkedCategories');
	var url = location.href.split('?')[0];
	var newUrl = url;
	var redirect = true;
	var error = '';
	
	var txtEmail = document.getElementById('txtEmail');
	if (txtEmail != null)
	{
		if (txtEmail.value != ''){
			
			if (echeck(txtEmail.value)==false){
				redirect=false;
				error += '1';
			}
			else{newUrl += '?email=' + txtEmail.value;}
		}
		else{
			redirect=false;
			error += '2';
		}

	}
	else
	{	
		newUrl += '?subscrID=' + subscrID;
	}
	
	if (checkedCategories.value != '|'){
		newUrl += '&checkedCategories=' + checkedCategories.value;
	}
	else{
		redirect=false;
		error += '3';
	}
	
	if (redirect==true){
		location.href = newUrl;
	}
	else{
		if (subscrID == ""){
			location.href = url + '?error=' + error;
		}
		else{
			location.href += '&error=' + error;
		}
	}
	
}