var newWin=null;
var onscreen=false;
//var FilbaladURL = window.location.protocol+'//'+window.location.host+'/Mangam/English';
var FilbaladURL = window.location.protocol+'//'+window.location.host+'/English';
var IsRedirect = false;
var RedirectURL = "";
var thisDate = new Date();
var fnToLoad = function(){};
var PageName  = window.location.pathname.split('/')[window.location.pathname.split('/').length-1]; 
function NewWin(url,name,xpos,ypos,width,height)
    {
       newWin=window.open(
           url,
           name,
           "screenX="+xpos+",screenY="+ypos+",WIDTH="+width+",HEIGHT="+height+ 
           ",location=0,resizable=0,status=0,titlebar=1,directories=0,toolbar=0,menubar=0,scrollbars=0,status=0"
       );
       newWin.focus();
       onscreen=true; 
    }
 
//--------------------------------------------------------------------
function ValidateEmail(Email)
{	
	var myRegExp = new RegExp()
	myRegExp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(!myRegExp.test(Email))
	{
		return false;
    }
    else
	{
		return true;
	}
}	
//------------------------------------------------------------------
function PreviewPic(fileObj,imgID)
{	
	//alert(window.navigator.appName)
	var imgObj = document.getElementById(imgID);	
	imgObj.src = 'file:///'+fileObj.value;
	//alert(fileObj.value)
}	
//------------------------------------------------------------------
//'Javascript function that validate checkboxes if they ae selected or not
function validateChkBoxs(ChkBoxsname)
	{
		var ChkBoxs	=	document.getElementsByName(ChkBoxsname);
		for(i=0; i < ChkBoxs.length; i++)
		{
			if(ChkBoxs[i].checked)
				{
					return true;					
				}							
		}
		alert("You Must Check At Least One Checkbox!");
		return false;
	}
//-------------------------------------------------------------------
function CheckOrClear(ChechBoxName)
	{
	var i,checkObj;
	checkObj	= ChechBoxName;
	if(checkObj!=null){
	for(i = 0;i < checkObj.length;i++)
		{	if(checkObj[i].checked){checkObj[i].checked = false;}
			else{checkObj[i].checked = true;}						
		}}
	}	
