function displayErrorMessage(errorMessage) {
	err = document.getElementById("error");   
	err.innerHTML = errorMessage;
	
}
function fnIsNumeric(value){
	var regEx = /^[0-9]+$/;
	if (!regEx.test(value)){
		return false;
	}
	return true;	
}
//phone number validation
function fnValidatePhone(phoneNumber) {

	var temp = "";
	temp = phoneNumber;
	var firstDigit= temp.substring(0,1);
    var numeric = fnIsNumeric(temp);
	if (!numeric){
    	return false;
	}
	else  if(temp.length >= 12 || temp.length < 10) {
  		return false;
    }  
  
  	else if (temp.length == 10 )
  	{
   		if((firstDigit == 0 || firstDigit == 1))  
     	{
     		return false;;
    	}
      	return true;
  	}
 	else if (temp.length == 11  )
  	{
  		if((firstDigit == 0 || firstDigit == 1))  
      	{
  			var ch = temp.charAt(1);
  			if(ch == 0 || ch == 1)
   			{       
    	  			return false;
  			}
 		}
 		return true;
 	} 
}
//phone number trim for phone number validation

function phoneTrim(sString)

{
sString = trimLeft(sString); 
sString =trimRight(sString);
return sString;
}
function trimLeft(sString) 
{
while (sString.substring(0,1) == ' ')
{
sString = sString.substring(1,sString.length);
}
return sString;
}

