
function appear(elm) {
	var rules = document.getElementById(elm);
	if (rules) {
		rules.style.position = 'absolute';
		rules.style.display = 'block';
	}
}

function disappear(elm) {
	var rules = document.getElementById(elm);
	if (rules) {
		rules.style.position = 'absolute';
		rules.style.display = 'none';
	}
}


function processEmailSignupGetPost() {
	var myajax = hdiAjax.ajaxObject;
	var myfiletype = hdiAjax.filetype;
	if (myajax.readyState == 4) {

		if (myajax.status == 200) {
			//document.getElementById('EmailSignupForm').submit();
		}
		else {
			alert('Email-Signup request failed.');
		}
	}
}

function handleEntryFormLoad() {
	throwSweepstakesConversionEventTag(1, 1);
	throwSweepstakesOptInConversionEventTag(1, 1);
}

function handleEntryFormSubmit() {
	try {

		if (!validateEntryForm()) {
			return false;
		}
		handleEntryEmailSignup();
		handleEntryPost();
		throwSweepstakesConversionEventTag(2, 10);
		throwSweepstakesRegistrationTag();
	}
	catch(e) {
		alert('There was a problem while processing this page.');
	}
	return false;
}

function validateEntryForm() {
	try {
		var errMsg = '';
		var nameRegex = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/;
		var emailRegex = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
		var addressRegex = /^([a-zA-Z0-9\.\,\-\s])+$/;
		var stateRegex = /^([A-Za-z]){2}$/;
		var zipRegex = /^([0-9]{5})|[0-9\-]{10}$/;
		var firstName = trimStr(document.getElementById('FirstNameText').value);
		var lastName = trimStr(document.getElementById('LastNameText').value);
		var address1 = trimStr(document.getElementById('Address1Text').value);
		var address2 = trimStr(document.getElementById('Address2Text').value);
		var city = trimStr(document.getElementById('CityText').value);
		var state = trimStr(document.getElementById('StateText').value);
		var zipCode = trimStr(document.getElementById('ZipCodeText').value);
		var emailAddress = trimStr(document.getElementById('EmailAddressText').value);
		var confirmEmail = trimStr(document.getElementById('ConfirmEmailText').value);
		var sourceElement = document.getElementById('SourceHidden');
		var source;
		if (document.getElementById('SourceRadio1').checked) 
			sourceElement.value = trimStr(document.getElementById('SourceRadio1').value);
		else if (document.getElementById('SourceRadio2').checked) 
			sourceElement.value = trimStr(document.getElementById('SourceRadio2').value);
		else if (document.getElementById('SourceRadio3').checked) 
			sourceElement.value = trimStr(document.getElementById('SourceRadio3').value);
		else if (document.getElementById('SourceRadio4').checked) 
			sourceElement.value = trimStr(document.getElementById('SourceTextArea').value);
		source = trimStr(sourceElement.value);
		if (firstName.length == 0)
			errMsg += 'First name field is required. \n\n';
		else if (nameRegex.test(firstName) != true)
			errMsg += 'First name field is invalid. \n\n';
		if (lastName.length == 0) 
			errMsg += 'Last name field is required. \n\n';
		else if (nameRegex.test(lastName) != true)
			errMsg += 'Last name field is invalid. \n\n';
		if ((address1.length > 0) || 
		   (address2.length > 0) || 
		   (city.length > 0) || 
		   (state.length > 0) || 
		   (zipCode.length > 0)) {
			if (addressRegex.test(address1) != true)
				errMsg += 'Address field is invalid. \n\n';
			if (address2.length > 0) {
				if (addressRegex.test(address2) != true)
					errMsg += 'Address 2 field is invalid. \n\n';
			}
			if (addressRegex.test(city) != true)
				errMsg += 'City field is invalid. \n\n';
			if (stateRegex.test(state) != true)
				errMsg += 'State field is invalid. \n\n';
			if (zipRegex.test(zipCode) != true)
				errMsg += 'Zip Code field is invalid. \n\n';
		}
		if (emailRegex.test(emailAddress) != true)
			errMsg += 'Email address field is invalid. \n\n';
		if (emailAddress != confirmEmail) 
			errMsg += 'Email address does not match Confirm email address. \n\n';
		if (errMsg.length != 0) {
			alert(errMsg);
			return false;
		}
	}
	catch(e) {	
		return false;
	}
	return true;
}

function handleEntryEmailSignup() {
	try {
		var bEmailSignup = (document.getElementById('EmailSignupCheckbox').checked);
		if (bEmailSignup == true) {

			var postUrl = getEmailSignupPostUrl();
			var postParameters = getEmailSignupPostParameters();
			hdiAjax.postAjaxRequest(postUrl, postParameters, processEmailSignupGetPost, 'txt');
			
			throwSweepstakesOptInConversionEventTag(2, 10);
		}
	}
	catch(e) {
		alert('There was a problem while processing email signup.');
	}
}

