// Global Variables
var isAlert = false; 
var underage = false;
var expdate = new Date();
				expdate.setTime(expdate.getTime() +  (24 * 60 * 60 * 1000 * 3)); 


function checkNotEmpty( strValue ) {
/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
   var strTemp = strValue;
   strTemp = trimAllSpaceSpace(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}

//////////////////////////////////////////////////////////////////////////////////
function trimAllSpaceSpace( strValue ) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}



//////////////validate email//////////////
function checkEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
//var objRegExp  = /(^[a-z0-9]([a-z0-9_\.\'\-]*)@([a-z0-9_\.\'\-]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.\'\-]*)@([a-z_\.\'\-]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i;
var objRegExp =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  //check for valid email
  return objRegExp.test(strValue);
}


function regValidate(formname)
{

var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;
	
	if(  !checkNotEmpty( formname.firstName.value ) )
    {  
		alertMessage += "First Name\n";
        formname.firstName.style.background = errorcolor
        isAlert = true;
    } else { formname.firstName.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.lastName.value ) )
    {   
		alertMessage += "Last Name\n";
        formname.lastName.style.background = errorcolor
        isAlert = true;
    } else { formname.lastName.style.background = normalcolor;  }
	
	
	if(  !checkEmail( formname.email.value ) )
    {   
		alertMessage += "Email\n";
        formname.email.style.background = errorcolor
        isAlert = true;
    } else { formname.email.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.address1.value ) )
    {   
		alertMessage += "Address 1\n";
        formname.address1.style.background = errorcolor
        isAlert = true;
    } else { formname.address1.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.city.value ) )
    {   
		alertMessage += "City \n";
        formname.city.style.background = errorcolor
        isAlert = true;
    } else { formname.city.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.state.value ) )
    {   
		alertMessage += "State \n";
        formname.state.style.background = errorcolor
        isAlert = true;
    } else { formname.state.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.zip.value ) )
    {   
		alertMessage += "Zip \n";
        formname.zip.style.background = errorcolor
        isAlert = true;
    } else { formname.zip.style.background = normalcolor;  }
	
	
	if(  !checkNotEmpty( formname.optionalText1.value ) )
    {   
		alertMessage += "Password \n";
        formname.optionalText1.style.background = errorcolor
        isAlert = true;
    } else { formname.optionalText1.style.background = normalcolor;  }
	
	if( formname.dobMonth.value == "0" )
    {   
		alertMessage += "Month of birth\n";
        formname.dobMonth.style.background = errorcolor
        isAlert = true;
    } else { formname.dobMonth.style.background = normalcolor; }
	
	if( formname.dobDay.value == "0" )
    {   
		alertMessage += "Day of birth\n";
        formname.dobDay.style.background = errorcolor
        isAlert = true;
    } else { formname.dobDay.style.background = normalcolor; }
	
	if( formname.dobYear.value == "0" )
    {   
		alertMessage += "Day of Year\n";
        formname.dobYear.style.background = errorcolor
        isAlert = true;
    } else { formname.dobYear.style.background = normalcolor; }
	
	if(formname.dobMonth.value != "0" && formname.dobDay.value != "0" && formname.dobYear.value != "0")
	{
		formname.birthDate.value = formname.dobMonth.value + "/" + formname.dobDay.value +"/" + formname.dobYear.value;
		//alert(formname.birthDate.value);
	}
	
	
	if(  checkNotEmpty( formname.areacode.value ) &&  checkNotEmpty( formname.first3degits.value ) && checkNotEmpty( formname.last4degits.value ))
    {   
		formname.phone.value = formname.areacode.value + "-" + formname.first3degits.value + formname.last4degits.value ;
		
    } else { 
		/*alertMessage += "Phone \n";
		formname.areacode.style.background = errorcolor;  
		formname.first3degits.style.background = errorcolor;
		formname.last4degits.style.background = errorcolor
		isAlert = true;*/
		formname.phone.value ="";
		} 
	
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	
	 	out = false;
	 	formname.submit();
		
    }
	
	/*if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	
		if(IsUnderage(formname.dobMonth.value,formname.dobDay.value,formname.dobYear.value))
		{
		
		out = false;
		}
		else{
	 	out = false;
	 	formname.submit();
		}
    }*/
	
}
}





////////////////////////////////////////////////////////////////////////////////
////Clinique login validation////////////////////
function loginValidate(formname)
{

var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;
		
	if(  !checkEmail( formname.email.value ) )
    {   
		alertMessage += "Email\n";
        formname.email.style.background = errorcolor
        isAlert = true;
    } else { formname.email.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.optionalText1.value ) )
    {   
		alertMessage += "Password \n";
        formname.optionalText1.style.background = errorcolor
        isAlert = true;
    } else { formname.optionalText1.style.background = normalcolor;  }
	
	
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	//alert("else here");
	 	out = false;
	 	formname.submit();
		
    }
}
}
//////////////////////////////////////////////////////////////////////////////////



