/*	
	Document: JavaScript
	Developer: Shegun Konibire
	-------------------------------------	*/
	
function HidePageElements(){
	if(GUNZ.$('page3')){
		GUNZ.hide('collectionForm2');
		GUNZ.hide('collectionForm3');
		GUNZ.hide('submitControl');		
	}
}

function ShowPageElements(){
	if(GUNZ.$('page3')){
		// GUNZ.show('collectionSteps');
		GUNZ.show('step1to2');
		GUNZ.show('ckdiv');
	}
}

function ThatHeightThingy(){
	new ElementMaxHeight();	
}

function duplicateAdd(checkbox){
	if (checkbox.checked == true){		
		GUNZ.$('coTitle').value = GUNZ.$('uTitle').value;
		GUNZ.$('coFName').value = GUNZ.$('uFName').value;
		GUNZ.$('coLName').value = GUNZ.$('uLName').value;
		GUNZ.$('coCompany').value = GUNZ.$('uCompany').value;
		GUNZ.$('coAdd1').value = GUNZ.$('uAdd1').value;
		GUNZ.$('coAdd2').value = GUNZ.$('uAdd2').value;
		GUNZ.$('coAdd3').value = GUNZ.$('uAdd3').value;
		GUNZ.$('coCity').value = GUNZ.$('uCity').value;
		GUNZ.$('coPCode').value = GUNZ.$('uPCode').value;
		GUNZ.$('coEmail').value = GUNZ.$('uEmail').value;
		GUNZ.$('coPhone').value = GUNZ.$('uPhone').value;
		GUNZ.$('coFax').value = GUNZ.$('uFax').value;		
	}
	clearAllErrors();
	new ElementMaxHeight();
}

function PrepareBookingForm(){
	if(GUNZ.$('page3')){
		GUNZ.$('GoTo2').onclick = new Function('GoToStepTwo(this); return false;');
		GUNZ.$('GoTo3').onclick = new Function('GoToStepThree(this); return false;');		
		// GUNZ.$('pg1').onclick = new Function('Page("1"); return false;');
		// GUNZ.$('pg2').onclick = new Function('Page("2"); return false;');
		// GUNZ.$('pg3').onclick = new Function('Page("3"); return false;');
	}
}

function PrepareContactForm(){
	if(GUNZ.$('page4')){
		var aListOfHelperText = GUNZ.getElementsbyClass(GUNZ.$("contactForm"), "p", "helperTextTxt");
		for (var x=0; x<aListOfHelperText.length; x++){
			GUNZ.hide(aListOfHelperText[x]);
		}
		var aListOfHelperImgs = GUNZ.getElementsbyClass(GUNZ.$("contactForm"), "span", "helperTextImg");
		for (var x=0; x<aListOfHelperImgs.length; x++){
			aListOfHelperImgs[x].style.display = 'inline';
			//GUNZ.show(aListOfHelperImgs[x]);
		}
		
	}	
}

function Page(pageRef){
	// Do links
	var aListOfLinks = GUNZ.$('collectionSteps').getElementsByTagName('A');
	for (var x=0; x<aListOfLinks.length; x++){
		aListOfLinks[x].className = "";
	}
	aListOfLinks[pageRef-1].className = "currentStep";
	aListOfLinks[pageRef-1].blur();
	// Do screens
	var aListOfScreens = GUNZ.getElementsbyClass(GUNZ.$("bookingForm"), "div", "collectionForm");
	for (var x=0; x<aListOfScreens.length; x++){
		GUNZ.hide(aListOfScreens[x]);
	}	
	GUNZ.show(aListOfScreens[pageRef-1]);
	// Do buttons
	var aListOfButtons = GUNZ.getElementsbyClass(GUNZ.$("bookingForm"), "div", "submit");
	for (var x=0; x<aListOfButtons.length; x++){
		GUNZ.hide(aListOfButtons[x]);
	}	
	GUNZ.show(aListOfButtons[pageRef-1]);
}