function getEmailSignupPostParameters() {
	try {
		var emailValue = document.getElementById("EmailAddressText").value;
		var firstNameValue = document.getElementById("FirstNameText").value;
		var lastNameValue = document.getElementById("LastNameText").value;
		var sourceValue = document.getElementById("EmailSignupSource").value;
		var ipAddressValue = document.getElementById("LocalIPAddress").value;
		var divCodeValue = document.getElementById("EmailDivisionCode").value;
		var dateTimeValue = createDateTimeStamp();
		var postStr = "EMAIL_ADDRESS=" + encodeURI(emailValue) + "&" +
						"FIRST_NAME=" + encodeURI(firstNameValue) + "&" +
						"LAST_NAME=" + encodeURI(lastNameValue) + "&" +
						"SOURCE=" + encodeURI(sourceValue) + "&" +
						"IP_ADDRESS=" + encodeURI(ipAddressValue) + "&" +
						"DIV_CODE=" + encodeURI(divCodeValue) + "&" +
						"DATE_TIME=" + encodeURI(dateTimeValue);
		return postStr;
	}
	catch(e) {
		return 'Error building email signup post parameters.';
	}
}

function handleSourceClick() {
	var elmSourceHidden = document.getElementById('SourceHidden');
	var elmSourceText = document.getElementById('SourceTextArea');
	if (document.getElementById('SourceRadio1').checked) {
		elmSourceText.value = '';
		elmSourceText.style.visibility = 'hidden';
	}
	else if (document.getElementById('SourceRadio2').checked) {
		elmSourceText.value = '';
		elmSourceText.style.visibility = 'hidden';
	}
	else if (document.getElementById('SourceRadio3').checked) {
		elmSourceText.value = '';
		elmSourceText.style.visibility = 'hidden';
	}
	else if (document.getElementById('SourceRadio4').checked) {
		elmSourceText.value = '';
		elmSourceText.style.visibility = 'visible';
	}
}

function handleEntryPost() {

	var sUrl = '/silhouettes/Sweepstakes/EntryFormPost.asp';
	var sParameters = getEntryFormPostParameters();
	hdiAjax.postAjaxRequest(sUrl, sParameters, handleEntryPostResponse, 'txt');
}

function handleEntryPostResponse() {
	var myAjax = hdiAjax.ajaxObject;
	var myFileType = hdiAjax.filetype;
	if (myAjax.readyState == 4) {

		if (myAjax.status == 200) {
			document.location.href = '/Sweepstakes/Confirm.asp';
		}
		else {
			alert('Sweepstakes entry request failed.');
		}
	}
}

function getEntryFormPostParameters() {
	var firstNameValue = document.getElementById("FirstNameText").value;
	var lastNameValue = document.getElementById("LastNameText").value;
	var address1Value = document.getElementById("Address1Text").value;
	var address2Value = document.getElementById("Address2Text").value;
	var cityValue = document.getElementById("CityText").value;
	var stateValue = document.getElementById("StateText").value;
	var zipCodeValue = document.getElementById("ZipCodeText").value;
	var emailValue = document.getElementById("EmailAddressText").value;
	var confirmValue = document.getElementById("ConfirmEmailText").value;
	var sourceValue = document.getElementById("SourceHidden").value;

	var postStr = "FIRST_NAME=" + encodeURI(firstNameValue) + "&" +
					"LAST_NAME=" + encodeURI(lastNameValue) + "&" +
					"ADDRESS=" + encodeURI(address1Value) + "&" +
					"ADDRESS_2=" + encodeURI(address2Value) + "&" +
					"CITY=" + encodeURI(cityValue) + "&" +
					"STATE=" + encodeURI(stateValue) + "&" +
					"ZIP=" + encodeURI(zipCodeValue) + "&" +
					"EMAIL_ADDRESS=" + encodeURI(emailValue) + "&" +
					"SOURCE=" + encodeURI(sourceValue) + "&" +
					"TIMESTAMP_=" + encodeURI(createDateTimeStamp());
	return postStr;
}

function throwSweepstakesConversionEventTag(pAction, pPoints) {
	var paramEventID	= 'Sweepstakes';
	var paramEventCat	= 'Sweepstakes Signup';
	try {
		cmCreateConversionEventTag(paramEventID, pAction, paramEventCat , pPoints);
	}
	catch(e) {
		alert('An error occurred while throwing the coremetrics ConversionEvent tag.');
	}
}

function throwSweepstakesOptInConversionEventTag(pAction, pPoints) {
	var paramEventID	= 'Sweeps Opt In';
	var paramEventCat	= 'Email Signup';
	try {
		cmCreateConversionEventTag(paramEventID, pAction, paramEventCat , pPoints);
	}
	catch(e) {
		alert('An error occurred while throwing the coremetrics ConversionEvent tag.');
	}
}

function throwSweepstakesRegistrationTag() {
	try{
		var emailValue = document.getElementById("EmailAddressText").value;
		var sourceValue = document.getElementById("SourceHidden").value;
		
		cmCreateRegistrationTag(emailValue, emailValue, '','', '', 'Sweepstakes Signup', 'Y', '', '', '', '', sourceValue, '')
	}
	catch(e) {
		alert('An error occurred while throwing the coremetrics registration tag.');
	}	
}

function MM_showHideLayers() { //v9.0
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) 
		with (document) 
			if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
		if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
		obj.visibility=v; }
}
