// example Script.load("static/js/"+jsFile);

function showLightBox(id){
//document.getElementById('light_'+id).style.display='block';document.getElementById('fade_'+id).style.display='block';
shiftOpacity('light_'+id, 500,100,true);
shiftOpacity('fade_'+id, 500,90,true);
selectHandler('none');
}

function hideLightBox(id){
//document.getElementById('light_'+id).style.display='none';document.getElementById('fade_'+id).style.display='none';
shiftOpacity('light_'+id, 500,100,false);
shiftOpacity('fade_'+id, 500,90,false);
selectHandler('');
}

function selectHandler(val){
	var temp=document.getElementsByTagName("select");
	for(var i=0; i<temp.length; i++){					
			temp[i].style.display=val;
	}
}
/**
send ajax call to send the email to the friends,
data are send in post and from the form inputs
*/
function sendObjectToFriend(){
	var Ajax=getAjax();
	Ajax.open("POST",url+lang_abb+"/ajax/sendToFriend/",true);
	var params = "";
	params+="fromName="+document.getElementById('fromName').value;
	params+="&fromEmail="+document.getElementById('fromEmail').value;

	params+="&toName="+document.getElementById('toName').value;
	params+="&toEmail="+document.getElementById('toEmail').value;
	
	params+="&senderNotes="+document.getElementById('senderNotes').value;
	params+="&objectinfo="+document.getElementById('objectinfo').innerHTML;
	params+="&o_id="+document.getElementById('o_id').value;
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.send(params);
    hideLightBox('share');
	document.getElementById('feedback').innerHTML=defualtLang['email_sent'];
	document.getElementById('feedback').style.display="block";
}


/**
send ajax call to send the email to the friends,
data are send in post and from the form inputs
*/
function ajaxReport(){
	var Ajax=getAjax();
	Ajax.open("POST",url+lang_abb+"/ajax/report/",true);
	var params = "";
	if(document.getElementById('worngCat').checked)
	    params+="worngCat=ok";
	else
		params+="worngCat=no";

	if(document.getElementById('littleInfo').checked)
	    params+="&littleInfo=ok";
	else
		params+="&littleInfo=no";
	
	if(document.getElementById('notAvaliable').checked)
	    params+="&notAvaliable=ok";
	else
		params+="&notAvaliable=no";

	if(document.getElementById('otherAd').checked)
	    params+="&otherAd=ok";
	else
		params+="&otherAd=no";

	if(document.getElementById('inCorrectInfo').checked)
	    params+="&inCorrectInfo=ok";
	else
		params+="&inCorrectInfo=no";

	if(document.getElementById('fake').checked)
	    params+="&fake=ok";
	else
		params+="&fake=no";

	params+="&otherInfo="+document.getElementById('otherInfo').value;
	params+="&id="+document.getElementById('id').value;	
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.send(params);
    hideLightBox('report');
	document.getElementById('feedback').innerHTML=defualtLang['report_sent'];
	document.getElementById('feedback').style.display="block";
}

/**
*save the object to the user's favorites
**/
function saveObject(input){
	try{
	document.getElementById('feedback').style.display="none";
	}catch(e){}
	var Ajax=getAjax();
	Ajax.open("POST",url+lang_abb+"/ajax/saveObject/",true);
	var params = "";
	params+="input="+input;
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.onreadystatechange=function() {
		var done = 4, ok = 200;
		if (Ajax.readyState == done && Ajax.status == ok) {
		  if (Ajax.responseText) {
			saveObjectFeedBack(Ajax.responseText);
		  }
		}
	  };
	Ajax.send(params);
}

function saveObjectFeedBack(resp){
document.getElementById('feedback').innerHTML=resp;
document.getElementById('feedback').style.display="block";
}

