// Load jQuery
google.load("jquery",1.2);

google.setOnLoadCallback(function() {
	$.getJSON("accepted_upcs.js",function(data) {
		acceptedUPCs = data.acceptedUPCs;
	});
	if ($("#UPC_Input").val()=="") {
		$("#UPC_Input").val("Enter UPC Code");
		$("#UPC_Input").css("color","#666")
	}
});


   function omniTrack(pageName) { 
        s_pageName = pageName; 
        sendAnalyticsEvent(''); 
        } 
	

function validateUPC(formRef) {
	var enteredUPC = $.trim(formRef.inpUPC.value);

	if ($.inArray(enteredUPC,acceptedUPCs)<0) {
		alert("Sorry, that is not a valid UPC code. Please try again.");
		return false;
	}
}

function validateRegistration(formRef) {
	formTextFields = new Array(
		"inpownerName",
		"inpEmail",
		"inpAddress1",
		"inpCity",
		"inpZip"
	);
	formSelects = new Array(
		"inpState"
	);
	formCheckboxes = new Array(
		"Agree_Check"
	);
	formEmail = new Array(
		"inpEmail"
	);
	formFieldErrors = new Array();
	formError = "";
	alertMessage = "";

	// Validate text fields
	for(i=0;i<formTextFields.length;i++) {
		if (formError!="") { break; }
		field = eval("formRef."+formTextFields[i]);
		if ($.trim(field.value)=="") { formError = "required"; }
	}
	
	// Validate selects
	for(i=0;i<formSelects.length;i++) {
		if (formError!="") { break; }
		field = eval("formRef."+formSelects[i]);
		if (field.selectedIndex==0) { formError = "required"; }
	}
	
	// Validate checkboxes
	for(i=0;i<formCheckboxes.length;i++) {
		if (formError!="") { break; }
		field = eval("formRef."+formCheckboxes[i]);
		if (field.checked==false) { formError = "agreement"; }
	}
	
	  //Validate email confirmation
	  if (formError=="") {
		var  email1 = eval("formRef."+formEmail[0]+".value");
		var at="@"
		var dt="."
		
		if (email1.indexOf(at) == -1)
		{
		formError ="invalidEmail"
		}
		if (email1.indexOf(dt) == -1)
		{
		formError ="invalidEmail"
		}
		
		}

	
/*	// Validate age
	if (formError=="") {
		birthMonthIndex = formRef.inpBirthMonth.selectedIndex;
		birthMonth = Number(formRef.inpBirthMonth.options[birthMonthIndex].value)-1;
		birthDayIndex = formRef.inpBirthDay.selectedIndex;
		birthDay = Number(formRef.inpBirthDay.options[birthDayIndex].value);
		birthYearIndex = formRef.inpBirthYear.selectedIndex;
		birthYear = Number(formRef.inpBirthYear.options[birthYearIndex].value);
		
		today = new Date();
		userAge = new Date();
		userAge.setFullYear(birthYear+18,birthMonth,birthDay);
		if (userAge>today) { formError = "age"; }
	}*/
	
	
	
	// Find which error message to display
	switch(formError) {
		case "required":
		$("#Required_Message").fadeIn('fast').show();
		alertMessage = "Please fill up all mandatory fields.";
		break;
		case "agreement":
		alertMessage = "You must agree to the Official Rules to be eligible for the sweepstakes.";
		break;
		case "email confirm":
		alertMessage = "The email addresses you entered do not match. Please correct.";
		break;
		case "age":
		alertMessage = "Sorry, you must be at least 18 years old to enter.";
		break;
		case "invalidEmail":
		alertMessage = "Please provide valid email address.";
		break;
	}
	
	if (alertMessage!="") {
		alert(alertMessage);
		return false;
	}
}

function popupOfficialRules() {
	width=600;
	height=500;
	url = "officialrules.pdf";
	LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
	props = 'height='+height+',width='+width+',top='+TopPosition+',left='+LeftPosition+',scrollbars=3';
	win = window.open(url,"Friends_Rules",props)
}

function generateUPC() {
	var randomNumber=Math.floor(Math.random()*acceptedUPCs.length);
	var randomUPC = acceptedUPCs[randomNumber];
	$("#UPC_Input").val(randomUPC);
	$("#Generate_UPC").fadeOut('fast');
	$("#UPC_Input").css("color","#000")
}

function focusUPCInput(inputRef) {
	if (inputRef.value=="Enter UPC Code") {
		inputRef.value = "";
		inputRef.style.color = "#000";
	}
}