//Omar Gonzalez 01242005 - Custom check form validation tools

// Include cookie set of functions
document.write('<scr' + 'ipt src="http://especiales.univision.com/contentuvn/jscript/cookiesSetGetDelete.js"></scr' + 'ipt>');

// Include Age Validation Form
document.write('<form name="invalid" method="POST" action="http://survey.univision.com/ViewsFlash/servlet/viewsflash" target="inva" style="padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;">');
document.write('<input type="hidden" name="POLL_ID" value="">');
document.write('<input type="hidden" name="NGUSERID" value="">');
document.write('<input type="hidden" name="USERNAME" value="">');
document.write('<input type="hidden" name="MWCUID" value="">');
document.write('<input type="hidden" name="IP_AGENT" value="">');
document.write('<input type="hidden" name="cmd" value="tally">');
document.write('<input type="hidden" name="pollid" value="SP_TV_Contest!INVALID_AGE_DATA">');
document.write('<input type="hidden" name="results" value="?http://especiales.univision.com/contentuvn/jscript/ageValidationEmptyResult.html">');
document.write('</form>');

/********************************************************************
// checkList  -  pass (element name, form name)
// Validate to true if a list element is selected different than the 0 location
********************************************************************/
function checkList(element,form,message){
	var f = eval(document.forms[form]);
	var e = eval(f.elements[element]);
	if (e[e.selectedIndex].value == "" || e.selectedIndex == 0){
		if (message){
			alert(message);
			e.focus();
		}
		return false;
	}
	else return true;
}

/********************************************************************
// checkRadio  -  pass (element name, form name)
// Validate to true if a radio button is checked
********************************************************************/
function checkRadio(element,form,message){
	var f = eval(document.forms[form]);
	var checked = false;
	var semaphore = true;
	var focuselement;
	for (i=0;i<f.elements.length;i++){
		if (f.elements[i].type == "radio" && 
			f.elements[i].name == element){
			if (semaphore){
				focuselement = eval(f.elements[i]);
				semaphore = false;
			}
			if (f.elements[i].checked){
				checked = true;
			}
		}
	}
	if (!checked){
		if (message){
			alert(message);
			focuselement.focus();
		}
	}
	return checked;
}

function getRadioValue(element,form){
	var result = '';
	var f = eval(document.forms[form]);
	for (i=0;i<f.elements.length;i++){
		if (f.elements[i].name == element){
			if (f.elements[i].checked) result = f.elements[i].value;
		}
	}
	return result;
}

/********************************************************************
// checkCheckbox  -  pass (array of element names, form name, min of checkbox to be checked)
// Validate to true if min checkboxes are/is checked
********************************************************************/
function checkCheckbox(element,form,minReq,message){
	var f = eval(document.forms[form]);
	var count = 0;
	var focuselement;
	for (i=0;i<element.length;i++){
		if (f.elements[element[i]].checked){
		   count++;
		}
	}
	if (count < minReq){
		if (message){
			alert(message);
	   		f.elements[element[0]].focus();
		}
	   	return false;
	}
	else return true;
}

/********************************************************************
// checkMax  -  pass (element name, maximum number of checked, form name)
// In a list of checkboxes check not more than the max number specified
********************************************************************/
function checkMax(element,max,form,message){
	var f = eval(document.forms[form]);
	var counter = 0;
	for (i=0;i<f.elements.length;i++){
		if (f.elements[i].name == element.name && f.elements[i].checked)
			counter++;
	}
	if (counter > max){
		if (message){
			alert(message);
		}
		return false;
	}
}

/********************************************************************
// checkOpt  -  pass (element name, element value for optional, optional element name, form name)
// In a list of checkboxes if you have an option that requires to fill out an aditior text field
// it will validate true if any other checkbox is selected or if the actual value of the checkbox
// is the one for the optional field it will required the optional field not to be empty or null
********************************************************************/
function checkOpt(element,elementvalue,optelement,form,message){
	var f = eval(document.forms[form]);
	for (i=0;i<f.elements.length;i++){
		if(f.elements[i].name == element && f.elements[i].value == elementvalue){
			if (f.elements[i].checked && (f.elements[optelement].value == "" || f.elements[optelement].value == null)){
				if (message){
					alert(message);
					f.elements[optelement].focus();
				}
				return false;
			}
			else return true;
		}
	}
}

/********************************************************************
// checkEmail  -  (pass element name, form name)
// It will validate true if the string represent a valid email address
********************************************************************/
function checkEmail(element,form,message){
	var f = eval(document.forms[form]);
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(f.elements[element].value)){
		return true;
	}
	else{
		if (message){
			alert(message);
			f.elements[element].focus();
		}
		return false;
	}
}

