//Function : validateLogin
//Date : 02.08.09
//Created By: H
//Description: Validates login fields
function validateUserLogin(txtUserName,txtPassword)
{
	if(txtUserName.value.length==0)
	{
		alert("Username can't be blank");
		return false;
	}
	else if(txtPassword.value.length==0)
	{
		alert("Password can't be blank");
		return false;
	}
}
function validateLogin(txtUserName,txtPassword)
{
	if(txtUserName.value.length==0)
	{
		alert("Username can't be blank");
		return false;
	}
	else if(txtPassword.value.length==0)
	{
		alert("Password can't be blank");
		return false;
	}
	else
	{
		return validateEmail(txtUserName.value);
	}
}


//Function : validateRegister
//Date : 11.08.09
//Created By: H
//Description: Validates registration fields
function validateRegister(frmRegister)
{ 
	if(frmRegister.txtEmail.value.length==0)
	{
		alert("Email can't be blank");
		frmRegister.txtEmail.focus();
		return false;
	}
	else
	{
		//var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var email = frmRegister.txtEmail.value;
		if(reg.test(email) == false) 
		{
		  alert('Invalid Email Address');
		  frmRegister.txtEmail.focus();
		  return false;
		}
	}

	if(checkUserName(frmRegister.txtEmail.value) == false)
	{
		return false;
	}

	if(frmRegister.txtPassword.value.length==0)
	{
		alert("Password can't be blank");
		frmRegister.txtPassword.focus();
		return false;
	}
	if(frmRegister.txtConfPassword.value.length==0)
	{
		alert("Confirm Password can't be blank");
		frmRegister.txtConfPassword.focus();
		return false;
	}
	else if(frmRegister.txtConfPassword.value!=frmRegister.txtPassword.value)
	{
		alert("Password and Confirm Password are not matching");
		frmRegister.txtConfPassword.focus();
		return false;
	}
	else if(frmRegister.cmbSecurityQue.value.length==0)
	{
		alert("Security Question need to be selected");
		frmRegister.cmbSecurityQue.focus();
		return false;
	}
	else if(frmRegister.cmbSecurityQue.value==0 && (frmRegister.txtSecurityQue.value.length==0 || frmRegister.txtSecurityQue.value=="Enter your question here..."))
	{
		alert("Enter your own Security Question");
		frmRegister.txtSecurityQue.focus();
		return false;
	}
	else if(frmRegister.txtAnswer.value.length==0)
	{
		alert("Answer can't be blank");
		frmRegister.txtAnswer.focus();
		return false;
	}
	else if(frmRegister.txtFirstName.value.length==0)
	{
		alert("Firstname can't be blank");
		frmRegister.txtFirstName.focus();
		return false;
	}
	else if(frmRegister.txtLastName.value.length==0)
	{
		alert("Lastname can't be blank");
		frmRegister.txtLastName.focus();
		return false;
	}
	else if(frmRegister.txtAlternateEmail.value.length!=0)
	{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var email = frmRegister.txtAlternateEmail.value;
		if(reg.test(email) == false) 
		{
		  alert('Invalid Alternate Email Address');
		  frmRegister.txtAlternateEmail.focus();
		  return false;
		}
	}
/*	else if(frmRegister.txtName.value.length==0)
	{
	alert("Name can't be blank");
	frmRegister.txtName.focus();
	return false;
	}
*/	
	if(frmRegister.txtAddress1.value.length==0)
	{
		alert("Street Address1 can't be blank");
		frmRegister.txtAddress1.focus();
		return false;
	}
	if(frmRegister.txtCity.value.length==0)
	{
		alert("City can't be blank");
		frmRegister.txtCity.focus();
		return false;
	}
	if(frmRegister.txtState.value.length==0)
	{
		alert("State can't be blank");
		frmRegister.txtState.focus();
		return false;
	}
	if(frmRegister.txtZip.value.length==0)
	{
		alert("Zip can't be blank");
		frmRegister.txtZip.focus();
		return false;
	}
	if(frmRegister.txtCountry.value.length==0)
	{
		alert("Country can't be blank");
		frmRegister.txtCountry.focus();
		return false;
	}
	if(frmRegister.txtDomainName.value.length==0)
	{
		alert("Domain Name can't be blank");
		frmRegister.txtDomainName.focus();
		return false;
	}
	if(frmRegister.txtNameOnCard.value.length==0)
	{
		alert("Name On Card can't be blank");
		frmRegister.txtNameOnCard.focus();
		return false;
	}
	if(frmRegister.txtCardNumber.value.length==0)
	{
		alert("Card Number can't be blank");
		frmRegister.txtCardNumber.focus();
		return false;
	}
	if(frmRegister.txtSecurityCode.value.length==0)
	{
		alert("Security Code can't be blank");
		frmRegister.txtSecurityCode.focus();
		return false;
	}
	if(checkCardInfo(frmRegister) == false)
	{
		return false;
	}
	if(frmRegister.txtCode.value=="")
	{
		alert("Code can't be blank");
		frmRegister.txtCode.focus();
		return false;
	}
}

