/*******************************************************
Javascript for Sweepstakes
*******************************************************/
usaList = new Array(55)
usaList[0]="Select State"
usaList[1]="AA"
usaList[2]="AE"
usaList[3]="AK"
usaList[4]="AL"
usaList[5]="AP"
usaList[6]="AR"
usaList[7]="AZ"
usaList[8]="CA"
usaList[9]="CO"
usaList[10]="CT"
usaList[11]="DC"
usaList[12]="DE"
usaList[13]="FL"
usaList[14]="GA"
usaList[15]="HI"
usaList[16]="ID"
usaList[17]="IL"
usaList[18]="IN"
usaList[19]="IA"
usaList[20]="KS"
usaList[21]="KY"
usaList[22]="LA"
usaList[23]="MA"
usaList[24]="MD"
usaList[25]="ME"
usaList[26]="MI"
usaList[27]="MN"
usaList[28]="MS"
usaList[29]="MO"
usaList[30]="MT"
usaList[31]="NC"
usaList[32]="ND"
usaList[33]="NE"
usaList[34]="NH"
usaList[35]="NJ"
usaList[36]="NM"
usaList[37]="NV"
usaList[38]="NY"
usaList[39]="OH"
usaList[40]="OK"
usaList[41]="OR"
usaList[42]="PA"
usaList[43]="RI"
usaList[44]="SC"
usaList[45]="SD"
usaList[46]="TN"
usaList[47]="TX"
usaList[48]="UT"
usaList[49]="VA"
usaList[50]="VT"
usaList[51]="WA"
usaList[52]="WI"
usaList[53]="WV"
usaList[54]="WY"

monthList = new Array(13)
monthList[0] = "Month"
monthList[1] = "January"
monthList[2] = "February"
monthList[3] = "March"
monthList[4] = "April"
monthList[5] = "May" 
monthList[6] = "June"
monthList[7] = "July"
monthList[8] = "August"
monthList[9] = "September"
monthList[10] = "October"
monthList[11] = "November"
monthList[12] = "December"

var newDate = new Date();
var begYear = newDate.getYear()-90;
var endYear = newDate.getYear()-18;
iNum = endYear - begYear +1
yearList = new Array(iNum)
yearList[0] = "Year"

var j=1
for(var i=begYear; i <= endYear; i++)
{
	yearList[j] = i;
	j += 1
}

dayList = new Array(32)
dayList[0] = "Day"
for(var i=1; i <= 31; i++)
{
	dayList[i] = i;
}
function displaySelection(arr,aa)
	{
	  	for(var i=0; i < arr.length; i++)
	  	{
		if (arr[i] == aa)
		 {
		document.write ("<option selected value='"+i+"'>" + aa)
		 } else
			{document.write ("<option value='"+i+"'>" + arr[i])
			}
		} 
	  }
function displaySelection1(arr,aa)
	{
	  	for(var i=0; i < arr.length; i++)
	  	{
		if (arr[i] == aa)
		 {
		document.write ("<option selected value='"+ arr[i]+"'>" + aa)
		 } else
			{document.write ("<option value='"+ arr[i]+"'>" + arr[i])
			}
		} 
	  }
function displaySelectionMonth(arr,aa)
	{
		var compValue
	  	for(var i=0; i < arr.length; i++)
	  	{
	  	compValue = i + '';
		if (compValue == aa)
		 {
		document.write ("<option selected value='"+i+"'>" + arr[i])
		 } else
			{document.write ("<option value='"+i+"'>" + arr[i])
			}
		} 
	  }
/*
function displaySelection2(arr,aa)
	{
	  	for(var i=0; i < arr.length; i++)
	  	{
		if (arr[i] == aa)
		 {
		document.write ("<option selected value='"+i+"'>" + aa)
		 } else
			{document.write ("<option value='"+i+"'>" + arr[i])
			}
		} 
	  }
*/	  
function displayYear(arr,aa)
	{
	  	for(var i=0; i < arr.length; i++)
	  	{
		if (arr[i] == aa)
		 {
		document.write ("<option selected value='"+ arr[i]+"'>" + aa)
		 } else
			{document.write ("<option value='"+ arr[i]+"'>" + arr[i])
			}
		} 
	  }
