var specValidFields = new Array();
var alertform = 'Please check that all required fields are filled in the form.';

function testPostable(obj){
	if ((obj.id).match(/^ctl/)) return false;
	if (obj.type == "radio" || obj.type == "checkbox") {
		if (obj.checked) 
			return true;
		else
			return false;
	}
	if ((obj.type == "checkbox") && (obj.checked)) return true;
	if ((obj.value != "") && (obj.value != "null") && (obj.type != "button") && (obj.type != "submit") && (obj.type != "hidden")) return true;
	return false
}

function submitClick(currentForm){
	if (checkForm(currentForm)) {
		$("#hiddenField").val("")
		var info = "<table><th width='50%'></th><th width='50%'></th>";
		$('#content :input').each(function(){
				if (testPostable(this)){
					if (getLabel(this)!='regNumP1') {
					if (getLabel(this)!='regNumP2') {
				info += "<tr valign='top'>"
				
				info += "<td>" + getLabel(this) + "</td>"
				
				info += "<td>"
				info += getValue(this)
				info += "</td>"
				info += "</tr>"
				}
				}
			}
		});
		info += "<tr valign='top'>"
		info += "<td>IP: </td>"
		info += "<td>"
		info += $('#HiddenIp').val()
		info += "</td>"
		info += "</tr>"
		info += "</table>"
		$("#hiddenField").val(info);
		__doPostBack('sendMail','');
		theForm.__EVENTTARGET.value = "confirmation";	 
	}
	return false;
}

function printForm(currentForm){
    if (checkForm(currentForm)) {

        $("#hiddenField").val("")

		var info = "<table style='margin:10px 10px 10px 10px;' id='printlayout'>";
		$('#content :input').each(function(){
				if (testPostable(this)){
					if (getLabel(this)!='regNumP1') {
					if (getLabel(this)!='regNumP2') {
				info += "<tr valign='top'>"
				
				info += "<td>" + getLabel(this) + "</td>"
				
				info += "<td>"
				info += getValue(this)
				info += "</td>"
				info += "</tr>"
				}
				}
			}
		});
		info += "<tr valign='top'>"
		info += "<td>IP: </td>"
		info += "<td>"
		info += $('#HiddenIp').val()
		info += "</td>"
		info += "</tr>"
		info += "</table>"
		$("#hiddenField").val(info);
			
		
		PrintLayout = window.open("", "layout", 
        "width=500,height=350,scrollbars=yes,location=no,status=no,resizable=yes,top=200,left=200");        
        
    	PrintLayout.opener = self;
		
		PrintLayout.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Application</title><meta name="ROBOTS" content="NOIMAGEINDEX"><meta name="keywords" content=""><link rel="stylesheet" type="text/css" href="/css/styles.css" /></head>');
    	PrintLayout.document.write('<body>');
    	PrintLayout.document.write('<img src="/img/logo.gif"><br><div style="margin:10px 10px 10px 12px;"><h1>' + DonationsFunds + '</h1><br><h2>Application</h2></div>');
		PrintLayout.document.write(info);
		PrintLayout.document.write('<div style="margin:10px 10px 10px 12px;"><a href="http://www.molson.com/">http://www.molson.com/</a></div></body>');
		PrintLayout.document.write('<script>window.onload = function (evt) { window.focus();window.print();window.close(); } </script>');
		PrintLayout.document.write('</html>');
		PrintLayout.document.close();
		
	}
	return false;
}

function submitClickContactUs(currentForm)
{
	$("#hiddenField").val("")
	if(checkForm(currentForm))
	{
		var fields = new Array('First Name', 'Last Name', 'Street Number', 'Street Name', 'Suite/Apt Number', 'City', 'State', 'Postal Code', 'Country', 'Email', 
							   'Phone', 'Age', 'Consumption', 'Gender', 'Freshness Date Code', 'Product', 'Container', 'Package', 'Purchased', 'Comments', 'hiddenip');
		var names  = new Array('First Name', 'Last Name', 'Street No', 'Street Name', 'Suite Apt', 'City', 'State', 'Zip', 'Country', 'Email', 
							   'Phone', 'Age', 'Consumption', 'Gender', 'Pull Date', 'Product', 'Primary Pkg', 'Secondary Pkg', 'Store Type', 'Comments', 'User Ip Address');
		var info = '';
		
		for(i=0; i<fields.length; i++)
		{
			thisField = document.forms['mainform'].elements[fields[i]];

			fieldType = ''+thisField.type;
			
			if(fieldType == 'undefined')
			{
				thisFieldValue = getCheckedValue(thisField);
			}
			else
			{
				thisFieldValue = thisField.value;
			}
		info += names[i]+':'+thisFieldValue +'\n';
		}
		
		info += 'Submitted from Molson.com.';
		
		$("#hiddenField").val(info);
		__doPostBack('sendMail','');
		theForm.__EVENTTARGET.value = "confirmation";	 
	}else{alert(alertform);}
	return false;
}