function trimRight(sString) 
{
while (sString.substring(sString.length-1,sString.length) == ' ')
{
sString = sString.substring(0,sString.length-1);
}
return sString;
}
//reformat 
function reformat(value) {
	
	// remove special characters like "\/-(). " and "," 
	re = /\$| |\(|\)|\+|\[|\-|\_|\]|\[|\}|\{|\\|\/|\$|\./g;		
	var valueret = value.replace(re, "");
	return valueret;
}


//dropdown validation
function fnDropDownValidate(value)
{
	if(value==0)
	{
		return 0;
		}
	return 1;
}

//Name validation function
function nameValidation(name)
{
	if(!((name.charCodeAt(0)>64 && name.charCodeAt(0)<91)||(name.charCodeAt(0)>96 && name.charCodeAt(0)<123)))
	{
		return 0;
	}

	for(iLoop=1;iLoop<name.length;iLoop++)
	{
		if(!((name.charCodeAt(iLoop)>64 && name.charCodeAt(iLoop)<91)||(name.charCodeAt(iLoop)>96 && name.charCodeAt(iLoop)<123)||
				(name.charCodeAt(iLoop)>48 && name.charCodeAt(iLoop)<58)||(name.charCodeAt(iLoop)==32)||(name.charCodeAt(iLoop)==39)||
				(name.charCodeAt(iLoop)==45)||(name.charCodeAt(iLoop)==46)))
		{
			return 0;
		}
	}                       

	if(iLoop == name.length)
	{
		return 1;
	}
	return 0;
}
function checkEmail(email)
{ 
	
	//var emailAddress = document.getElementById("emailValue");
	var regex1 = /^[^\s@]+@([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]\.|[A-Za-z0-9]\.)+([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]|[A-Za-z0-9])$/;
	var regex2 = /^(root@|abuse@|spam@)/;
	var emailLC = email.toLowerCase();
	if(!email.match(regex1))
	{
		displayErrorMessage("The e-mail address you have entered is not valid. Email should be of the form abct@de.com");
		//emailAddress.select();
		//emailAddress.focus();
		return false;
	} 
	else if(emailLC.match(regex2))
	{  
		displayErrorMessage("Email address should not contain root or spam or abuse.");  
	//	emailAddress.select();
	//	emailAddress.focus();
		return false;
	}

	return true;
}
//Trim function 
function trimStr( value ) {
	return LTrim(RTrim(value));
}
function LTrim(str) { 
	for (var k=0; k<str.length && str.charAt(k)<=" " ; k++) ;
	return str.substring(k,str.length);
}
function RTrim(str) {
	for (var j=str.length-1; j>=0 && str.charAt(j)<=" " ; j--) ;
	return str.substring(0,j+1);
}
function emailValidation()
{	
	var email = document.getElementById("emailValue").value;
	
	if(checkEmail(email)){
	//document.login_form.emailAddressValue.value = email; 
	document.login_form.emailAddressValue.value = email; 
	document.login_form.action="/d/sweepstakes/sweep_stakes_enroll.jsp";
	document.login_form.method ="POST";
	document.login_form.submit(); 
	
		
	}
}
function enrollSubmit(){
	if(validateEnroll()){
		document.enrollform.action="/d/sweepstakes/sweep_stakes_enroll.jsp?action=submit";
		document.enrollform.method ="POST";
		document.enrollform.submit(); 
}}

function validateEnroll()
{
	
var firstName = document.getElementById("id_first_name");

var lastName = document.getElementById("id_last_name");


var primaryEmailAddress = document.getElementById("id_email_address");
var confirmPrimaryAddress = document.getElementById("id_confirm_email");
var primaryPhoneVal = document.getElementById("id_phone_number");
//First name validation
if(firstName.value != null && trimStr(firstName.value) != "")
{	if(nameValidation(firstName.value) == 0)
	{  displayErrorMessage("A valid First name may only contain alphas, numerics, and the following special characters: hyphen, space, apostrophe, and period. A name should not start with a special character or a number.");
		return false;
	}
}
else
	
{
	displayErrorMessage("Please enter a first name.");
	return false;
}


//Last Name validation
if(document.getElementById("id_last_name") != null && trimStr(document.getElementById("id_last_name").value) != "")
{   
	if(nameValidation(document.getElementById("id_last_name").value) == 0)
	{ 
		displayErrorMessage("A valid Last name may only contain alphas, numerics, and the following special characters: hyphen, space, apostrophe, and period. A name should not start with a special character or a number.");    
		return false;
	}
}
else
{
	displayErrorMessage("Please enter a last name.");
	return false;
}

//Primary Email validations
var primaryEmail = document.getElementById("id_email_address").value;
var confirmPrimary = document.getElementById("id_confirm_email").value;
if(primaryEmail.toLowerCase() == confirmPrimary.toLowerCase())
{
	if(primaryEmail!='')
	{
		email1 = checkEmail(primaryEmail);
		if(!email1)
		{                                                                                                           
			displayErrorMessage("The e-mail address you have entered is not valid. Email should be of the form abc@xyz.com");
			primaryEmailAddress.select();
			primaryEmailAddress.focus();
			return false;
		}
	}
	else
	{
		displayErrorMessage("Error-Please enter a valid email");    
		primaryEmailAddress.select();
		primaryEmailAddress.focus();
		return false;
	}

}
else
{
	displayErrorMessage("Error-Email addresses don't match.Please re-enter the same email address in the confirm email textbox.");
	confirmPrimaryAddress.focus();
	confirmPrimaryAddress.select();
	return false;
}

var age=fnDropDownValidate(document.getElementById("id_age_drpdwn").selectedIndex)

if(age==0)
{
	displayErrorMessage("Please select your age.");
	document.getElementById("id_age_drpdwn").focus();
	//document.getElementById("id_age_drpdwn").select();
	return false;
}


if(primaryPhoneVal.value != "")
	{
	var phone = fnValidatePhone(phoneTrim(reformat(primaryPhoneVal.value)));
	
	if(!phone)
	{
		displayErrorMessage("The phone number you entered as your primary phone is not a valid 10-digit number.Please check your information and try again.");
		primaryPhoneVal.focus();
		primaryPhoneVal.select();
		return false;
		}
	 }
	var profession=fnDropDownValidate(document.getElementById("id_profession").selectedIndex)
	
	if(profession==0)
	{
		displayErrorMessage("Please say us who are you?.");
		document.getElementById("id_profession").focus();
		return false;
	}

	var storeNumber = document.getElementById("id_store_number").value;

	if(storeNumber!=""){
	
	var numeric = fnIsNumeric(storeNumber);
	if(!numeric)
	{
		displayErrorMessage("Store Number can only be numeric. Please re-enter the value.");
		document.getElementById("id_store_number").focus();
		document.getElementById("id_store_number").select();
		return false;
	}}
	
	
	var address = document.getElementById("id_address");
	if(address.value == "")
 	{
 		displayErrorMessage( "Please enter the address .");
 		document.getElementById("id_address").focus();
 		document.getElementById("id_address").select();
			return false;
	 	}

	var city = document.getElementById("id_city");
	if(city.value == "")
 	{
 		displayErrorMessage( "Please enter the city .");
 		document.getElementById("id_city").focus();
 		document.getElementById("id_city").select();
			return false;
	 	}
	
	var state=fnDropDownValidate(document.getElementById("id_stateDropDown").selectedIndex)

	if(state==0)
	{
		displayErrorMessage("Please select a state.");
		document.getElementById("id_stateDropDown").focus();
		//document.getElementById("id_stateDropDown").select();
		return false;
	}
	
	var zipcode = document.getElementById("id_zip_code");
	
	if(zipcode.value == "")
 	{
 		displayErrorMessage( "Please enter a Zip code.");	
 		document.getElementById("id_zip_code").focus();
 		document.getElementById("id_zip_code").select();
	return false;
	}
	var zip = fnIsNumeric(zipcode.value);
	if(!zip)
	{
		displayErrorMessage("Please enter a valid 5-digit ZIP code. ");
		document.getElementById("id_zip_code").focus();
 		document.getElementById("id_zip_code").select();
		return false;
	}
	
	if(zipcode.value.length != 5)
	{
		displayErrorMessage("Please enter a valid 5-digit ZIP code. ");
		document.getElementById("id_zip_code").focus();
 		document.getElementById("id_zip_code").select();
		return false;
	}
		
	var referrer = document.getElementById("id_referrer");
	if(referrer.value!="") {
		if(!checkEmail(referrer.value))
		{
			displayErrorMessage("The e-mail address you have entered is not valid. Email should be of the form abc@xyz.com");
			document.getElementById("id_referrer").focus();
			document.getElementById("id_referrer").select();
			return false;
		}
	}
	
	var captchaId = document.getElementById("id_captcha_response");
	if(captchaId.value == "")
 	{
 		displayErrorMessage( "Please type the characters you see in the box.");	
 		document.getElementById("id_captcha_response").focus();
 		document.getElementById("id_captcha_response").select();
	return false;
	}
	
	return true;
}

function redirect(){
	document.reg_success_form.action="/d/sweepstakes/sweep_stakes_invite_friends.jsp";
	document.reg_success_form.method ="POST";
	document.reg_success_form.submit();
	
	
}
function redirectToInviteFriends(emailId,sweepId){
	
	document.location.href="/d/sweepstakes/sweep_stakes_invite_friends.jsp?emailId="+emailId+"&SwpId="+sweepId;
	//document.enrollform.method ="POST";
}

function validateSweepFriendsDetails(){
		
	var textArea=document.getElementById("recipient_list").value;
	var emailAddress1=document.getElementById("id_email_1").value;
	
	var emailAddress2=document.getElementById("id_email_2").value;
	
	var emailAddress3=document.getElementById("id_email_3").value;
	
	var emailAddress4=document.getElementById("id_email_4").value;
	
	
	var emailId1=document.getElementById("id_email_1");
	var emailId2=document.getElementById("id_email_2");
	var emailId3=document.getElementById("id_email_3");
	var emailId4=document.getElementById("id_email_4");
	if(textArea=="" && emailAddress1=="" && emailAddress2=="" && emailAddress3=="" && emailAddress4=="" )
	{
		displayErrorMessage("Please enter your friends list from address book or enter it manually.");
		return false;
	}
	
	if(emailAddress1!=""){
		var emailCheck1=checkEmail(emailAddress1);
		if(!emailCheck1){
			displayErrorMessage("The e-mail address you have entered is not valid. Email should be of the form abc@xyz.com");
			emailId1.focus();
			emailId1.select();
		return false;
	}
}
	if(emailAddress2!=""){
	var emailCheck2=checkEmail(emailAddress2);
	if(!emailCheck2){
		displayErrorMessage("The e-mail address you have entered is not valid. Email should be of the form abc@xyz.com");
		document.getElementById("id_email_2").focus();
		document.getElementById("id_email_2").select();
		return false;
	}}
	if(emailAddress3!=""){
	var emailCheck3=checkEmail(emailAddress3);
	if(!emailCheck3){
		displayErrorMessage("The e-mail address you have entered is not valid. Email should be of the form abc@xyz.com");
		document.getElementById("id_email_3").focus();
		document.getElementById("id_email_3").select();
		return false;
	}}
	
	if(emailAddress4!=""){
	var emailCheck4=checkEmail(emailAddress4);
	if(!emailCheck4){
		displayErrorMessage("The e-mail address you have entered is not valid. Email should be of the form abc@xyz.com");
		emailId4.focus();
		emailId4.select();
		return false;
	}}
	
	return true;
}

function validateFrendsDetails(){

	if(validateSweepFriendsDetails())
		{
		var recipientList = document.getElementById("recipient_list").value;
		var emailValue = "";
		if(recipientList!=null && recipientList!="") {
			var temp = new Array();
			temp =recipientList.split(",");
			for(var i=0;i<temp.length;i++){
				if (i==0) {
					emailValue=emailValue+temp[i].substring(temp[i].indexOf("<")+1, temp[i].indexOf(">"));	
				} else {
					emailValue=emailValue+","+temp[i].substring(temp[i].indexOf("<")+1, temp[i].indexOf(">"));
				}
			}
		}
		document.frends_invite_form.recipientMails.value=emailValue;
		document.frends_invite_form.action="/d/sweepstakes/sweep_stakes_invite_friends.jsp?action=submit";
			document.frends_invite_form.method ="POST";
			document.frends_invite_form.submit();
	}
}


function sendEmail(){
	document.friends_send_email_form.action="/d/sweepstakes/sweep_stakes_send_email.jsp?action=submit";
	document.friends_send_email_form.method ="POST";
	document.friends_send_email_form.submit();
	
}


function exit(){
	document.location.href="/d/sweepstakes/sweep_stakes_login.jsp";
}