﻿var relativeSitePath = "/havilot";

function userlogOut(){
		areaToDisplay = 'logOut';
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		} 
		var url=relativeSitePath+"/webSite/Modules/Shop/functions.asp";
		url=url+"?f=userLogOut";
		xmlHttp.onreadystatechange=stateChanged ;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
}


function userLogin(){
	var email = document.forms[0].email.value;
	var Password = document.forms[0].psw.value;
	if(isValidEmail(email) && (Password!=''))
	{
		areaToDisplay = 'LoginForm';
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		} 
		var url=relativeSitePath+"/webSite/Modules/Shop/functions.asp";
		url=url+"?email="+email+"&pwd="+Password;
		url=url+"&f=userLogIn_dotNet";
		url=url+"&pid=<%%>&sid=<%%>";
		xmlHttp.onreadystatechange=stateChanged ;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	else
	{
		if(! isValidEmail(email)){
			alert('Please insert a valid Email');
			document.forms[0].email.focus();
			return;
		}
		if(Password==''){
			alert('Please insert password');
			document.forms[0].Password.focus();
			return;
		}
	}
}



function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}


function stateChanged() 
{ 
	//if(document.getElementById(areaToDisplay))
	//	document.getElementById(areaToDisplay).innerHTML="";
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		switch(areaToDisplay)
		{
			case "LoginForm":
				  //document.getElementById(areaToDisplay).innerHTML="";
				  var returnText = new String(xmlHttp.responseText);
				 			 
				  if(returnText.search(/USERFOUND=TRUE/gi)!=-1)
				  {
				  	//alert(returnText);
				  	document.getElementById("LoginTitle").innerHTML = 'Hello Yosi';
				  	document.getElementById("LoginUserName").style.display = 'none';
				  	document.getElementById("LoginPassword").style.display = 'none';
				  	document.getElementById("LoginSend").style.display = 'none';
				  	document.getElementById("LoginNewUser").style.display = 'none';
				  	document.getElementById("LoginLogout").style.display = '';				  	
				  }
				  else
				  {
				   alert(returnText);
				   //if( document.getElementById("btnContinue"))
				   //	document.getElementById("btnContinue").style.display='none';
				  }
				  break
		    case "logOut":
				  if(xmlHttp.responseText!=''){
					  //document.location.href = xmlHttp.responseText 
				  }
				  top.document.location.href = relativeSitePath+'/';
				  break
			default:
				  document.getElementById(areaToDisplay).innerHTML="";
				  document.getElementById(areaToDisplay).innerHTML=xmlHttp.responseText ;
		}
	} 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else 
		if (window.ActiveXObject)
		{
			objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	return objXMLHttp;
} 