function getCheckedValue(radioObj) {
	//alert(radioObj);
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function checkForm(divName){
	var bValid = true
	$( "#"+divName+" .required" ).each(function() {
		if($(this).valid() == 0){
			bValid = false;
		}
	});
	return bValid;
}
function getLabel(obj){
	if (obj.type == "radio" || obj.type == "checkbox"){
		if ($('#' + obj.name + "ID").text()){
			return $('#' + obj.name + "ID").text().replace(/\s?\*\s?/,"").replace(/\s?:\s?/,"")
		} else {
			return obj.name
		}
	} else {
		if ($('#' + obj.id + "ID").text()){
			return $('#' + obj.id + "ID").text().replace(/\s?\*\s?/,"").replace(/\s?:\s?/,"")
		} else {
			return obj.id
		}
	}
}
function getValue(obj){
	switch (obj.type){
		case "select-one":
			return obj.options[obj.selectedIndex].text	
			break;
		case "radio":
			return $('#' + obj.name + obj.value).text()
			break;
		case "checkbox":
			return $('#' + obj.name + obj.value).text()
			break;
		case "text":
			return obj.value
			break;
		case "textarea":
			return obj.value
			break;
		default:
			break;
	}
}

function pushError(errMsg)
{
	if(specValidFields.length > 0)
	{
		for(i=0;  i<specValidFields.length; i++)	
		{
			if(specValidFields[i] == errMsg)
			{
				return false;
			}
		} 
		specValidFields.push(errMsg);
	}
	else
	{
		specValidFields.push(errMsg);
	}
	//alert ('101) ' + specValidFields.length + ', MSG : ' + errMsg);
	//alert ('101) ' + specValidFields.length);
}

function removeError(errMsg)
{
	var arrError = new Array();
	arrError = errMsg.split(',');
	
	if(specValidFields.length > 0)
	{
		for(i=0; i<arrError.length; i++)
		{
			for(j=specValidFields.length; j>=0; j--)	
			{
				if(specValidFields[j] == arrError[i])	
				{
					specValidFields.splice(j, 1);
				}
			} 
		}
	}
	//alert ('105) ' + specValidFields.length + ', MSG : ' + errMsg);
}

function alertError(errSource, alertType)
{
	switch(alertType)
	{
		case 'alert':
			//alert(errSource + '\n\n' + errMsg);
			alert(errMsg);
		break;
		
		case 'confirm':
			//answer = confirm(errSource + '\n\n' + errMsg + '\n\n' + continuer);
			answer = confirm(errMsg + '\n\n' + continuer);
			if(!answer)
			{
				window.location = 'default.aspx';
			}
		break;
		}
	//alertError(document.getElementById('noneHealthy').childNodes[0].innerHTML, 'confirm' )
}
function fill_emailTo(prov)
{
	switch(prov)
	{
		case 'BC': //british-colombia
		case 'AB': //alberta
		case 'SK': //saskatchewan
		case 'MB': //manitoba
		case 'YT': //yukon
		case 'NT': //north-west territories
		case 'NU': //nunavut
			emailto = 'Sagree.moodley@molson.com';
		break;
		
		case 'ON': //ontario
			emailto = 'judy.mcclelland@molson.com';
		break;
		
		case 'QC': //quebec
			emailto = 'Monique.depatie@molson.com';
		break;
		
		case 'NL': //newfoundland
			emailto = 'patty.amirault@molson.com';
		break;
		
		case 'NB': //new-brunswick
		case 'NS': //nova-scotia
		case 'PE': //prince-edouard-island
			emailto = 'patty.amirault@molson.com';
		break;
		
		case '':
			emailto = '';
		break;
	}
	
	document.getElementById('emailTo').value = emailto;
}