function checkdates(Prefix)
{
	var mdays	= new Array(31,28,31,30,31,30,31,31,30,31,30,31);	
	yearObj     = document.getElementById(Prefix+'Year');
	monthObj    = document.getElementById(Prefix+'Month');
	dayObj      = document.getElementById(Prefix+'Day');	
	var y		= parseInt(yearObj.value);
	var m		= parseInt(monthObj.value);
	var d		= parseInt(dayObj.value);
	if(y%4 == 0)
		mdays[1] = 29;
	if(d > mdays[m-1])
		dayObj.selectedIndex = mdays[m-1];
}		
var ownerClicked = new Array();
function showOwner(usrid,carid)
{	
	var ContactDetails = new ContactCarsAjax();
	if(!ownerClicked[carid])
	{
		ContactDetails.Load("UsedCars_List.asp","action=show&carid="+carid+"&userid="+usrid,"owner_"+carid,callBack);
		document.getElementById("owner_"+carid).style.display='block';
		ownerClicked[carid]=true;
	}
	else
	{		
		document.getElementById("owner_"+carid).style.display='none';
		ownerClicked[carid]=false;
	}
	ContactDetails = null;
}
//---------------------------------------------------------------------------
function SubmitLogin()
{	
	var msg		= "";
	var IsValid	= false;	
	var userName	= document.loginForm.userName.value;
	var userPass	= document.loginForm.userPass.value;	
	var chkRemeber  = document.loginForm.chkRemeber;	
	if(userName=="")
	{
		msg	= "- Please, enter a valid username!\n"	
		alert(msg);
		document.loginForm.userName.focus();
		return IsValid;			
	}
	if(userPass == "")
	{
		msg = "- Please, enter the password !\n"
		alert(msg);
		document.loginForm.userPass.focus();
		return IsValid;							
	}	
	if(chkRemeber.checked)
	{
		chkRemeber = "on";
	}
	else
	{
		chkRemeber = "off";
	}
	IsValid	= true;
	
	PostedData = 'userName='+escape(userName);
	PostedData = PostedData+'&userPass='+escape(userPass);	
	PostedData = PostedData+'&action=Check&type=loginpage&chkRemeber='+chkRemeber; 	
		
	if(IsValid)
	{	
		Filbalad.Submit(FilbaladURL+'/includes/MemberIncludes/loginAjax.asp',PostedData,'MembersLoginBox',callBackLogin);
	}
}
function SendPass()
{
	var msg		= "";
	var IsValid	= false;	
	var userName	= document.ForgetPassForm.UserName.value;
	var userEmail	= document.ForgetPassForm.UserEmail.value;		
	if(userName=="" & userEmail == "")
	{
		msg	= "- Please, enter a valid username!\n"	
		alert(msg);
		document.ForgetPassForm.UserName.focus();
		return IsValid;			
	}
	
	IsValid	= true;
	
	PostedData = 'userName='+escape(userName);
	PostedData = PostedData+'&userEmail='+escape(userEmail);	
	PostedData = PostedData+'&action=sendpass'; 	
		
	if(IsValid)
	{	
		Filbalad.Submit(FilbaladURL+'/includes/MemberIncludes/ForgotPass.asp',PostedData,'MembersLoginBox',callBack);
	}
}
function ReSendActivation()
{
	var msg		= "";
	var IsValid	= false;	
	var userName	= document.ReSendActivationForm.UserName.value;	
	if(userName=="")
	{
		msg	= "- Please, enter a valid username!\n"	
		alert(msg);
		document.ReSendActivationForm.UserName.focus();
		return IsValid;			
	}
	
	IsValid	= true;
	
	PostedData = 'userName='+escape(userName);	
	PostedData = PostedData+'&action=sendactivation'; 	
		
	if(IsValid)
	{	
		Filbalad.Submit(FilbaladURL+'/includes/MemberIncludes/ReSendActivate.asp',PostedData,'MembersLoginBox',callBack);
	}
}
function key(e)
{
	if(e.keyCode == 13)
		{
			return SubmitLogin();
		}
	if(e.which == 13)
		{
			return SubmitLogin();
		}
}
//----------------
function callBackLogin(xmlHttp,resObject)
	{	
	 if(xmlHttp.readyState==4)	 
		{ 
			var loginSpan = '';	
			var tempText  = '';			
			if(PageName.toLowerCase()!="openSite.asp".toLowerCase())
			{
				loginSpan = document.getElementById('menu_login');
				tempText  = loginSpan.innerHTML;
			}
			var ReqTxt = xmlHttp.responseText.split("$$");			
			if(parseInt(ReqTxt[0])==1)//If login correct
			{			
				var loginMsg  = " "
					//loginMsg += "<li id=\"menu_login\">";
					loginMsg += "	<a href=\"UserProfile.asp?userid="+ReqTxt[2]+"\">Hello";//userid
					loginMsg += "		"+ReqTxt[3]+"";//username
					loginMsg += "	</a>";
					loginMsg += "|";
					loginMsg += "<a href=\"myAds.asp\"> My previous ads ";
					loginMsg += "	</a>";
					loginMsg += "|";
					loginMsg += "<a onclick=\"LoginOut()\" href=\"javascript:void(null);\"> log out</a>";
					//loginMsg += "</li>";
				tempText      = "";					
				tempText	  = loginMsg ;				
				setTimeout("hideLogin()",3000);
				if(IsRedirect){if(RedirectURL==''){location.href="?";}else{location.href=RedirectURL;};}
				if(typeof(fnToLoad)=='function'){fnToLoad()}				
			}			
			loginSpan.innerHTML = tempText;
			resObject.innerHTML	= ReqTxt[1];//Msg to be written	
			
		}
	}
function LoginTryAgain()
{
	document.getElementById('MembersLoginBox').innerHTML = LoginFormHtml;
}	
function LoginOut()
{
	javascript:FilbaladLogin.Load(FilbaladURL+'/includes/MemberIncludes/logOutAjax.asp','action=logout','menu_login',callBack);
}	
function ShowSendPassForm()
{
	javascript:Filbalad.Load(FilbaladURL+'/includes/MemberIncludes/ForgotPass.asp','','MembersLoginBox',callBack);
}	
function SendActivationForm()
{
	javascript:Filbalad.Load(FilbaladURL+'/includes/MemberIncludes/ReSendActivate.asp','','MembersLoginBox',callBack);
}
function ShowRegisterMiniForm()
{
	javascript:Filbalad.Load(FilbaladURL+'/includes/MemberIncludes/RegisterMini.asp','','MembersLoginBox',callBack);
}