/**
*show ajax logIn/signUp popup
**/
function logInSaveObject(id,isObject){
	if(document.getElementById('saveObjectIDLogedIN') && document.getElementById('saveObjectIDLogedIN').value!=0){
		if(isObject){
			uid= document.getElementById('saveObjectIDLogedIN').value
			input=encode64(uid+","+id);
			saveObject(input);
		}
		else{
			saveSearch(id);
		}
	}
	else{
		if(isObject){
			document.getElementById('saveObjectID').value=id;
					showLightBox("save");
		}
		else{
			try{
				document.getElementById('saveSearchID').value=1;
			}catch(e){}
			showLightBox("save");
		}
	}
}
function validateAjax(fields, submitBtn,theCase,theSubCase,params){
			fieldTemp=fields;
			hideById("logInError", true);
			noEmail=false;wrongEmail=false;
			wrongPW=false;noPW=false;
			fieldMissing=false;notSame=false;
				notChecked=false;
			noConf=false;errors=new Array;
			
			var inputFields = document.getElementById(fields).getElementsByTagName("input");
			for(var i=0; i<inputFields.length; i++){
				try{
				inputFields[i].focus();
				}catch(e){}
			}
			window.setTimeout("test1("+theCase+","+theSubCase+",'"+params+"')", 1);			
}

/*
*This function is just there to force the timeout otherwise IE doesn't work properly
theCase  0==signUp 1==login
theSubCase 0==save object   1== save query
*/
function test1(theCase,theSubCase,params){
			if(noEmail){errors.push(defualtLang[NOEMAIL]);}
			if(fieldMissing){errors.push(defualtLang[FIELDSMISSING]);}
			if(noPW){errors.push(defualtLang[NOPW]);}
			if(wrongPW){errors.push(defualtLang[WRONGPW]);}
			if(wrongEmail){errors.push(defualtLang[WRONGEMAIL]);}
			if(fieldMissing){errors.push(defualtLang[FIELDMISSING]);}
			if(notSame){errors.push(defualtLang[NOTSAME]);}
			if(noConf){errors.push(defualtLang[NOCONF]);}
			if(notChecked){errors.push(defualtLang[checkTerms]);}
			if(errors.length>0){displayErrors();}
			else{
				if(theCase==0)
					ajaxSignUp(theSubCase,params);		
				else{
					ajaxSignIn(theSubCase,params);
				}
			}
}
/**
*call ajax signup script
**/
function ajaxSignUp(theSubCase,params1){
	var Ajax=getAjax();
	Ajax.open("POST",url+lang_abb+"/ajax/submitSignUp/",true);
	var params = "";
	params+="email="+document.getElementById('email').value;
	params+="&confirmEmail="+document.getElementById('confirmEmail').value;
	params+="&password="+document.getElementById('password').value;
	params+="&confirmPassword="+document.getElementById('confirmPassword').value;
	params+="&captcha="+document.getElementById('captcha').value;
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.onreadystatechange=function() {
		var done = 4, ok = 200;
		if (Ajax.readyState == done && Ajax.status == ok) {
		  if (Ajax.responseText) {
			ajaxSignUpCallBack(Ajax.responseText,theSubCase,params1);
		  }
		}
	  };
	Ajax.send(params);
}
/**
*call back function for ajax signUP
*if signup ok do the save functionality
**/
function ajaxSignUpCallBack(resp,theSubCase,params1){
	try{
		theSubCase=document.getElementById('saveSearchID').value;
		document.getElementById('saveSearchID').value=0;
	}catch(e){}
	if(resp.indexOf("ok")>=0){
				temp=resp.substring(2);
		        arr=temp.split("/");
				try{
					document.getElementById('saveObjectIDLogedIN').value=arr[0];
				}catch(e){}
				try{
					document.getElementById('logInSideBar').style.display="none";
				}catch(e){}
				document.getElementById("welcomHeader").innerHTML=arr[1]+" <a href='/logOut/'>"+defualtLang['Log_out']+"</a>";
				document.getElementById("barHeader").innerHTML="&nbsp;|&nbsp;";
		if(theSubCase==0){//save object
			var uid=arr[0];
			var id=document.getElementById('saveObjectID').value;			
			input=encode64(uid+","+id);
			saveObject(input);
			hideLightBox("save");				
		}
		else{
			saveSearch(params1);
			hideLightBox("save");
		}
	}
	else{
	document.getElementById('signUpError').innerHTML=resp;
	document.getElementById('signUpError').style.display="block";
	}
}