////Clinique image upload validation////////////////////
function uploadValidate(formname)
{

var out = true;
while (out) {  
	var alertMessage = "";
    var isAlert = false;
	
	
	
	
	if(  !checkNotEmpty( formname.beforeImages.value ) && !checkNotEmpty( formname.afterImages.value ))
    {   
		alertMessage += "Please upload your before or after photo. \n";
        formname.beforeImages.style.background = errorcolor;
		 formname.afterImages.style.background = errorcolor
        isAlert = true;
    } else { formname.beforeImages.style.background = normalcolor; formname.afterImages.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.afterImages.value ) && checkNotEmpty( formname.caption.value ))
    {   
		alertMessage += "Please upload your after photo. \n";
        formname.afterImages.style.background = errorcolor
        isAlert = true;
    } else { formname.afterImages.style.background = normalcolor;  }
	
	if(!formname.tags.checked)
	{   
		alertMessage += "Do you agree the terms? \n";
        formname.tags.style.background = errorcolor
        isAlert = true;
    } else { formname.tags.style.background = normalcolor;  }
	
	var b = formname.beforeImages.value;
	var temp = new Array();
	temp = b.lastIndexOf('.');
	var file_type = b.substring(temp);
	//alert(file_type);
	if(checkNotEmpty( formname.beforeImages.value ) )
	{
		if(file_type.toLowerCase()  != '.jpg' && file_type.toLowerCase()  != '.jpeg' && file_type.toLowerCase()  != '.png ' )
		{
		alertMessage += "Incorrect file type. you can only upload files ending in .jpg, .jpeg, and .png at this time. \n";
		formname.beforeImages.style.background = errorcolor
        isAlert = true;
		}
		else { formname.beforeImages.style.background = normalcolor;  }
    } 
	
	var b2 = formname.afterImages.value;
	var temp2 = new Array();
	temp2 = b2.lastIndexOf('.');
	var file_type2 = b2.substring(temp2);
	if(checkNotEmpty( formname.afterImages.value ) )
	{
		if(file_type2.toLowerCase()  != '.jpg' && file_type2.toLowerCase()  != '.jpeg' && file_type2.toLowerCase()  != '.png ' )
		{
		alertMessage += "Incorrect file type. you can only upload files ending in .jpg, .jpeg, and .png at this time. \n";
		formname.afterImages.style.background = errorcolor
        isAlert = true;
		}
		else { formname.afterImages.style.background = normalcolor;  }
    } 


	
	if(  !checkNotEmpty( formname.caption.value ) && checkNotEmpty( formname.afterImages.value ) )
    {   
		alertMessage += "Please upload your story \n";
        formname.caption.style.background = errorcolor
        isAlert = true;
    } else { 
		var num_charaters = formname.caption.value.length;
		if(num_charaters > 1500)
		{
			alertMessage += "The total number of characters including space in your essay is " + num_charaters+", The maximum characters allowed is 1500. \n";
        	formname.caption.style.background = errorcolor
        	isAlert = true;
		}
		else
		{
		formname.caption.style.background = normalcolor;  
		}
		
	}
	
	
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	//alert("else here");
	 	out = false;
	 	formname.submit();
		
    }
}
}