function isValidValue(value, valueSet) 
{ 
	var ok = true; 
    for (i=0; i< value.length; i++) 
    { 
		theChar =value.charAt(i) 
		if (valueSet.indexOf(theChar,0) <=-1) 
		{	
			ok = false;
			break;
		} 
    } 
    return ok ;
}
function trimLeft(theString)
{
	var newString='';
	for ( var i=0; i<theString.length; i++)
	{
		if (theString.charAt(i) != " ")
			break;
	}
	for ( var j=i; j<theString.length; j++)	
		newString = newString + theString.charAt(j);
	return (newString);
}
function trimAll(theString)
{
	theString = trimLeft(theString);
	var newString='';
	for ( var i=theString.length-1; i>=0; i--)
	{
		if (theString.charAt(i) != " ")
			break;
	}
	for ( var j=0; j<=i; j++)	
		newString = newString + theString.charAt(j);
	return (newString);
}
function isValidInput()
{
	var retValue = true;
	if (!document.frmContest.accept.checked) {
		alert("You must accept the terms and conditions in order to participate in " + sTitle + ".")
		return false;
	}
/*
	if (retValue) {
		retValue = validSelection(document.frmContest.bdMonth)
		if (!retValue) alert("Please enter your birthdate.")
	}
	if (retValue) {
		retValue = validSelection(document.frmContest.bdDay)
		if (!retValue) alert("Please enter your birthdate.")
	}	
	if (retValue) {
		retValue = validSelection(document.frmContest.bdYear)
		if (!retValue) alert("Please enter your birthdate.")
	}
	if (retValue) {
		retValue = isDate(document.frmContest.bdDay.value,document.frmContest.bdMonth.value, document.frmContest.bdYear.value)
		if (!retValue) {
			alert("Please re-enter your birthdate.")
		}
	}
	iDate1 = Date.parse("01/17/1987");
	iDate2 = Date.parse(document.frmContest.bdMonth.value+"/"+document.frmContest.bdDay.value+"/"+document.frmContest.bdYear.value);
	if (iDate1 < iDate2) {
		alert("You must be 18 years old to register for the Snap-on PentaGrip 20/20 Sweepstakes.")
		retValue = false;
	}
*/	
	if (retValue) retValue = validField(document.frmContest.firstName,"First Name",1,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz -._'",-1,-1)
	if (retValue) retValue = validField(document.frmContest.lastName,"Last Name",1,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzfSOZsozYÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ-.' ",-1,-1)	
	if (retValue) retValue = validField(document.frmContest.address1,"Address1",1,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890# -._:,'",-1,-1)
	if (retValue) retValue = validField(document.frmContest.address2,"Address2",0,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890# -._:,'",-1,-1)
	if (retValue) retValue = validField(document.frmContest.city,"City",1,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz -._'",-1,-1)
	if (retValue) {
		retValue = validSelection(document.frmContest.state)
		if (!retValue) alert("Please choose your State.")
	}
	if (retValue) retValue = validField(document.frmContest.zip,"Zip",1,"0123456789-", 5, 10)
	if (retValue) retValue = validField(document.frmContest.phone1,"Phone",1,"0123456789",3,3)	
	if (retValue) retValue = validField(document.frmContest.phone2,"Phone",1,"0123456789",3,3)	
	if (retValue) retValue = validField(document.frmContest.phone3,"Phone",1,"0123456789",4,4)			
	var strEmail = trimAll(document.frmContest.email.value)
	if  (retValue && strEmail=="") {
		alert("The \"E-mail address\" field is a required field.");
		retValue = false;
	}
	var strEmail2 = trimAll(document.frmContest.email2.value)	
	if  (retValue && strEmail2=="") {
		alert("The \"Verify email address\" field is a required field.");
		retValue = false;
	}
	if (retValue)
		if  (strEmail.toUpperCase()!=strEmail2.toUpperCase()) {
			alert("The email addresses you entered do not match.");
			retValue = false;
		}
		else if (!isValidEmailAddress(document.frmContest.email)) {
			alert("Invalid email address.");
			retValue = false;
		}
	return (retValue);
}
function validSelection(theField) {
	if (theField.selectedIndex == 0) return false;
	return true;
}
function validBD(theField) {
	if (theField.selectedIndex == 0) {
		alert("Please enter your birthdate.")
		return false;
	}
	return true;
}
function validState(theField) {
	if (theField.selectedIndex == 0) {
		alert("Please enter your state.")
		return false;
	}
	return true;
}
function validField(theField, strFieldName, isRequired, validStrings, minLen, maxLen)
{
	var retValue = false
	
	var value = trimAll(theField.value)

	if (isRequired && value == "") 
		alert('The \"' + strFieldName + '\" is a required field.')
	else if (value!="" && !isValidValue(value, validStrings))
		alert('Please enter the valid value for the \"' + strFieldName + '\" field.')
		else if (minLen!=-1 && (value.length <minLen || value.length>maxLen)) {
			if (minLen==maxLen) 
				alert('Please enter the valid value for the \"' + strFieldName + '\" field.\nThe lenght of value should be '+minLen+'.')			
			else
				alert('Please enter the valid value (99999-9999) for the \"' + strFieldName + '\" field.\nThe lenght of value should be between '+minLen+' and '+maxLen+'.')
			}
		else retValue = true;
	
	if (!retValue) theField.focus();
	return (retValue)
}
function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function isDate (day,month,year) {

    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false
    var test = new Date(year,month,day);
    if ( (y2k(test.getYear()) == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate()) )
      {  
	      return true;
      }
    else
        return false
}
function isValidEmailAddress(theField) 
{
	
	var retValue = true;
	var value = theField.value;

	if (trimAll(value)=="")
	{
		return false;
	}	
	if (retValue && !isValidValue(value, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@_-."))
	{
		retValue = false;
	}
	if (trimAll(value.toUpperCase())=="NONE" ) return true;
	
	atPos =value.indexOf("@",1)
	// there must be one "@" symbol
	if (retValue && (atPos == -1)) retValue = false;
		
	// and only one "@" symbol
	if (retValue && (value.indexOf("@", atPos+1) != -1)) retValue = false;
		
	// and at least one "." after the "@"
	periodPos = value.indexOf(".",atPos)
	if(retValue && (periodPos == -1)) retValue = false;
	
	// and at least one character between "@" and "."
	if ( retValue && (atPos +2 > periodPos)) retValue = false;
	
	if ( retValue && (periodPos +3 > value.length)) retValue = false;
	
	return (retValue);
}
function goNext()
{
	if (isValidInput())
	{
		document.frmContest.action = "Sweepstakes.asp"  
		document.frmContest.submit();
	}
}
function winNew(strURL)
{
	var winNew = window.open(strURL,'','TOOLBAR=no,scrollbars=yes,resizable=1');
	if (!winNew.closed)
	{  
		winNew.focus();
	}
}	