function GoToStepTwo(buttonObj){
	clearAllErrors();
	var errList = 0;
	var aInputsToValidate = GUNZ.getElementsbyClass(GUNZ.$("collectionForm1"), "*", "validate");
	// loop through validation, and error reporting
	if(aInputsToValidate.length > 0){
		for(var x = 0; x < aInputsToValidate.length; x++){
			if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typeText ') != -1) {
				if (isEmpty(aInputsToValidate[x].value)) {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			} else if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typeEmail ') != -1) {
				if (GUNZ.eCheck(aInputsToValidate[x].value) == false) {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			} else if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typePhoneUK ') != -1) {
				// if (isNaN($(aItemsToValidate[x]).value) || isEmpty($(aItemsToValidate[x]).value)) {
				if (!checkUKTelephone(aInputsToValidate[x].value)) {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			} else if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typeSelect ') != -1) {
				if (aInputsToValidate[x].value == "null") {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			}
		}
	}
	// if there are errors stop form from submitting
	if (errList > 0){		
		if(GUNZ.$('errorNotice')){
			GUNZ.$('errorNotice').style.display = "block";
			//$('#errorNotice').show('slow');
		}		
		new ElementMaxHeight(); // special to this website - Links Courier
		buttonObj.blur();
	} else {	
		GUNZ.hide('collectionForm1');
		GUNZ.hide('step1to2');
		GUNZ.show('collectionForm2');
		GUNZ.show('step2to3');		
	}
}

function GoToStepThree(buttonObj){
	clearAllErrors();
	var errList = 0;
	var aInputsToValidate = GUNZ.getElementsbyClass(GUNZ.$("collectionForm2"), "*", "validate");
	// loop through validation, and error reporting
	if(aInputsToValidate.length > 0){
		for(var x = 0; x < aInputsToValidate.length; x++){
			if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typeText ') != -1) {
				if (isEmpty(aInputsToValidate[x].value)) {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			} else if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typeEmail ') != -1) {
				if (GUNZ.eCheck(aInputsToValidate[x].value) == false) {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			} else if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typePhoneUK ') != -1) {
				// if (isNaN($(aItemsToValidate[x]).value) || isEmpty($(aItemsToValidate[x]).value)) {
				if (!checkUKTelephone(aInputsToValidate[x].value)) {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			} else if (aInputsToValidate[x].className && (' ' + aInputsToValidate[x].className + ' ').indexOf(' typeSelect ') != -1) {
				if (aInputsToValidate[x].value == "null") {
					errList++;
					reportError(aInputsToValidate[x]);
				}
			}
		}
	}
	// if there are errors stop form from submitting
	if (errList > 0){		
		if(GUNZ.$('errorNotice')){
			GUNZ.$('errorNotice').style.display = "block";
			//$('#errorNotice').show('slow');
		}		
		new ElementMaxHeight(); // special to this website - Links Courier
		buttonObj.blur();
	} else {	
		GUNZ.hide('collectionForm2');
		GUNZ.hide('step2to3');
		GUNZ.show('collectionForm3');
		GUNZ.show('submitControl');		
	}
}

// Setting up form validation
function SetUpContactForm(){
	if(GUNZ.$("contactForm")){
		GUNZ.$("contactForm").onsubmit = new Function("return validateForm(this);");
	}	
}

// Setting up form validation
function SetUpBookingForm(){
	if(GUNZ.$("bookingForm")){
		GUNZ.$("bookingForm").onsubmit = new Function("return validateForm(this);");
	}	
}

// Attaches the named function to the 
// window load event.
addLoadEvent(HidePageElements);
addLoadEvent(ShowPageElements);
addLoadEvent(ThatHeightThingy);
addLoadEvent(PrepareBookingForm);
addLoadEvent(SetUpContactForm);
addLoadEvent(SetUpBookingForm);

// Window onload event
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}
						