//Function : validateRegisterUser
//Date : 12.08.09
//Created By: H
//Description: Validates registration fields
function validateRegisterUser(frmRegister)
{ 
	if(frmRegister.txtEmail.value.length==0)
	{
		alert("Email can't be blank");
		frmRegister.txtEmail.focus();
		return false;
	}
	else
	{
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var email = frmRegister.txtEmail.value;
		if(reg.test(email) == false) 
		{
		  alert('Invalid Email Address');
		  frmRegister.txtEmail.focus();
		  return false;
		}
	}

	if(checkUserName(frmRegister.txtEmail.value) == false)
	{
		return false;
	}
	
	if(frmRegister.txtConfEmail.value.length==0)
	{
		alert("Please confirm your email address.");
		frmRegister.txtConfEmail.focus();
		return false;
	}
	else
	{
		//var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var email = frmRegister.txtConfEmail.value;
		if(reg.test(email) == false) 
		{
		  alert('Invalid Confirm Email Address');
		  frmRegister.txtConfEmail.focus();
		  return false;
		}
	}
	if(frmRegister.txtConfEmail.value!=frmRegister.txtEmail.value)
	{
		alert("Your email and confirmed email do not match. Please make sure to enter the exact same email address in both fields.");
		frmRegister.txtConfEmail.focus();
		return false;
	}
	if(frmRegister.txtPassword.value.length==0)
	{
		alert("You must enter a password.");
		frmRegister.txtPassword.focus();
		return false;
	}
	if(frmRegister.txtConfPassword.value.length==0)
	{
		alert("Please confirm your password.");
		frmRegister.txtConfPassword.focus();
		return false;
	}
	if(frmRegister.txtConfPassword.value!=frmRegister.txtPassword.value)
	{
		alert("Your password and confirmed password do not match.");
		frmRegister.txtConfPassword.focus();
		return false;
	}
	if(frmRegister.cmbSecurityQue.value.length==0)
	{
		alert("Please select a Security Question.");
		frmRegister.cmbSecurityQue.focus();
		return false;
	}
	else if(frmRegister.cmbSecurityQue.value==0 && (frmRegister.txtSecurityQue.value.length==0 || frmRegister.txtSecurityQue.value=="Enter your question here..."))
	{
		alert("Please type in your security question or select a pre-set question.");
		frmRegister.txtSecurityQue.focus();
		return false;
	}
	if(frmRegister.txtAnswer.value.length==0)
	{
		alert("Answer can't be blank");
		frmRegister.txtAnswer.focus();
		return false;
	}
	if(frmRegister.txtFirstName.value.length==0)
	{
		alert("Please enter your first name.");
		frmRegister.txtFirstName.focus();
		return false;
	}
	if(frmRegister.txtLastName.value.length==0)
	{
		alert("Please enter your last name.");
		frmRegister.txtLastName.focus();
		return false;
	}
	if(frmRegister.txtAddress1.value.length==0)
	{
		alert("Street Address can't be blank");
		frmRegister.txtAddress1.focus();
		return false;
	}
	if(frmRegister.txtZip.value.length==0)
	{
		alert("Zip can't be blank");
		frmRegister.txtZip.focus();
		return false;
	}
	if(frmRegister.txtState.value.length==0)
	{
		alert("State can't be blank");
		frmRegister.txtState.focus();
		return false;
	}
	if(frmRegister.txtCity.value.length==0)
	{
		alert("City can't be blank");
		frmRegister.txtCity.focus();
		return false;
	}
	if (frmRegister.agreetoterms.checked == 0) {
		alert("You must agree to the Terms of Use to continue.");
		frmRegister.agreetoterms.focus();
		return false;
	}
}