////Clinique sent to friend validation////////////////////
function friendValidate(formname)
{

var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;
	
	if(  !checkNotEmpty( formname.senderName.value ) || formname.senderName.value == "Your Name" )
    {  // alert("here2");
		alertMessage += "Name \n";
        formname.senderName.style.background = errorcolor
        isAlert = true;
    } else { formname.senderName.style.background = normalcolor;  }
	
	if(  !checkEmail( formname.senderEmail.value ) )
    {   //alert("here3");
		alertMessage += "Email\n";
        formname.senderEmail.style.background = errorcolor
        isAlert = true;
    } else { formname.senderEmail.style.background = normalcolor;  }
	
	
	 var elements = Form.getElements( formname );
	 var check =0;
	 var check2 = 0;
	  for( i = 0; i < elements.length; i++ )
    {  
		if( i == 7 )
		{
			if(elements[i].value == '' ||  elements[i].value == "Friend's Email")
			{
				check = check +1;
			}
			else if(!checkEmail(elements[i].value))
			{
				check2 = check2 +1;
			}
			
		}
		if( i == 9)
		{
			if(elements[i].value == '' ||  elements[i].value == "Friend's Email")
			{
				check = check +1;
			}
			else if(!checkEmail(elements[i].value))
			{
				check2 = check2 +1;
			}
		}
		if( i == 11)
		{
			if(elements[i].value == '' ||  elements[i].value == "Friend's Email")
			{
				check = check +1;
			}
			else if(!checkEmail(elements[i].value))
			{
				check2 = check2 +1;
			}
		}
		if( i == 13 )
		{
			if(elements[i].value == '' ||  elements[i].value == "Friend's Email")
			{
				check = check +1;
			}
			else if(!checkEmail(elements[i].value))
			{
				check2 = check2 +1;
			}
		}
		if( i == 15 )
		{
			if(elements[i].value == '' ||  elements[i].value == "Friend's Email")
			{
				check = check +1;
			}
			else if(!checkEmail(elements[i].value))
			{
				check2 = check2 +1;
			}
		}
	
	}
	
	//alert(check);
	//alert(check2);
	if(check ==5)
		{
			alertMessage += "Your friend's email\n";
			isAlert = true;
			
		}
	if(check2 >0)
		{
			alertMessage += "Your friend's valid email\n";
			isAlert = true;
			
		}
	
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	 	out = false;
	 	formname.submit();
		
    }
	
}
}


////Clinique password validation////////////////////
function passwordValidate(formname)
{

var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;
	
	if(  !checkEmail( formname.personEmail.value ) )
    {   
		alertMessage += "Email\n";
        formname.personEmail.style.background = errorcolor
        isAlert = true;
    } else { formname.personEmail.style.background = normalcolor;  }
	
	
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	//alert("else here");
	 	out = false;
	 	formname.submit();
		
    }
}
}
//////////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////////////////////
///////////////////Prom Night////////////////////////////////////////////////
function promRegValidate(formname)
{

var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;
	
	if(  !checkNotEmpty( formname.firstName.value ) )
    {  
		alertMessage += "First Name\n";
        formname.firstName.style.background = errorcolor
        isAlert = true;
    } else { formname.firstName.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.lastName.value ) )
    {   
		alertMessage += "Last Name\n";
        formname.lastName.style.background = errorcolor
        isAlert = true;
    } else { formname.lastName.style.background = normalcolor;  }
	
	
	if(  !checkEmail( formname.email.value ) )
    {   
		alertMessage += "Email\n";
        formname.email.style.background = errorcolor
        isAlert = true;
    } else { formname.email.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.address1.value ) )
    {  
		alertMessage += "Address 1\n";
        formname.address1.style.background = errorcolor
        isAlert = true;
    } else { formname.address1.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.city.value ) )
    {  
		alertMessage += "City \n";
        formname.city.style.background = errorcolor
        isAlert = true;
    } else { formname.city.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.state.value ) )
    {   
		alertMessage += "State \n";
        formname.state.style.background = errorcolor
        isAlert = true;
    } else { formname.state.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.zip.value ) )
    {  
		alertMessage += "Zip \n";
        formname.zip.style.background = errorcolor
        isAlert = true;
    } else { formname.zip.style.background = normalcolor;  }
	
	
	if( formname.dobMonth.value == "0" )
    {   
		alertMessage += "Month of birth\n";
        formname.dobMonth.style.background = errorcolor
        isAlert = true;
    } else { formname.dobMonth.style.background = normalcolor; }
	
	if( formname.dobDay.value == "0" )
    {   
		alertMessage += "Day of birth\n";
        formname.dobDay.style.background = errorcolor
        isAlert = true;
    } else { formname.dobDay.style.background = normalcolor; }
	
	if( formname.dobYear.value == "0" )
    {   
		alertMessage += "Day of Year\n";
        formname.dobYear.style.background = errorcolor
        isAlert = true;
    } else { formname.dobYear.style.background = normalcolor; }
	
	if(formname.dobMonth.value != "0" && formname.dobDay.value != "0" && formname.dobYear.value != "0")
	{
		formname.birthDate.value = formname.dobMonth.value + "/" + formname.dobDay.value +"/" + formname.dobYear.value;
		//alert(formname.birthDate.value);
	}
	
	if(!checkNotEmpty( formname.phone.value ) )
    {   
		alertMessage += "Phone\n";
        formname.phone.style.background = errorcolor
        isAlert = true;
    } else { formname.phone.style.background = normalcolor; }
	
	if( formname.gender.value == "0" )
    {   
		alertMessage += "Gender\n";
        formname.gender.style.background = errorcolor
        isAlert = true;
    } else { formname.gender.style.background = normalcolor; }
	
	if( !checkNotEmpty( formname.uploadEntryFile1.value ))
    {   
		alertMessage += "Please upload your  photo. \n";
        formname.uploadEntryFile1.style.background = errorcolor
        isAlert = true;
    } else { formname.uploadEntryFile1.style.background = normalcolor;  }
	
	var b2 = formname.uploadEntryFile1.value;
	var temp2 = new Array();
	temp2 = b2.lastIndexOf('.');
	var file_type2 = b2.substring(temp2);
	if(checkNotEmpty( formname.uploadEntryFile1.value ) )
	{
		if(file_type2.toLowerCase()  != '.jpg' && file_type2.toLowerCase()  != '.jpeg' && file_type2.toLowerCase()  != '.png ' )
		{
		alertMessage += "Incorrect file type. you can only upload files ending in .jpg, .jpeg, and .png at this time. \n";
		formname.uploadEntryFile1.style.background = errorcolor
        isAlert = true;
		}
		else { formname.uploadEntryFile1.style.background = normalcolor;  }
    } 

	
	/*if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	
	 	out = false;
	 	formname.submit();
		
    }*/
	
	
	
	
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	
		if(PromNightCheckIsUnderage(formname))
		{ 
		//alert("We're sorry, you do not qualify for this program.");
		out = false;
		
		}
		else{
		
	 	out = false;
	 	formname.submit();
		}
    }
	
}
}