/********************************************************************
// checkEmpty  -  (pass element name, form name)
// It will validate true if the string is not empty or equal to null
********************************************************************/
function checkEmpty(element,form,message){
	var f = eval(document.forms[form]);
	if (f.elements[element].value == '' || f.elements[element].value == null){
		if (message){
			alert(message);
			f.elements[element].focus();
		}
		return false;
	}
	else
		return true;
}

/********************************************************************
// checkZipCode  -  (pass element name, form name, number of minimum digits required)
// It will validate true if the string is all digits with the minimum amount of digits
// and not all zeros
********************************************************************/
function checkZipCode(element,form,digits,message){
	var f = eval(document.forms[form]);
	var myReg = new RegExp("\\d{" + digits + "}");
	var myReg2 = new RegExp("0{" + digits + "}");
	if (!f.elements[element].value.match(myReg) || f.elements[element].value.match(myReg2)){
		if (message){
			alert(message);
			f.elements[element].focus();
		}
		return false;
	}
	else
		return true;
}

/********************************************************************
// checkNumber  -  (pass element name, form name, number of minimum digits required)
// It will validate true if the string is all digits with the minimum amount of digits
********************************************************************/
function checkNumber(element,form,digits,message){
	var f = eval(document.forms[form]);
	var myReg = new RegExp("\\d{" + digits + "}");
	if (!f.elements[element].value.match(myReg)){
		if (message){
			alert(message);
			f.elements[element].focus();
		}
		return false;
	}
	else
		return true;
}

/********************************************************************
// checkName  -  (pass element name, form name)
// check If a name field contains any numeric characters on it
********************************************************************/
function checkName(element,form,message){
	var f = eval(document.forms[form]);
	var myReg = new RegExp(".\\d+.");
	if (f.elements[element].value.match(myReg)){
		if (message){
			alert(message);
			f.elements[element].focus();
		}
		return false;
	}
	else
		return true;
}

/********************************************************************
	ageTest  -  parameters being passed (
					age limit integer, 
					contest starting month integer,
					contest starting date integer,
					contest starting year integer,
					alert message for invalid entry,
					user month entry value integer,
					user date entry value integer,
					user year entry value integer,
					main form name string
					)
	Description - check if a birth date entry is invalid
********************************************************************/
function ageTest(age,sM,sD,sY,message,uM,uD,uY,pollid){
	uM = parseInt(uM,10); uD = parseInt(uD,10); uY = parseInt(uY,10); age = parseInt(age,10);
	sM = parseInt(sM,10); sD = parseInt(sD,10); sY = parseInt(sY,10);
	var notValid = false; var cDate = new Date();
	var cM = cDate.getMonth() + 1; var cD = cDate.getDate();
	var cY = cDate.getYear(); if (cY < 1000) cY = cY + 1900;
	if (sM > 0) cM = sM;
	if (sD > 0) cD = sD;
	if (sY > 0) cY = sY;
	if ((cY-uY) < age) notValid = true;
	else if ((cY-uY) > age) notValid = false;
	else {
		if (cM > uM) notValid = false;
		else if (cM < uM) notValid = true;
		else {
			if (cD >= uD) notValid = false;
			else notValid = true;
		}
	}
	invalidAge = new String(GetCookie('invalidAge'));
	if ((invalidAge == "true") || notValid) {
		document.cookie = "invalidAge=true; path=/; domain=.univision.com;";
		alert(message);
		if (invalidAge != "true"){
			var f = document.invalid;
			f.POLL_ID.value = pollid;
			window.open('','inva','width=10,height=10,left=0,top=0,screenX=0,screenY=0,scrollbars=no,resizable=no');
			document.invalid.submit();
			window.focus();
		}
		return false;
	}
	else return true;
}

/********************************************************************
 	getBirthDate  -  parameters being passed (
					user month entry value integer,
					user date entry value integer,
					user year entry value integer
					)
	returns date on the following format: MMDDYYYY
********************************************************************/
function getBirthDate(month,day,year){
	var birthdate = "";
	var tmp2 = new String(month);
	month = (tmp2.length < 2) ? ("0" + month) : month;
	var tmp3 = new String(day);
	day = (tmp3.length < 2) ? ("0" + day) : day;
	birthdate = month + day + year;
	return birthdate;
}

