var newContactDate=new Array("","","","09","00");

function initForm(){
	document.TUWTCY.reset();
	document.TUWTCY.when_date.value = newContactDate.join("");
	
}

function changeval(type){
	if (type == "dt"){
		var thisDate = document.TUWTCY.date.value;
		var sept = thisDate.split(",");
		newContactDate[0] = sept[0];
		newContactDate[1] = addLeadingZero(sept[1]);
		newContactDate[2] = addLeadingZero(sept[2]);
		var thisDay = sept[3];
		//rebuild the hour drop down, Saturdays have different opening times
		buildOpeningTimes(thisDay);
		
		//reset the time now that it has been rebuilt
		newContactDate[3] = addLeadingZero(document.TUWTCY.hour.value);
		newContactDate[4] = document.TUWTCY.min.value;
	}
	if (type == "hr"){ newContactDate[3] = addLeadingZero(document.TUWTCY.hour.value); }
	if (type == "mi"){ newContactDate[4] = document.TUWTCY.min.value; }
	
	document.TUWTCY.when_date.value = newContactDate.join("");

}


function getTextDay(day){
	if (day == 1) { return("Monday");}
	if (day == 2) { return("Tuesday");}
	if (day == 3) { return("Wednesday");}
	if (day == 4) { return("Thursday");}
	if (day == 5) { return("Friday");}
	if (day == 6) { return("Saturday");}
}

function validate(thisForm){

	if (thisForm.udr_name.value == "") {
		alert("Please enter your name before submitting");
		thisForm.udr_name.focus();
		return false;
	}
	if (thisForm.email.value == "") {
		alert("Please enter your email adress before submitting");
		thisForm.email.focus();
		return false;
	}
	if (thisForm.telno.value == "") {
		alert("Please enter your phone number before submitting");
		thisForm.telno.focus();
		return false;
	}
	if (thisForm.memberid.value == "Please Select Product") {
		alert("Please choose which product area you are interested in before submitting");
		thisForm.memberid.focus();
		return false;
	}

	var modPhone = new Array(0)
	var telePhone = thisForm.telno.value;
	var splitTelePhone = telePhone.split("");
	
	for(i=0;i<splitTelePhone.length;i++){
		if(splitTelePhone[i]!=" "){
			modPhone.push(splitTelePhone[i])
		}
	}
	
	if(modPhone.length < 10 || modPhone.length > 11 ){
		alert("Please enter a valid phone number, e.g. 01422 123456");
		return false;
	}
	
	
	if(document.TUWTCY.callLater.checked==true) {
		if (newContactDate[0] == ""){
			alert("Please select a day to contacted from the dropdown");
			return false;
		}
	}
		
	if(document.TUWTCY.callLater.checked==true){
		var approve = confirm("You have selected to be contacted at " + newContactDate[3] + ":" + newContactDate[4] + " on " + getDayOutput()+ "\nPlease click OK to submit your request." );
		if (approve){return true;}
		else{return false;}
	}else{
		var approve = confirm("You have selected to be contacted now\nPlease click OK to submit your request.");
		if (approve){return true;}
		else{return false;}	
	}	
}

function addLeadingZero(todaysDate){
	if (todaysDate<10){
		return(0 + todaysDate);
	}else{
	return(todaysDate);
	}
}

function buildOpeningTimes(dayoftheweek) {
	
	if (dayoftheweek != 6) { 
		option0 = new Option("09 :",09)
		option1 = new Option("10 :",10)
		option2 = new Option("11 :",11)
		option3 = new Option("12 :",12)
		option4 = new Option("13 :",13)
		option5 = new Option("14 :",14)
		option6 = new Option("15 :",15)
		option7 = new Option("16 :",16)
		option8 = new Option("17 :",17)
		option9 = new Option("18 :",18)
		option10 = new Option("19 :",19)
		
		for (i=0; i<=10; i++)
		{
			document.TUWTCY.hour.options[i] = eval("option" + i);
		}	
		return;
	}
		if (dayoftheweek == 6) { 
			option0 = new Option("09 :",09)
			option1 = new Option("10 :",10)
			option2 = new Option("11 :",11)
			option3 = new Option("12 :",12)
			document.TUWTCY.hour.options[0] = option0
			document.TUWTCY.hour.options[1] = option1
			document.TUWTCY.hour.options[2] = option2
			document.TUWTCY.hour.options[3] = option3

			
			for (i=10; i>3; i--)
			{
				document.getElementById("hour").remove(i)
			}
			return;
	}
}

function getDayOutput(){
		
	var chosenDate = document.TUWTCY.date.value;
	var chosenDateSplit = chosenDate.split(",");
	var chosenDay = chosenDateSplit[3];
	return(getTextDay(chosenDay));
}

function changeCall(changeTime){
	if(changeTime == "now" && document.TUWTCY.callNow.checked==false){
		document.TUWTCY.when.value="D"
		document.TUWTCY.callLater.checked=true;
	}
	if(changeTime == "now" && document.TUWTCY.callNow.checked==true){
		document.TUWTCY.when.value="0"
		document.TUWTCY.callLater.checked=false;
	}
	if(changeTime == "later" && document.TUWTCY.callLater.checked==false){
		document.TUWTCY.when.value="0"
		document.TUWTCY.callNow.checked=true;
	}
	if(changeTime == "later" && document.TUWTCY.callLater.checked==true){
		document.TUWTCY.when.value="D"
		document.TUWTCY.callNow.checked=false;
	}	

}

