
document.onkeyup = function(e)
{
	
	var event = e || window.event;
	var keynum = event.keyCode;
	
	var arr = new Array("Up","Con");

	if	(keynum==27)
	{
		var key;
		for (var i=0; i<2; i++)
		{
			var oP = $("popup"+arr[i]);
			var oAns = $("divAnswer"+arr[i]);	
			$("xSp" + arr[i]).className = "invisible";	
			oP.className="divCont" +arr[i]+" invisible";
			oP.className="divCont" +arr[i]+" invisible";
			oAns.className= "invisible";	
		}	
	}
}



var searchValue = '';

function checkInput(thisForm)
{
	var oTxtSearch = $('txtSearch');
	oTxtSearch.value = searchValue;
	searchValue = '';
	oFrm = $(thisForm);
	if ((oTxtSearch.value=='')||(oTxtSearch.value=='חפש באתר'))
	{
		alert('נא הכנס ערך לחיפוש');
		oTxtSearch.focus();
	}
	else
	{ 
		oFrm.submit();
	}
}

function showFormPopup(popType, frmName)
{
	//there are two possible popTypes -> con & up
	$('spWrongCode').style.display = "none";
	if (popType == 'Con')
	{
		secondPopType = 'Up';
	}
	else
	{
		secondPopType = 'Con';
	}
	
	oP = $("popup"+popType);
	oSecondP = $("popup"+secondPopType);
	oFrm = $(frmName);
	
	oAns = $("divAnswer"+popType);
	oSecondAns = $("divAnswer"+secondPopType);
	
	if (oP.className=="divCont"+popType+" invisible")
	{
		$("xSp"+popType).className = "visible";
		oP.className="divCont"+popType+" visible";
		oAns.className= "invisible";
		
		$("xSp"+secondPopType).className = "invisible";
		oSecondP.className="divCont"+secondPopType+" invisible";
		oSecondAns.className = "invisible";
		
		for (var i=0; i<oFrm.elements.length; i++)
		{
			oFrm.elements[i].value = '';
		}
	}
	else
	{
		$("xSp"+popType).className = "invisible";
		oP.className="divCont"+popType+" invisible";
		oAns.className= "invisible";
		
	}
}

function send(frmName, rnd)
{
	
	var poststr = "txtName=" + encodeURI( document.getElementById(frmName + "Form").txtConName.value )+
				"&txtEmail=" + encodeURI( document.getElementById(frmName + "Form").txtConEmail.value );
	if (frmName=='contact')
	{
	  	poststr+= "&txtPhone=" + encodeURI( document.getElementById(frmName + "Form").txtConPhone.value )+
		            "&txtText=" + encodeURI( document.getElementById(frmName + "Form").txtConText.value )+
		            "&rndchk=" + encodeURI( document.getElementById(frmName + "Form").con_rndchk.value ) +
		            "&rnd=" + encodeURI(rnd);     
		doAjax("/_ajax/contact.post.ajax.php", contactAfterAjax, poststr, false);
	}
	else if (frmName=='update')
	{
		doAjax("/_ajax/update.post.ajax.php", updateAfterAjax, poststr, false);
	}
}

function updateAfterAjax(strResponse)
{
	$("xSpUp").className = "invisible";
	$("divAnswerUp").className = "visible";
	$("divAnswerUp").innerHTML = strResponse;
}

function contactAfterAjax(strResponse)
{
	if (strResponse!="wrong_code"){
		$("xSpCon").className = "invisible";
		$("divAnswerCon").className = "visible";
		$("divAnswerCon").innerHTML = strResponse;
	}
	else{
		$('spWrongCode').style.display = "block";
	}
}

// global function to get elements
function $(id)
{
	return document.getElementById(id);
}


function validFormCon(strIdForm)
{
	var oFrm = $(strIdForm);
	if(oFrm.txtName.value==""){
		alert("נא הכנס שם שולח");
		oFrm.txtName.focus();
	}
	else if(oFrm.txtPhone.value==""){
		alert("נא הכנס מספר טלפון ליצירת קשר");
		oFrm.txtPhone.focus();
	}
	else if(!IsEmail(oFrm.txtEmail.value)||(oFrm.txtEmail.value=='')){
		alert("כתובת הדוא\"ל שהכנסת ריקה או אינה חוקית");
		oFrm.txtEmail.focus();
	}
	else if(oFrm.txtText.value==""){
		alert("נא הכנס תוכן הודעה");
		oFrm.txtText.focus();
	}
	else if(oFrm.rndchk.value==""){
		alert("נא הכנס קוד אבטחה");
		oFrm.rndchk.focus();
	}
	else
	{
		return true;
	}
}

function validFormCon1(strIdForm)
{
	var oFrm = $(strIdForm);
	if(oFrm.txtConName.value==""){
		alert("נא הכנס שם שולח");
		oFrm.txtConName.focus();
	}
	else if(oFrm.txtConPhone.value=="")
	{
		alert("נא הכנס מספר טלפון ליצירת קשר");
		oFrm.txtConPhone.focus();
	}
	else if(!IsEmail(oFrm.txtConEmail.value)||(oFrm.txtConEmail.value==''))
	{
		alert("כתובת הדוא\"ל שהכנסת ריקה או אינה חוקית");
		oFrm.txtConEmail.focus();
	}
	else if(oFrm.txtConText.value=="")
	{
		alert("נא הכנס תוכן הודעה");
		oFrm.txtConText.focus();
	}
	else if(oFrm.con_rndchk.value==""){
		alert("נא הכנס קוד אבטחה");
		oFrm.con_rndchk.focus();
	}
	else
	{
		return true;
	}
}

function validFormUp(strIdForm)
{
	var oFrm = $(strIdForm);
	if(oFrm.txtConName.value==""){
		alert("נא הכנס שם שולח");
		oFrm.txtConName.focus();
	}
	else if(!IsEmail(oFrm.txtConEmail.value)||(oFrm.txtConEmail.value==''))
	{
		alert("כתובת הדוא\"ל שהכנסת ריקה או אינה חוקית");
		oFrm.txtConEmail.focus();
	}
	else
	{
		return true;
	}
}

function IsEmail(sText) {
	var at="@";
	var dot=".";
	var lat=sText.indexOf(at)
	var lstr=sText.length
	var ldot=sText.indexOf(dot)
	if (sText=="" || sText==null){ return false }
	if (sText.indexOf(at)==-1 || sText.indexOf(at)==0 || sText.indexOf(at)==lstr){ return false }
	if (sText.indexOf(dot)==-1 || sText.indexOf(dot)==0 || sText.indexOf(dot)==lstr){ return false }
	if (sText.indexOf(at,(lat+1))!=-1){ return false }
	if (sText.substring(lat-1,lat)==dot || sText.substring(lat+1,lat+2)==dot){ return false }
	if (sText.indexOf(dot,(lat+2))==-1){ return false }
	if (sText.indexOf(" ")!=-1){ return false }
 	return true					
}