////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// cookie functions
	var ch1Cookie = document.cookie;
	////////////////////////////
	// general cookie getting function for "ch1Cookie"
	function getCookie(name)
	{	var index = ch1Cookie.indexOf(name + "=");
		if (index == -1) return null;
		index = ch1Cookie.indexOf("=", index) + 1;
		var endstr = ch1Cookie.indexOf(";", index);
		if (endstr == -1) endstr = ch1Cookie.length;
		return unescape(ch1Cookie.substring(index, endstr));
	}
	////////////////////////////
	// general cookie setting function
	function setCookie(name, value)
	{	// set today's date
		var todayDate = new Date();
		var expiry = new Date((todayDate.getTime() + 90 * 24 * 60 * 60 * 1000) * 6); // plus (28 days * 6 == 6 months)
		if (value != null && value != "")
			document.cookie = name + "=" + escape(value) + "; expires=" + expiry.toGMTString() + "; path=/" + "; domain=www.channelone.com";
		ch1Cookie = document.cookie;
	}
	////////////////////////////
	// get Alloy CHANNELONE cookie and return values
	function getUserCookie(toSplit, cookieName)
	{	var cookies = toSplit.split('&');
		var cookiePieces;
		var i = -1;
		while (cookies[++i])
		{	cookiePieces = cookies[i].split('=');
			if (cookiePieces[0].substring(0,1) == ' ')
				cookiePieces[0] = cookiePieces[0].substring(1, cookiePieces[0].length);
			if (cookiePieces[0] == cookieName)
				return ""+unescape(cookiePieces[1]);
		}
		return "";
	}
	////////////////////////////
	// set "CHANNELONE" cookie to UserInfoArray
	var UserInfoArray = getCookie("DanceFlickOff");
	////////////////////////////
	// returns any element of UserInfoArray passed through myVal
	function showDATA(myVal)
	{	if(UserInfoArray != null)	return getUserCookie(UserInfoArray, myVal);
		else	return "";
	}



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+escape (value)+expires+"; path=/";
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

// end cookie functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// validation functions


var errorcolor="#F3FD7F"; //this is the error hightlight color of fields
var normalcolor="#ffffff"  // this is the normal background of fields

function checkParentalConsentTerms() {
	if (document.termsForm.terms.checked) {
		window.location="/life/sports/parental_consent_form.pdf";
	} else { alert("You must agree to ChannelOne.com's terms.\n\nPlease check this box to agree."); }
}

function validateform(formname) {  
  	var alertMessage = "Please enter the following: \n\n";
	var isAlert = false;

    // The following makes sure a field is NOT EMPTY  (validateNotEmpty) Repeat As necessary


    // Makes sure the Date of dob Fields arent empty
    if(!validateNotEmpty (formname.dobMonth.value)){
    	alertMessage += "Month of birth \n";
    	formname.dobMonth.style.background=errorcolor;
    	isAlert = true;	
    }
    if(!validateNotEmpty (formname.dobDay.value)){
    	alertMessage += "Day of birth \n";
    	formname.dobDay.style.background=errorcolor;
    	isAlert = true;	
    }
    if(!validateNotEmpty (formname.dobYear.value)){
    	alertMessage += "Year of birth \n\n";
    	formname.dobYear.style.background=errorcolor;
    	isAlert = true;	
    }	


    if(!validateNotEmpty (formname.firstName.value)){
    	alertMessage += "First Name \n";
    	formname.firstName.style.background=errorcolor
    	isAlert = true;	
    }
    
     if(!validateNotEmpty (formname.lastName.value)){
    	alertMessage += "Last Name \n";
    	formname.lastName.style.background=errorcolor;
    	isAlert = true;	
    }
    
    // The following Validates Emails are in correct Syntax
    if(!validateNotEmpty (formname.email.value)){
    	alertMessage += "Email \n";
    	formname.email.style.background=errorcolor;
	   	isAlert = true;	
	}else{	
		if(!validateEmail(formname.email.value)){
	    	alertMessage += "Email: Improper syntax \n";
	    	formname.email.style.background=errorcolor;
		   	isAlert = true;	
	   }	
    }

 
    if(!validateNotEmpty (formname.address1.value)){
    	alertMessage += "Address \n";
    	formname.address1.style.background=errorcolor;
    	isAlert = true;	
    }
	
	if(!validateNotEmpty (formname.city.value)){
    	alertMessage += "City \n";
    	formname.city.style.background=errorcolor;
    	isAlert = true;	
    }
    
    if(!validateNotEmpty (formname.state.value)){
    	alertMessage += "State \n";
    	formname.state.style.background=errorcolor;
    	isAlert = true;	
    }

	
    if(!validateNotEmpty (formname.zip.value)){
    	alertMessage += "Zip \n";
    	formname.zip.style.background=errorcolor;
    	isAlert = true;	
    }

 
    if(!validateNotEmpty (formname.phone.value)){
    	alertMessage += "Phone \n";
    	formname.phone.style.background=errorcolor;
    	isAlert = true;	
    }


	
	if(isAlert) {
		alert(alertMessage);
		return false;
    } else { //  *** This takes the THREE DOB fields (MONTH,DAY,YEAR) and puts them into one field (DOB) for Coppa validation
	   	var dob = formname.dobMonth.value+ "/" + formname.dobDay.value +"/"+ formname.dobYear.value;
	   	formname.dob.value=dob;
//		removeCarriageReturn(document.ch1_form.optionalTextarea1);
//    	alert (dob);
		return true;	
    }

}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	removeCarriageReturn
//	Replace the cariage return with <br/> tag
////////////////////////////////////////////////////////////////////////////////////////////////////////////
function removeCarriageReturn(textarea) {
	//textarea is reference to that object, replaceWith is string that will replace the encoded return
	textarea.value = escape(textarea.value); //encode textarea string's carriage returns
	var replaceWith = "  ";
	
	for(i=0; i<textarea.value.length; i++){
	//loop through string, replacing carriage return encoding with HTML break tag
		if(textarea.value.indexOf("%0D%0A") > -1){
		//Windows encodes returns as \r\n hex
		textarea.value=textarea.value.replace("%0D%0A",replaceWith);
		}
		else if(textarea.value.indexOf("%0A") > -1){
		//Unix encodes returns as \n hex
		textarea.value=textarea.value.replace("%0A",replaceWith);
		}
		else if(textarea.value.indexOf("%0D") > -1){
		//Macintosh encodes returns as \r hex
		textarea.value=textarea.value.replace("%0D",replaceWith);
		}
	}
	textarea.value=unescape(textarea.value) //unescape all other encoded characters
	return null;
}