//Function : validateInitiateSite
//Date : 11/24/08
//Created By: Jen
//Description: Validates registration fields
function validateInitiateSite(frmRegister) { 
	checked=false;
	for(i=0; i<frmRegister.SiteType.length; i++){
		if(frmRegister.SiteType[i].checked)
			checked=true;
	}
	if(!checked) {
		alert("You must select a site type.");
		frmRegister.SiteType.focus();
		return false;
	}
	
	if (frmRegister.realestate.checked) {
		if(frmRegister.REAgentId.value.length==0)
		{
			alert("Please enter your Agent ID.");
			frmRegister.REAgentId.focus();
			return false;
		}
		/*
		if(frmRegister.REOfficeId.value.length==0)
		{
			alert("Please enter your Office ID.");
			frmRegister.REOfficeId.focus();
			return false;
		}
		*/
		if(frmRegister.REBrokerage.value.length==0)
		{
			alert("Please enter your Brokerage name.");
			frmRegister.REBrokerage.focus();
			return false;
		}
	}
	
	if(frmRegister.Title.value.length==0)
	{
		alert("Please enter a title for your website.");
		frmRegister.Title.focus();
		return false;
	}
}


//Function : validateRegisterBilling
//Date : 12.08.09
//Created By: H
//Description: Validates billing fields
function validateRegisterBilling(frmRegister)
{ 
	if(frmRegister.txtNameOnCard.value.length==0)
	{
		alert("Name On Card can't be blank");
		frmRegister.txtNameOnCard.focus();
		return false;
	}
	if(frmRegister.txtCardNumber.value.length==0)
	{
		alert("Card Number can't be blank");
		frmRegister.txtCardNumber.focus();
		return false;
	}
	if(frmRegister.txtSecurityCode.value.length==0)
	{
		alert("Security Code can't be blank");
		frmRegister.txtSecurityCode.focus();
		return false;
	}
	if(checkCardInfo(frmRegister) == false)
	{
		return false;
	}
	if(frmRegister.txtAddress1.value.length==0)
	{
		alert("Street Address1 can't be blank");
		frmRegister.txtAddress1.focus();
		return false;
	}
	if(frmRegister.txtZip.value.length==0)
	{
		alert("Zip can't be blank");
		frmRegister.txtZip.focus();
		return false;
	}
	if(frmRegister.txtState.value.length==0)
	{
		alert("State can't be blank");
		frmRegister.txtState.focus();
		return false;
	}
	if(frmRegister.txtCity.value.length==0)
	{
		alert("City can't be blank");
		frmRegister.txtCity.focus();
		return false;
	}
}

//Function : forgetPwd
//Date : 01.11.09
//Created By: F
//Description: Redirects to forget password page
function forgetPwd(form)
{
	form.action = "index.php?file=forgetPwd";
	form.submit();
}


//Function : validateProfile
//Date : 01.10.09
//Created By: H
//Description: Validates profile fiels
function validateProfile(frmProfile)
{
	/*if(frmProfile.txtEmail.value.length==0)
	{
	alert("Email can't be blank");
	frmProfile.txtEmail.focus();
	return false;
	}
	else
	{
	if(!validateEmail(frmProfile.txtEmail.value))
	{
	alert('Invalid Email Address');
	frmProfile.txtEmail.focus();
	return false;
	}
	}*/
	
	
/*	if(frmProfile.txtAlternateEmail.value.length==0)
	{
		alert("Alternate email can't be blank");
		frmProfile.txtAlternateEmail.focus();
		return false;
	}
	else
	{
		if(!(validateEmail(frmProfile.txtAlternateEmail.value)))
		{
			alert('Invalid Alternate email');
			frmProfile.txtAlternateEmail.focus();
			return false;
		}
	}
*/	
	if(frmProfile.txtStreet1.value.length==0)
	{
		alert("Street Address1 can't be blank");
		frmProfile.txtStreet1.focus();
		return false;
	}
	else if(frmProfile.txtCity.value.length==0)
	{
		alert("City can't be blank");
		frmProfile.txtCity.focus();
		return false;
	}
	else if(frmProfile.txtState.value.length==0)
	{
		alert("State can't be blank");
		frmProfile.txtState.focus();
		return false;
	}
	else if(frmProfile.txtZip.value.length==0)
	{
		alert("Zip can't be blank");
		frmProfile.txtZip.focus();
		return false;
	}
/*	else if(frmProfile.txtCountry.value.length==0)
	{
		alert("Country can't be blank");
		frmProfile.txtCountry.focus();
		return false;
	}
*/}


