

// Global Variables
// Global Variables
var errorcolor = "#FFD600"; //this is the error hightlight color of fields
var normalcolor = "#ffffff"  // this is the normal background of fields
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 sweepstakeValidate(name)
{

var form_name=document.command;

var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;
	
	
	
	if(form_name.maillingRequest.value == "1")
	{
		if(  !checkNotEmpty( form_name.address1.value ) )
    	{   
			alertMessage += "Address 1\n";
        	form_name.address1.style.background = errorcolor;
        	isAlert = true;
    	} else { form_name.address1.style.background = normalcolor;  }
	
		if(  !checkNotEmpty( form_name.city.value ) )
    	{   
			alertMessage += "City \n";
        	form_name.city.style.background = errorcolor;
        	isAlert = true;
    	} else { form_name.city.style.background = normalcolor;  }
	
		if(  !checkNotEmpty( form_name.zipcode.value ) )
    	{   
			alertMessage += "Zip \n";
        	form_name.zipcode.style.background = errorcolor;
        	isAlert = true;
    	} else { form_name.zipcode.style.background = normalcolor;  }
	}


	if(form_name.phoneRequest.value == "1")
	{
		if(  !checkNotEmpty( form_name.phoneNumber.value ) )
    	{    
			alertMessage += "Phone Number \n";
        	form_name.phoneNumber.style.background = errorcolor;
        	isAlert = true;
    	} else { form_name.phoneNumber.style.background = normalcolor;  }
	}

	if(!form_name.terms.checked)
	{   
		alertMessage += "Do you agree the terms? \n";
        form_name.terms.style.background = errorcolor;
        isAlert = true;
    } else { form_name.terms.style.background = normalcolor;  }
	
	
	if( isAlert )
    {   alert( alertMessage );
		out = false;
    }
    else { 
	
	 	out = false;
	 	form_name.submit();
		
    }
	
	
}
}



//////////////////////////////////////////////////////////////////////////////////
////admin validation//////////////
function basic_info_action(next){

		document.command.forwardPage.value=next
		//document.command.submit();
		
var formname=document.command;
var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;

	if(  !checkNotEmpty( formname.name.value ) )
    {   
		alertMessage += "sweepstake name\n";
        formname.name.style.background = errorcolor
        isAlert = true;
    } else { formname.name.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.description.value ) )
    {   
		alertMessage += "sweepstake description\n";
        formname.description.style.background = errorcolor
        isAlert = true;
    } else { formname.description.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.startDate.value ) )
    {   
		alertMessage += "sweepstake start date \n";
        formname.startDate.style.background = errorcolor
        isAlert = true;
    } else { formname.startDate.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.endDate.value ) )
    {   
		alertMessage += "sweepstake end date \n";
        formname.endDate.style.background = errorcolor
        isAlert = true;
    } else { formname.endDate.style.background = normalcolor;  }
	
	if( checkNotEmpty( formname.startDate.value ) )
    {   
		var mm = formname.startDate.value.substring(0,2);
		var dd = formname.startDate.value.substring(3,5);
		var yy = formname.startDate.value.substring(6);
		var invalid_start_date = false;
		if((!isNaN(mm) && mm.length == 2) && (!isNaN(dd) && dd.length == 2) && (!isNaN(yy) && yy.length == 4) )
		{
			invalid_start_date = false;
		}
		else{
			invalid_start_date = true;
		}
		if(invalid_start_date == true)
		{
			alertMessage += "incorrect start date format\n";
        	formname.startDate.style.background = errorcolor
        	isAlert = true;
		}
		else{ formname.startDate.style.background = normalcolor; }
    }
	
	if( checkNotEmpty( formname.endDate.value ) )
    {   
		var mm2 = formname.endDate.value.substring(0,2);
		var dd2 = formname.endDate.value.substring(3,5);
		var yy2 = formname.endDate.value.substring(6);
		var invalid_end_date = false;
		if((!isNaN(mm2) && mm2.length == 2) && (!isNaN(dd2) && dd2.length == 2) && (!isNaN(yy2) && yy2.length == 4) )
		{
			invalid_end_date = false;
		}
		else{
			invalid_end_date = true;
		}
		if(invalid_end_date == true)
		{
			alertMessage += "incorrect end date format\n";
        	formname.startDate.style.background = errorcolor
        	isAlert = true;
		}
		else{ formname.endDate.style.background = normalcolor; }
    }
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	
	 	out = false;
	 	formname.submit();
		
    }
	
}

}

