//global variables that can be used by ALL the function son this page.
var inputs;
var imgFalse = '/i/deals/google/false.png';
var imgTrue = '/i/deals/google/true.png';
var photocounter=0;
var amount=10;

//this function runs when the page is loaded, put all your other onload stuff in here too.
function init() {
    replaceChecks();
}

function replaceChecks() {
    
    //get all the input fields on the page
    inputs = document.getElementsByTagName('input');

    //cycle trough the input fields
    for(var i=0; i < inputs.length; i++) {

        //check if the input is a checkbox
        if(inputs[i].getAttribute('type') == 'checkbox') {
            
            //create a new image
            var img = document.createElement('img');
            
            //check if the checkbox is checked
            if(inputs[i].checked) {
               // img.src = imgTrue;
                img.src = imgFalse;			   
            } else {
                img.src = imgFalse;
            }

            //set image ID and onclick action
            img.id = 'checkImage'+i;
            //set image
            img.onclick = new Function('checkChange('+i+')');
            //place image in front of the checkbox
            inputs[i].parentNode.insertBefore(img, inputs[i]);
            
            //hide the checkbox
            inputs[i].style.display='none';
        }
    }
}

//change the checkbox status and the replacement image
function checkChange(i) {
    if(inputs[i].checked) {
        inputs[i].checked = '';
        document.getElementById('checkImage'+i).src=imgFalse;
		photocounter--;
    } else {
        inputs[i].checked = 'checked';
        document.getElementById('checkImage'+i).src=imgTrue;
		photocounter++;
    }
}

window.onload = init;

function validate(){
if (photocounter<amount || photocounter>amount)
	{
		alert("Please vote for only " + amount + " photographs.");
		return false;
	}

	if (trimSpaces(window.document.form1.First_Name.value)==""){
		window.alert ("Please enter your first name.");
		return false;
	} 
	
	if (trimSpaces(window.document.form1.Email.value)==""){
		window.alert ("Please enter an e-mail address.");
		return false;
	}

//this function will strip preceding and following 
//spaces
//
function trimSpaces(sDataToTrim)
{
	var sReturnDataF = '';
	var sReturnDataE = '';
	var bFrontDone = false;
	var bEndDone = false;

	//strip leading spaces
	//
	for(x=0;x<sDataToTrim.length;x++)
	{
		//if not a space, append to the return
		//value
		//
		if(sDataToTrim.charAt(x)!=' ')
		{
			sReturnDataF = sReturnDataF + sDataToTrim.charAt(x);
			bFrontDone = true;
		}
		else
		{
			if(bFrontDone)
				sReturnDataF = sReturnDataF + sDataToTrim.charAt(x);
		}	
	}

	//strip trailing spaces
	//
	for(x=sReturnDataF.length-1;x>=0;x--)
	{
		//if not a space, append to the return
		//value
		//
		if(sReturnDataF.charAt(x)!=' ')
		{
			sReturnDataE = sReturnDataF.charAt(x) + sReturnDataE;
			bEndDone = true;
		}
		else
		{
			if(bEndDone)
				sReturnDataE = sReturnDataF.charAt(x) + sReturnDataE;
		}	
	}
	return sReturnDataE;
}


//Email Validation - start
	var email = window.document.form1.Email.value

	var invalid = ' /:,;'

	for(var i=0; i<invalid.length; i++)
	{
		var badChar = invalid.charAt(i)

		if (email.indexOf(badChar,0) != -1)
		{
			alert( 'The e-mail address you entered contains one or more invalid characters.' )
			return false;
		}
	}

	var atSignPos = email.indexOf('@',1)

	if (atSignPos == -1)
	{
		alert( 'The e-mail address you entered is missing its @ sign.' )
		return false;
	}
	else if (email.indexOf('@',atSignPos+1) != -1)
	{
		alert( 'The e-mail address you entered contains too many @ signs.' )
		return false;
	}

	var dotPos = email.indexOf('.',atSignPos+2)

	if (dotPos == -1)
	{
		alert( 'The e-mail address you entered is missing its extension.' )
		return false;
	}
	else if (dotPos+3 > email.length)
	{
		alert( 'The e-mail address you entered appears to use an invalid extension.' )
		return false;
	}
	// Validation - end

	return true;
} 