function PromNightCheckIsUnderage(formname) {
 
  PromNightdate(); 
		
	//DOB CHECK
   
  var birth_year=formname.dobYear.value;
  var birth_month=formname.dobMonth.value;
  var birth_day=formname.dobDay.value;

  if (birth_month=="0" ) {
    alertMessage += "Month of birth\n";
	isAlert = true;
	 }
	    
  if (birth_day=="0" ) {
    alertMessage += "Day of birth\n";
	isAlert = true; 
	}

  if ((birth_month=="02" ) && (birth_day >"29" ))  {
    alertMessage += "Please select a day less than or equal to 29 for February.\n";
	isAlert = true;
	 }
  if (birth_year=="0" ) {
    alertMessage += "Year of birth\n";
	isAlert = true;
	 }

agealert = ""

// Checks Year for less than 13
if(((thisyear - 13) < birth_year) && birth_year != "0"){
  isAlert = false;
  underage = true;
  
}

else if(eval(birth_year) == eval(thisyear - 13))
// Checks for the month on the 13th year
{  
    if((birth_month != "0") && (eval(thismonth) < eval(birth_month)))
	{
	isAlert = false;
  	underage = true;	
    }
	// checks for the day on the month of the thirteenth year    
	else if (eval(birth_month) == eval(thismonth))
	{
        if((birth_day != "0") && (eval(thisday) < eval(birth_day)))
		{       
	    isAlert = false;
  	   underage = true;
	    }
	  	  
	    //else if (thisday == birth_day) alert("Happy Birthday!");
    }
}

if (isAlert){alert(alertMessage);}
else  
return PromNightcheckForPrevious(formname);      


//frm.submit(); 


}

// If the data is fine - this checks the cookie to make sure that the person hasnt already been in coppa violation
function PromNightcheckForPrevious(frm){
	if (PromNightGetCookie ("AgeVerification")== null ){
	//alert (underage);			
	PromNightSetCookie ("AgeVerification",underage,expdate);
	}
	if(PromNightGetCookie ("AgeVerification")!="false") {
	//alert (underage);
	//SetCookie ("AgeVerification",underage,expdate);
		alert("We're sorry, you do not qualify for this program. \n ")
		return true;
	}else{
		if (!underage) {
			
			frm.submit(); 
			return false;
		}
		else	
		 {
		 	PromNightSetCookie ("AgeVerification",underage,expdate);
			alert("We're sorry, you do not qualify for this program.");
			return true;
		}
	}
}

// Functions for Cookies

function PromNighttakeYear(theDate)
{
	x = theDate.getYear();
	var y = x % 100;
	y += (y < 38) ? 2000 : 1900;
	return y;
}


function PromNightdate() {
temp_date = new Date();
thisday = temp_date.getDate();
thismonth = (temp_date.getMonth()+1);
thisyear = PromNighttakeYear(temp_date);

/* if (!document.layers){
	thisyear = thisyear;
} else {
	thisyear = thisyear+=1900;
}*/
}

function PromNightSetCookie (name,value,expires) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "");
}
function PromNightGetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return PromNightgetCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}

function PromNightgetCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}

// An adaptation of Dorcht's function for deleting a cookie.
function PromNightdelCookie (name,path,domain) {
  if (PromNightGetCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}