////////////optin rules validation/////////////////////
function optin_rules_action(next){

		document.command.forwardPage.value=next
		//document.command.submit();
		var formname=document.command;
var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;

	if(  !checkNotEmpty( formname.minQualifyingAge.value ) )
    {   
		alertMessage += "min. qualifying age\n";
        formname.minQualifyingAge.style.background = errorcolor
        isAlert = true;
    } else { formname.minQualifyingAge.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.maxQualifyingAge.value ) )
    {   
		alertMessage += "max. qualifying age\n";
        formname.maxQualifyingAge.style.background = errorcolor
        isAlert = true;
    } else { formname.maxQualifyingAge.style.background = normalcolor;  }
	
	if(  !checkNotEmpty( formname.terms.value ) )
    {   
		alertMessage += "terms \n";
        formname.terms.style.background = errorcolor
        isAlert = true;
    } else { 
		var num_charaters = formname.terms.value.length;
		if(num_charaters > 15000)
		{
			alertMessage += "The total number of characters including space in terms is " + num_charaters+", The maximum characters allowed is 15000. \n";
        	formname.terms.style.background = errorcolor
        	isAlert = true;
		}
		else
		{
		formname.terms.style.background = normalcolor;  
		}
		
	}
	
	if(  !checkNotEmpty( formname.rules.value ) )
    {   
		alertMessage += "rules \n";
        formname.rules.style.background = errorcolor
        isAlert = true;
    } else { 
		var num_charaters = formname.rules.value.length;
		if(num_charaters > 15000)
		{
			alertMessage += "The total number of characters including space in rules is " + num_charaters+", The maximum characters allowed is 15000. \n";
        	formname.rules.style.background = errorcolor
        	isAlert = true;
		}
		else
		{
		formname.rules.style.background = normalcolor;  
		}
	}
	/*var checkoptin =0;
	alert(formname.optins.length);
	for( i = 0; i < document.command.optins.length; i++ )
    {alert(document.command.optins[i].value);
		if(document.command.optins[i].value != '' )
		{
			checkoptin=1;
		}
	}
	if(checkoptin == 0 || checkoptin == 0)
	{
		alertMessage += "at least one optin \n";
		formname.optins[0].style.background = normalcolor;
        isAlert = true;
	}*/
	
	
	/*if(isNaN(document.command.optins.length)){
		alertMessage += "at least one optin \n";
        isAlert = true;
	}*/
	
	

	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	
	 	out = false;
	 	formname.submit();
		
    }
	
}

}
	
//////////edit thank you/////////////

function edit_thankyou_action(next){

		document.command.forwardPage.value=next
		//document.command.submit();
		
var formname=document.command;
var out = true;
while (out) {  
	var alertMessage = "Please enter your: \n";
    var isAlert = false;

	if(  !checkNotEmpty( formname.thankyouCopy.value ) )
    {   
		alertMessage += "thankyou copy\n";
        formname.thankyouCopy.style.background = errorcolor
        isAlert = true;
    } else { formname.thankyouCopy.style.background = normalcolor;  }
	
	
	
	
	if( isAlert )
    {   alert( alertMessage );
	out = false;
    }
    else { 
	
	 	out = false;
	 	formname.submit();
		
    }
	
}

}


////////////publish////////////////////
function view_action(next){
		document.command.forwardPage.value=next
		document.command.submit();
}
function publish_action(status,serverHost){
		
		var ecardid = null;
		var sweepstakeid = document.command.sweepstakeId.value;

		if(document.command.addEcard.checked)
		{
			if(document.command.ecardId.value == '')
			{
				alert("Please enter E-card ID");
			}
			else{
				ecardid= document.command.ecardId.value;
				window.location = serverHost+"/sweepstake/publish_action/"+sweepstakeid+"/"+ecardid+"/"+status;
			}
		}
		else{
			window.location = serverHost+"/sweepstake/publish_action/"+sweepstakeid+"//"+status;
		}
		
		
}







