var sid = '399';
var subid = '';

function getVar (x) {
	var param = new Array();
	var query = window.location.search.substring(1).toLowerCase();
	var pairs = query.split('&');
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos > 0) param[pairs[i].substring(0,pos)] = pairs[i].substring(pos+1);
	}
	return (param[x] == null) ? '' : param[x];
}

function populateForm () {
	var fields = new Array();
	fields = ['yob','gender','country','zip','ethnicity','education','income','email'];
	for (var i=0; i<fields.length; i++) {
		value = '';
		value = getVar(fields[i]);
		if (value != ''){
			document.getElementById(fields[i]).value = unescape(value);
		}
	}
}

function hideOption(subIDnum) {
	var hideList = ['CD2168','CD2762','CD866','CD148','CD1046','CD541','CD593','CD810','CD1002','CD1250','CD4624']
	for (var i=0; i<hideList.length; i++) {
		if (subIDnum == hideList[i]) {
			hideDiv('panelCheckBox1');
			document.forms[0].panel.checked = false;
		}
	}
}

function initialize () {
	var newSID = getVar('sid');
	if ((parseInt(newSID) == newSID) && (parseInt(newSID) > 0)) sid = parseInt(newSID);
	document.forms[0].SID.value = sid;
	
	var newSubID = getVar('subid');
	if ((parseInt(newSubID) == newSubID) && (parseInt(newSubID) > 0)) subid = parseInt(newSubID);
	document.forms[0].SUBID.value = subid;

	document.forms[0].UID.value = getVar('uid');
	
	hideOption(newSubID);
	
	populateForm();
}

function validate (f) {
	var re = new RegExp('[^0-9]');
	if (f.gender.value.length < 1) {
		alert('Please select your gender.');
		return false;
	}
	if ((f.yob.value.length != 4) || re.test(f.yob.value)) {
		alert('Year of birth must contain exactly four digits only.');
		return false;
	}
	var currentDate = new Date();
	var respondentAge = currentDate.getFullYear() - parseInt(f.yob.value);
	if(respondentAge < 13 || respondentAge >= 99)
	{
	    alert('Please enter a valid age.\r\nIf you are under 13 years of age, please have a parent or legal guardian sign up on your behalf.');
	    return false;
	}
	if ((f.zip.value.length != 5) || re.test(f.zip.value)) {
		alert('Zip code must contain exactly five digits only.');
		return false;
	}
	if (f.country.value.length < 1) {
		alert('Please select your country.');
		return false;
	}
	var emailregex = "([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})";
	var validEmail = new RegExp(emailregex,'i');
	if ((f.email.value.length < 1) || (f.email.value == 'Email') || !validEmail.test(f.email.value)) {
		alert('Please enter a valid email address.');
		return false;
	}
	f.D.value = "";
	f.D.value += f.yob.value;
	f.D.value += f.gender.options[f.gender.selectedIndex].value;
	f.D.value += f.country.options[f.country.selectedIndex].value;
	f.D.value += f.zip.value;
	f.D.value += f.ethnicity.options[f.ethnicity.selectedIndex].value;
	f.D.value += f.education.options[f.education.selectedIndex].value;
	f.D.value += f.income.options[f.income.selectedIndex].value;
	if ((f.D.value.length != 15) || re.test(f.D.value)) {
		alert('An unknown error has occurred, please reload the page and try again.');
		return false;
	}
	f.D.value += '000000000';
	return true;
}

imgs.push('prizes/wii.jpg');
imgs.push('prizes/iphone.jpg');
imgs.push('prizes/sony.jpg');
imgs.push('prizes/sephora.jpg');

initRollovers();

function switchReward (x) {
	changeImages('rewardimage',imgs[x-1]);
	hideDivs(Array('hdr1.1','hdr1.2','hdr1.3','hdr1.4','txt1.1','txt1.2','txt1.3','txt1.4','txt2.1','txt2.2','txt2.3','txt2.4'));
	showDiv('hdr1.' + x);
	showDiv('txt1.' + x);
	showDiv('txt2.' + x);
}

function formonly () {
	hideDiv('layout2');
	hideDiv('layout3');
	showDiv('formonly');
	showDiv('formpad');
}

function layout () {
	hideDiv('formonly');
	hideDiv('formpad');
	showDiv('layout2');
	showDiv('layout3');
}