/**
*call ajax signin script
**/
function ajaxSignIn(theSubCase,params1){
	var Ajax=getAjax();
	Ajax.open("POST",url+lang_abb+"/ajax/logIn/",true);
	var params = "";
	params+="email="+document.getElementById('emailLog').value;
	params+="&password="+document.getElementById('passwordLog').value;
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.onreadystatechange=function() {
		var done = 4, ok = 200;
		if (Ajax.readyState == done && Ajax.status == ok) {
		  if (Ajax.responseText) {
			ajaxSignUpCallBack(Ajax.responseText,theSubCase,params1);
		  }
		}
	  };
	Ajax.send(params);
}


/**
*save the search to the user's favorites
**/
function saveSearch(input){
	var Ajax=getAjax();
	Ajax.open("POST",url+lang_abb+"/ajax/saveSearch/",true);
	var params = "";
	params+="input="+input;
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.onreadystatechange=function() {
		var done = 4, ok = 200;
		if (Ajax.readyState == done && Ajax.status == ok) {
		  if (Ajax.responseText) {
			saveObjectFeedBack(Ajax.responseText);
		  }
		}
	  };
	Ajax.send(params);
}

/**
*get the quarters of the selected city and put it in the quarters dropdown
*/
function getQuartersAjax(select){
	document.getElementById('quartersContainer').innerHTML='<select id="quarterIDFK1" style="width:128px" disabled="disabled"><option>'+defualtLang['loading']+'</option></select>';
	document.getElementById('quarterIDFK').value="-1";
	if(select.value!=-1){
		var Ajax=getAjax();
		Ajax.open("POST",url+lang_abb+"/ajax/getQuarters/",true);
		var params = "";
		params+="CityName="+select.value;
		Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		Ajax.setRequestHeader("Content-length", params.length);
		Ajax.setRequestHeader("Connection", "close");
		Ajax.onreadystatechange=function() {
			var done = 4, ok = 200;
			if (Ajax.readyState == done && Ajax.status == ok) {
			  if (Ajax.responseText) {
				document.getElementById('quartersContainer').innerHTML=Ajax.responseText;
			  }
			}
		  };
		Ajax.send(params);
	}
	else{
		document.getElementById('quartersContainer').innerHTML='<select id="quarterIDFK1" style="width:128px" disabled="disabled"><option> </option></select>';
	}
}
function setHidden(obj){
	document.getElementById('quarterIDFK').value=obj.value;
}


/**
*get the quarters of the selected city and put it in the quarters dropdown
*/
function getQuartersAjax2(select){
	document.getElementById('quartersContainer').innerHTML='<select id="quarterIDFK1" style="width:128px" disabled="disabled"><option>'+defualtLang['loading']+'</option></select>';
	document.getElementById('quarterIDFK').value="-1";
	if(select.value!=-1){
		var Ajax=getAjax();
		Ajax.open("POST",url+lang_abb+"/ajax/getQuarters2/",true);
		var params = "";
		params+="CityName="+select.value;
		Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		Ajax.setRequestHeader("Content-length", params.length);
		Ajax.setRequestHeader("Connection", "close");
		Ajax.onreadystatechange=function() {
			var done = 4, ok = 200;
			if (Ajax.readyState == done && Ajax.status == ok) {
			  if (Ajax.responseText) {
				document.getElementById('quartersContainer').innerHTML=Ajax.responseText;
			  }
			}
		  };
		Ajax.send(params);
	}
	else{
		document.getElementById('quartersContainer').innerHTML='<select id="quarterIDFK1" style="width:128px" disabled="disabled"><option> </option></select>';
	}
}