/********************************************************************
 	changeDays  -  parameters being passed (
					string formName, 
					string monthElementFormName, 
					string dayElementFormName, 
					string yearElementFormName
					)
********************************************************************/
function changeDays(formName, monthElementFormName, dayElementFormName, yearElementFormName){
	var f = eval(document.forms[formName]);
	var thisindex;
	thisindex= -1;
	dobyear= f.elements[yearElementFormName][f.elements[yearElementFormName].selectedIndex].value;
	if (dobyear % 4 != 0){feb= 28;}
	else if (dobyear % 400 == 0){feb= 29;}
	else if (dobyear % 100 == 0){feb= 28;}
	else{feb= 29;}
	days= new Array(30, 31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	thismonth= f.elements[monthElementFormName].selectedIndex;
	thisday= f.elements[dayElementFormName].selectedIndex;
	f.elements[dayElementFormName].length= 1;
	for (i=1; i<=days[thismonth]; i++){
		f.elements[dayElementFormName].options[i]= new Option(i, i);
	}
	f.elements[dayElementFormName].selectedIndex= thisday;
}

/********************************************************************
 	getUserCookies  -  parameters being passed (
					string formName,
					string userIdFormElementName,
					string userNameFormElementName,
					string mwcuidFormElementName,
					string ipAgentFormElementName
					)
 	Returns date on the following format: MMDDYYYY
********************************************************************/
function getUserCookies(formName,userIdFormElementName,userNameFormElementName,mwcuidFormElementName,ipAgentFormElementName){
	var f = eval(document.forms[formName]);
	nguserid	= new String(GetCookie('NGUserID'));
	username	= new String(GetCookie('uv'));
	mwcuid		= new String(GetCookie('mwcuid'));
	useragent	= new String(navigator.userAgent);
	var hex1, hex2, hex3='';// encryption - encryption
	for (i= username.length - 3 ; i >= -1; i = i - 2){
		hex1 = username.charAt(i + 2);
		hex2 = username.charAt(i + 1);
		hex1 = parseInt("0x" + hex2 + hex1);
		hex2 = String.fromCharCode(hex1);
		hex3 = hex2 + hex3;
	}
	username = hex3;
	f.elements[userIdFormElementName].value		= (nguserid.length <= 50)? nguserid : nguserid.substring(0,49);
	f.elements[userIdFormElementName].value		= (f.elements[userIdFormElementName].value == "null") ? "" : f.elements[userIdFormElementName].value;
	f.elements[userNameFormElementName].value	= (username.length <= 50)? username : username.substring(0,49);
	f.elements[userNameFormElementName].value	= (f.elements[userNameFormElementName].value == "null") ? "" : f.elements[userNameFormElementName].value;
	f.elements[mwcuidFormElementName].value		= (mwcuid.length <= 50)? mwcuid : mwcuid.substring(0,49);
	f.elements[mwcuidFormElementName].value		= (f.elements[mwcuidFormElementName].value == "null") ? "" : f.elements[mwcuidFormElementName].value;
	f.elements[ipAgentFormElementName].value	= (useragent.length <= 200)? useragent : useragent.substring(0,199);
	f.elements[ipAgentFormElementName].value	= (f.elements[ipAgentFormElementName].value == "null") ? "" : f.elements[ipAgentFormElementName].value;
}

/********************************************************************
 	allowNoDups  -  parameters passed (
					string cookieName,
					string expMonth,
					string expDay,
					string expYear,
					string alertMessage,
					string redirectURL
					)
 	Returns boolean value
********************************************************************/
function allowNoDups(cookieName,expMonth,expDay,expYear,alertMessage,redirectURL,popupWidth,popupHeight){
	if ((popupWidth) && (popupHeight)){
		cwin = window.open('','vfRes','width='+popupWidth+',height='+popupHeight);
		cwin.focus();
	}
	expDate = new Date(expYear,expMonth+1,expDay).toGMTString();
	// if you come up with a cookie indicating this was voted on already
	var cookie_ls = GetCookie(cookieName);
	var instance = cookieName;// specialname for cookie
	if (cookie_ls != "true") {
		// write a cookie, put it in the jar for next time and mark it with a freshness date
		document.cookie = instance + "=true; path=/; domain=univision.com; expires=" + expDate + ";";
		return true;
	}else{
		window.alert(alertMessage);
		if (popupWidth && popupHeight && cwin){
			cwin.location = redirectURL;
		}
		else{
			window.location = redirectURL;
		}
		return false;
	}
}

/********************************************************************
 	gParam  -  parameters passed (
					string strParam
					)
 	Returns the value of the query string parameter requested
********************************************************************/
function gParam(strParam){
	var location = new String( window.location);
	var questionIndex = location.indexOf( "?" );
	var parameters = new Array();
	var parameterString = location.substring( questionIndex + 1 );
	var tempParam = new Array();
	parameters = parameterString.split( "&" );	
	for(i=0; i <  parameters.length; i++){
		tempParam = parameters[i].split( "=");
		if (tempParam[0] == strParam){
			if (tempParam[1] == null) return ""; 
			else return tempParam[1];
		}
	}
}