function RegisterMiniSubmit()
{
	var IsValid	= false;	
	var specialChar = /[^a-zA-Z0-9_]+/;
	
	var UserName	= document.RegisterMiniForm.UserName.value;
	
	var Password	= document.RegisterMiniForm.Password.value;
	var PasswordConfirm= document.RegisterMiniForm.PasswordConfirm.value;
	
	var Email		= document.RegisterMiniForm.Email.value;	
	
	var FirstName	= "";//document.RegisterMiniForm.FirstName.value;
	var LastName	= "";//document.RegisterMiniForm.LastName.value;
	
	if (UserName=="")
	{
		alert("The username must be submitted !");
		document.RegisterMiniForm.UserName.focus();
		return false;
	}
	else
	{
		if (UserName.length <3) 
		{
			alert("The username must be three characters at least !");
			document.RegisterMiniForm.UserName.focus();
			return false;
		}
	
		else
		{
			if(UserName.search(specialChar) != -1)
			{
				alert("The username should not contain spaces, punctuation marks or special characters !");
				document.RegisterMiniForm.UserName.focus();
				return false;
			}
				
		}
	}		
	///////// PASSWORD //////////////////
	if (Password=="")
	{
	alert("The password must be entered !");
	document.RegisterMiniForm.Password.focus();
	return false;
	}
	else
	{
	if (Password.length <7) 
		{
			alert("The password must contain at least seven characters !");
			document.RegisterMiniForm.Password.focus();
			return false;
		}
	
		else
		{
		if(Password.search(specialChar) != -1)
			{
				alert("The password should not contain spaces, punctuation marks or special characters !");
				document.RegisterMiniForm.Password.focus();
				return false;
			} 
		}
		if (PasswordConfirm!=Password)
			{
				alert("Incorrect confirmation ");
				document.RegisterMiniForm.PasswordConfirm.focus();
				return false;
			} 
	}
	/////////// Email //////////////////////
	if (Email=="")
		{	
		alert("The email should be submitted !");
		document.RegisterMiniForm.Email.focus();
		return false;
		}
	if (!ValidateEmail(Email))
	{
		alert("The email is invalid !") 
		document.RegisterMiniForm.Email.focus();
		return false;
	}
	

	
	///////////// FirstName Last Quistion /////////////////////////
	/*if(FirstName=="")
	{
		alert("!يجب إدخال إسم المستخدم الاول");
		document.RegisterMiniForm.FirstName.focus();
		return false;		
	}
	else
	{
		if (FirstName.length <3) 
		{
			alert("!يجب أن يتكون اسم المستخدم الاول من 3 خانات على الأقل");
			document.RegisterMiniForm.FirstName.focus();
			return false;
		}		
	}*/
	IsValid	= true;		
	PostedData = 'UserName='+escape(UserName);
	PostedData += '&Email='+escape(Email);	
	PostedData += '&Password='+escape(Password);	
	PostedData += '&FirstName='+escape(FirstName);	
	PostedData += '&LastName='+escape(LastName);	
	PostedData += '&action=miniregister'; 	
		
	if(IsValid)
	{	
		Filbalad.Submit(FilbaladURL+'/includes/MemberIncludes/RegisterMini.asp',PostedData,'MembersLoginBox',callBack);
	}			
}
function CheckTxtChars(txtBoxObj)
{
	if(txtBoxObj.value==""|txtBoxObj.value.length < 3)
	{
		alert("The name should be three characters at least ") 
		txtBoxObj.focus();
		return false;
	}
	var txtBoxObjVal = txtBoxObj.value;	
	var specialChar = /[^a-zA-Z0-9_]+/;
	if(txtBoxObjVal.search(specialChar) != -1)
		{
			alert("The word should not contain spaces, punctuation marks or special characters !");
			txtBoxObj.focus();
			return false;
		}				
	return true;
}
function CheckEmail(Email)
{	
	if(Email.value=="")
	{
		alert("The email is invalid !") 
		Email.focus();
		return false;
	}
	var check	=/@[\w\-]+\./;
	var checkend=/\.[a-zA-Z]{2,3}$/;
	if(((Email.value.search(check)) == -1)||(Email.value.search(checkend) == -1))
		{	
			alert("The email is invalid !") 
			Email.focus();
			return false;
		}
	return true;	
}
function CheckUserOrEmail(VarFlag,DivName)
{
	var imgDiv = document.getElementById(DivName);
	imgDiv.innerHTML = '';			
	if(VarFlag == 'username')
	{				
		ParamsVar = "username="+document.RegisterMiniForm.UserName.value;		
		if(!CheckTxtChars(document.RegisterMiniForm.UserName))
		{
			return false;
		}	
	}
	else if(VarFlag == 'useremail')
	{	
		ParamsVar = "useremail="+document.RegisterMiniForm.Email.value;
		if(!CheckEmail(document.RegisterMiniForm.Email))
		{
			return false;
		}
	}	
	PageName = FilbaladURL+'/includes/MemberIncludes/CheckUser.asp';	
	Filbalad.Load(PageName,'jsAjaxObj=Filbalad&callBackFn=callBack&divRes='+DivName+'&'+ParamsVar,DivName,callBack);
}
function ResetChk(str)
{
	if(str == 'user')
	{
		document.getElementById('chkuser').innerHTML = "<label>&nbsp;</label><a href=\"#\" onclick=\"javascript:CheckUserOrEmail('username','chkuser');\">Check username availability</a>";
	}
	else(str== 'email')
	{
		document.getElementById('chkemail').innerHTML = "<label>&nbsp;</label><a href=\"#\" onclick=\"javascript:CheckUserOrEmail('useremail','chkemail');\">Check email availability</a>";
	}
}