/*
*my page section
*/
function ajaxGetMyStuff(subTab){
	showLightBox("loader");
	var Ajax=getAjax();
	Ajax.open("POST",url+"mypage/"+subTab,true);
	var params = "";
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.onreadystatechange=function() {
		var done = 4, ok = 200;
		if (Ajax.readyState == done && Ajax.status == ok) {
		  if (Ajax.responseText) {
				hideLightBox("loader");
				if(Ajax.responseText!="error")
			  		document.getElementById('detailTab1').innerHTML=Ajax.responseText;
				else
					window.location=url+"logInPage";
				if(subTab=="brokerTab"){
					new Accordian('basic-accordian',5,'header_highlight');
				}
		  }
		}
	  };
	Ajax.send(params);
}

function ajaxGetPage(container,sublink){
	showLightBox("loader");
var Ajax=getAjax();
	Ajax.open("POST",url+sublink,true);
	var params = "";
	Ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Ajax.setRequestHeader("Content-length", params.length);
	Ajax.setRequestHeader("Connection", "close");
	Ajax.onreadystatechange=function() {
		var done = 4, ok = 200;
		if (Ajax.readyState == done && Ajax.status == ok) {
		  if (Ajax.responseText) {
				hideLightBox("loader");
				document.getElementById(container).innerHTML=Ajax.responseText;
		  }
		}
	  };
	Ajax.send(params);
}
/************************Base64 functions****************************************/
/**
*javascript function to make base64 encoding
*@param input the srting to encode
*/
var keyStr = "ABCDEFGHIJKLMNOP" +"QRSTUVWXYZabcdef" +"ghijklmnopqrstuv" +"wxyz0123456789+/" +"=";
function encode64(input) {
  input = escape(input);  var output = "";  var chr1, chr2, chr3 = "";  var enc1, enc2, enc3, enc4 = "";
  var i = 0;
  do {
	 chr1 = input.charCodeAt(i++);
	 chr2 = input.charCodeAt(i++);
	 chr3 = input.charCodeAt(i++);
	 enc1 = chr1 >> 2;
	 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	 enc4 = chr3 & 63;
	 if (isNaN(chr2)) {
		enc3 = enc4 = 64;
	} else if (isNaN(chr3)) {
		enc4 = 64;
	 }
	 output = output +keyStr.charAt(enc1) +keyStr.charAt(enc2) +keyStr.charAt(enc3) +keyStr.charAt(enc4);
	 chr1 = chr2 = chr3 = "";
	 enc1 = enc2 = enc3 = enc4 = "";
  } while (i < input.length);
  return output;
}

  function decode64(input) {
        var output = "";var chr1, chr2, chr3;var enc1, enc2, enc3, enc4;
        var i = 0;
        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
        while (i < input.length) {
            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = Base64._utf8_decode(output);
        return output;

    }


/*************fade functions************************************************************************************/
	//shift opacity to show and hide the image
function shiftOpacity(id, millisec,opac,isIN)
{
	if(isIN){
		document.getElementById(id).style.display = 'block';
		document.getElementById(id).style.opacity = 0;
		changeOpac2(10, id); 
		opacity(id, 10, opac, millisec,isIN);
	}
	else
	{
		opacity(id, opac, 0, millisec,isIN);		
	}
}

function opacity(id, opacStart, opacEnd, millisec,isIN) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
	setTimeout("changeOpac(" + opacStart + ",'" + id + "',"+isIN+","+opacEnd+","+speed+")",speed); 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id,isIN,opacEnd,speed) { 
    var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 	
	var step=20;
	var browser=navigator.appName;
	if(browser.indexOf("internet")>0)
		step=30;
	if(isIN){
		if(opacity<opacEnd){
			opacity+=step;			
			setTimeout("changeOpac(" + opacity + ",'" + id + "',"+isIN+","+opacEnd+","+speed+")",speed); 
		}
	}
	else{
		if(opacity>opacEnd){
			opacity-=step;
			setTimeout("changeOpac(" + opacity + ",'" + id + "',"+isIN+","+opacEnd+","+speed+")",speed); 
		}
		else
			document.getElementById(id).style.display = 'none';
	}
}
function changeOpac2(opacity, id) { 

	var object = document.getElementById(id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}