//Function : validateWidget
//Date : 
//Created By: H
//Description: Validates widget fields
function validateWidget(frmWidget)
{

/*	if(frmWidget.txtName.value.length==0)
	{
		alert("Name can't be blank");
		frmWidget.txtName.focus();
		return false;
	}
*/	if(frmWidget.txtTitle.value.length==0)
	{
		alert("Title can't be blank");
		frmWidget.txtTitle.focus();
		return false;
	}
	if(frmWidget.txtContent.value.length==0)
	{
		alert("Content can't be blank");
		frmWidget.txtContent.focus();
		return false;
	}
}


//Function : activateWidget
//Date : 
//Created By: F
//Description: Activate widget
function activateWidget(id,nextView,operation)
{
	
	document.getElementById('hdn').value = id;
	document.frmWidgetMgnt.action = "index.php?file=widgetMgnt&view="+nextView+"&act=approved";
	document.frmWidgetMgnt.submit();	
}


//Function : inActivateWidget
//Date : 
//Created By: F
//Description: Inactivate widget
function inActivateWidget(id,nextView,operation)
{	
		document.getElementById('hdn').value = id;
		document.frmWidgetMgnt.action = "index.php?file=widgetMgnt&view="+nextView+"&act=blocked";
		document.frmWidgetMgnt.submit();
}


//Function : ActivateUser
//Date : 
//Created By: F
//Description: Activate user
function activateUser(id,nextView)
{
	
	document.getElementById('hdn').value = id;
	document.frmUserMgnt.action = "index.php?file=userMgnt&view="+nextView+"&act=activate";
	document.frmUserMgnt.submit();	
}

//Function : ActivateUser
//Date :
//Created By: F
//Description: Activate user
function inActivateUser(id,nextView)
{
	
		document.getElementById('hdn').value = id;
		document.frmUserMgnt.action = "index.php?file=userMgnt&view="+nextView+"&act=inactivate";
		document.frmUserMgnt.submit();
}


//Function : previewWidget
//Date :
//Created By: F
//Description: Preview widget
function previewWidget(id) 
{

	url = "../scripts/middle/previewWidget.php?widgetId="+id;
	window.open (url,"Widget","location=1,status=0,scrollbars=0,menubar=0,toolbar=0,resizable=0,width=800,height=800,left=100,top=100");
	// mywindow.moveTo(0,0);
}