// end validation functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// entry form functions

function saveFormFields() {
	var myDOB = document.ch1_form.dobMonth.value +"/"+ document.ch1_form.dobDay.value +"/"+ document.ch1_form.dobYear.value;
	document.ch1_form.dob.value = myDOB;
	
	var cookieString;
	cookieString = "dob=" + myDOB;
	cookieString += "&firstName=" + document.ch1_form.firstName.value;
	cookieString += "&lastName=" + document.ch1_form.lastName.value;
	cookieString += "&email=" + document.ch1_form.email.value;
	cookieString += "&address1=" + document.ch1_form.address1.value;
	cookieString += "&city=" + document.ch1_form.city.value;
	cookieString += "&state=" + document.ch1_form.state.value;
	cookieString += "&zip=" + document.ch1_form.zip.value;
	cookieString += "&phone=" + document.ch1_form.phone.value;
	createCookie("DanceFlickOff",cookieString,0);
}


function validateStep1() {
//	alert("about to enter contest validation");
	saveFormFields();
	if (validateform(document.ch1_form)) {
		if (! IsUnderage(document.ch1_form, "13") && ! getCookie("AGEVERIFICATION") ) {
		    if(document.ch1_form.optionalCheckbox1.checked && document.ch1_form.optionalCheckbox2.checked && document.ch1_form.optionalCheckbox3.checked){
				saveFormFields();
				document.getElementById('step2').style.display = '';
				document.getElementById('step1').style.display = 'none'; 
	    	} else {
				alert("You must read and agree to all three terms to enter.\n\nPlease check the boxes to agree.");
//				refreshForm();
			}
		} else {
				alert("We're sorry, but you are ineligible for online upload."); 
				var cookieString ="isUnderAge=True";
				createCookie("AGEVERIFICATION",cookieString,"2");
				eraseCookie("DanceFlickOff");
//				alert(document.cookie);
//				window.location="/static/play_of_the_week/";
		}
	} else {
//		alert("validateform failed");
	}

}

function refreshForm() {
	history.go(0);
	document.ch1_form.firstName.value = showDATA("firstName");
	document.ch1_form.lastName.value = showDATA("lastName");
	document.ch1_form.email.value = showDATA("email");
	document.ch1_form.address1.value = showDATA("address1");
	document.ch1_form.city.value = showDATA("city");
	document.ch1_form.state.value = showDATA("state");
	document.ch1_form.zip.value = showDATA("zip");
	document.ch1_form.phone.value = showDATA("phone");
}

// end entry form functions
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