//Function : check_usphone
//Date :
//Created By: F
//Description: Preview widget
function check_usphone(phoneNumber,id)
{
	if(phoneNumber != "")
	{
		if((phoneNumber.match(/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null) && (phoneNumber.match(/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/)==null))
		{ 
			alert("Please enter proper phone number");
			document.getElementById(id).focus();
			return false; 
		}
		else
		{
			a = phoneNumber.replace("(","");
			b = a.replace(")","");
			c = b.replace("-","");
			d = c.replace(" ","");
			document.getElementById(id).value = "("+d.substring(0,3)+") "+d.substring(3,6)+"-"+d.substring(6);
			return true;
		}
	}
} 

//Function : checkAll
//Date :
//Created By: F
//Description: check all checkboxes
function checkAll(arrChkBoxIds)
{
	if ((arrChkBoxIds != null)&&(document.getElementById('headerCheckBox') != null))
	{
		if(document.getElementById('headerCheckBox').checked == true)
		{
		// check to see if all other checkboxes are checked
			for (var i = 0; i < arrChkBoxIds.length; i++)
			{
				var cb = arrChkBoxIds[i];
				if (!cb.checked)
				{
					arrChkBoxIds[i].checked = true;
				}
			}
		}
		else
		{
		// check to see if all other checkboxes are checked
			for (var i = 0; i < arrChkBoxIds.length; i++)
			{
				var cb = arrChkBoxIds[i];
				if (cb.checked)
				{
					arrChkBoxIds[i].checked = false;
				}
			}
		}
	}
}


//Function : editWidget
//Date :
//Created By: F
//Description: Edit widget
function editWidget(id)
{	//alert(id);
	document.getElementById('editId').value = id;
	document.frmWidgetMgnt.action = "index.php?file=widgetMgnt";
	document.frmWidgetMgnt.submit();
}


//Function : confirmRemove
//Date :
//Created By: F
//Description: confirmation of remove widget
function confirmRemove(removeLink) 
{

	if(confirm('Are you sure you want to remove this widget?')) 
	{
		location.href = removeLink;
	}
	else
	{
		return false;
	}
}


//Function : validateChangePassword
//Date :
//Created By: H
//Description: validate confirm password fields
function validateChangePassword(frmChangePwd)
{
	if(frmChangePwd.cmbSecurityQue.value.length==0)
	{
		alert("Security Question need to be selected");
		frmChangePwd.cmbSecurityQue.focus();
		return false;
	}
	else if(frmChangePwd.cmbSecurityQue.value==0 && (frmChangePwd.txtSecurityQue.value.length==0 || frmChangePwd.txtSecurityQue.value=="Enter your question here..."))
	{
		alert("Enter your own Security Question");
		frmChangePwd.txtSecurityQue.focus();
		return false;
	}
	if(frmChangePwd.txtAnswer.value.length==0)
	{
		alert("Answer can't be blank");
		frmChangePwd.txtAnswer.focus();
		return false;
	}
//	if(frmChangePwd.txtOldPassword.value.length==0)
//	{
//		alert("Old Password can't be blank");
//		frmChangePwd.txtOldPassword.focus();
//		return false;
//	}
	if(frmChangePwd.txtNewPassword.value.length==0)
	{
		alert("New Password can't be blank");
		frmChangePwd.txtNewPassword.focus();
		return false;
	}
	if(frmChangePwd.txtConfirmNewPassword.value.length==0)
	{
		alert("Confirm New Password can't be blank");
		frmChangePwd.txtConfirmNewPassword.focus();
		return false;
	}
	if(frmChangePwd.txtNewPassword.value != frmChangePwd.txtConfirmNewPassword.value)
	{
		alert("New password and confirm new password are not match");
		return false;
	}

}

//Function : validateChangePassword
//Date :
//Created By: F
//Description: validate refer friend email list
function validateEmails(emailList)
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	
	if(emailList.length > 0)
	{
		var errorMsg = "";
		var arrEmail = emailList.split(";");
		
		for(i = 0; i < arrEmail.length; i++)
		{
			if(reg.test(arrEmail[i]) == false)
			{
				
				errorMsg = errorMsg + arrEmail[i] + "\n";
			}
		}
		
		if(errorMsg.length > 0)
		{
			alert("following emails are invalid\n" + errorMsg);
			return false;
		}
		else
		{
			return true;
		}

	}
	else
	{
		alert("Email is invalid.");
		return false;
	}
}


//Function : validateEmail
//Date :
//Created By: F
//Description: generalize function to validate email field in any form.
function validateEmail(email)
{
	//var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false)
	{
		alert('Invalid Email Address');
		return false;
	}
	else
	{
		return true;	
	}
}


//Function : validatCMSAdmineEmail
//Date :
//Created By: F
//Description: validat CMSAdmine email passwords
function validatCMSAdmineEmail(email,password)
{
	if(!validateEmail(email))
	{
		return false;		
	}
	else if(!validatePassword(password))
	{
		return false;
	}
	else	
	{
		return true;
	}
}


//Function : validatePassword
//Date :
//Created By: F
//Description: Generalize function to validate Password 
function validatePassword(password)
{
	if(password.length==0)
	{
		alert("invalid password.");
		return false;
	}
	else
	{
		return true;
	}
}


//Function : validateDomain
//Date :
//Created By: F
//Description: Generalize function to validate domain
function validateDomain(domain)
{
	//var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	//var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var reg = /^([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(domain) == false)
	{
		alert('Invalid domain name.');
		return false;
	}
	else
	{
		return true;	
	}
}


//Function : deleteCmsAdmin
//Date :
//Created By: F
//Description: Delete Cms admin user
/*
function deleteCmsAdmin(cmsAdminId)
{
	if(confirm('Are you sure you want to delete this administrator?'))
	{
		if(cmsAdminId > 0)
		{
			document.getElementById('hdnCmsAdminId').value = cmsAdminId;
			document.frmWebsiteInfo.submit();	
		}
		else
		{
			alert("Administrator user is not specified to delete.");	
			return false;
		}
	}
	else
	{
		return false;
	}
}
*/

function validateNewPassword(newPassword)
{
	if(newPassword.length<=3)
	{
		alert("please enter valid new password.");
		return false;
	}
	else
	{
		return true;
	}
}