var ajax = new sack();


function ping(){
	alert("hello world!");
}

function getModelList(sel){
	var makeid = sel.options[sel.selectedIndex].value;
	document.getElementById('model').options.length = 0;
	document.getElementById('derivitive').options.length = 0;
	if(makeid.length>0){
		ajax.requestFile = '../gen/getmodels.php?makeid='+makeid;
		ajax.onCompletion = popModelList;
		ajax.runAJAX();
	}
}

function getDerivList(sel){
	var modelid = sel.options[sel.selectedIndex].value;
	document.getElementById('derivitive').options.length = 0;
	if(modelid.length>0){
		ajax.requestFile = '../gen/getderivitives.php?modelid='+modelid;
		ajax.onCompletion = popDerivList;
		ajax.runAJAX();
	}
}

function popModelList(){
	var obj = document.getElementById('model');
	var derivobj = document.getElementById('derivitive');
	obj.options[obj.options.length] = new Option('Select model...','');
	derivobj.options[derivobj.options.length] = new Option('Select derivitive...','');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function popDerivList(){
	var obj = document.getElementById('derivitive');
	obj.options[obj.options.length] = new Option('Select derivitive...','');
	eval(ajax.response);	// Executing the response from Ajax as Javascript code	
}

function enableCompanyField(fm,Broker){
	if (Broker === undefined ) {
		Broker = "";
	}

	if (fm.type.value == "0"){
		fm.busname.disabled=false;
	}else{
		fm.busname.value="";
		fm.busname.disabled=true;
	}
	var obj = fm.quotetype;
	obj.length = 0;
	if (fm.type.value == "0"){
		obj.options[obj.options.length] = new Option('Business Contract Hire','2');
		obj.options[obj.options.length] = new Option('Contract Purchase','3');
	}else{
		obj.options[obj.options.length] = new Option('Personal Contract Hire','1');
	}
	obj.options[obj.options.length] = new Option('HP','4');
	if (fm.type.value == "0"){
		obj.options[obj.options.length] = new Option('Finance Lease','6');
	}else{
		obj.options[obj.options.length] = new Option('Personal Contract Purchase','5');
	}
	//obj.options[obj.options.length] = new Option('Operating Lease','7');
	if (Broker != "FINDRI"){
		obj.options[obj.options.length] = new Option('Lease Purchase','8');
	}
	obj.options[obj.options.length] = new Option('Outright Purchase','9');
}

function validateYear(str) {
	if (str.length != 4){
		return 0;
	}
	if ("" + parseInt(str) != str){
		return 0;
	}
	return 1;
}


function clearField(field) {
	if (field.value == field.defaultValue) {
		field.value = "";
	}
}


function emailCheck (emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];

	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}
	}

	if (user.match(userPat)==null) {
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
		}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		return false;
	}

	if (len<2) {
		return false;
	}

	return true;
}

function jumptohref(loc){
	window.location.href=loc.value;
}

function SubmitCallback(fm,chk){ /* used for call back req & franchise req */
	if ( chk === undefined ) {
		chk = "";
	}else{
		chk = chk.substring(9,14);
	}
	if (fm.name.value == ""){
		alert("You must enter your name");
		fm.name.focus();
	}
	else if (fm.tel.value == ""){
		alert("You must enter a telephone number");
		fm.tel.focus();
	}
	else{
		submit = "y";
		if (chk != "")	{
			if(fm.vld2.value != chk){
				submit = "n";
				alert("The validation code has not been entered correctly.");
				fm.vld.focus();
			}
		}
		if (submit === "y"){
			fm.submit();
		}
		
	}
}

function SubmitSendtoafriend(fm,chk){ /* used for call back req & franchise req */
	if ( chk === undefined ) {
		chk = "";
	}else{
		chk = chk.substring(9,14);
	}
	if (!emailCheck(fm.emailfrom.value)){
		alert("Your email address is not valid.");
		fm.email.focus();

	}else if (!emailCheck(fm.emailto.value)){
		alert("Your friend's email address is not valid.");
		fm.email.focus();
	}
	else{
		submit = "y";
		if (chk != "")	{
			if(fm.vld.value != chk){
				submit = "n";
				alert("The validation code has not been entered correctly.");
				fm.vld.focus();
			}
		}
		if (submit === "y"){
			fm.submit();
		}
		
	}
}

function SubmitTestimonial(fm){ 
	if (fm.name.value == ""){
		alert("You must enter your name");
		fm.name.focus();
	}
	else if (!emailCheck(fm.email.value)){
		alert("The email address is not valid.");
		fm.email.focus();
	}
	else if (fm.comments.value == ""){
		alert("You must enter some comments");
		fm.comments.focus();
	}
	else{
		fm.submit();
	}
}

function submitquote(fm,chk,bkr){

	if ( chk === undefined ) {
		chk = "";
	}else{
		chk = chk.substring(9,14);
	}

	if ( bkr === undefined ) {
		bkr = "";
	}
	if (bkr==="PRT" && fm.ifa_code.value == "" && fm.ifa_name.value == "" && fm.ifa_comp.value == "" && fm.ifa_town.value == ""){
		alert("Please enter IFA details");
		fm.ifa_code.focus();
	}
	else if (fm.quoteformtype.value=="rentals" && fm.rentalperiod.value==""){
		 alert("Please select rental period.");
		 fm.rentalperiod.focus();
	}
	else if (fm.make.value == ""){
		alert("Please select a make");
		fm.make.focus();
	}
	else if (fm.model.value == ""){
		alert("Please select a model");
		fm.model.focus();
	}
	else if (fm.conlen.value == ""){
		alert("Please select a contract period");
		fm.conlen.focus();
	}
	else if (fm.milage.value == ""){
		alert("Please select a mileage");
		fm.milage.focus();
	}
	else if (fm.maint.value == ""){
		alert("Please pick your maintenance requirements.");
		fm.maint.focus();
	}
	else if (fm.quoteformtype.value=="rentals" && checkradio(fm.collection_address)==null){
		 alert("Please select your pickup location.");
		 fm.rentalperiod.focus();
	}
	else if (fm.type.value == ""){
		alert("Please pick a type of quote.");
		fm.type.focus();
	}
	else if (fm.type.value == "0" && fm.busname.value == ""){
		alert("Please enter a business name.");
		fm.type.focus();
	}
	else if ((fm.firstname.value + fm.surname.value) == ""){
		alert("Please enter a name");
		fm.surname.focus();
	}
	else if ((fm.tel.value + fm.mobile.value) == ""){
		alert("Please enter a telephone number");
		fm.tel.focus();
	}
	else if (!emailCheck(fm.email.value)){
		alert("The email address is not valid.");
		fm.email.focus();
	}

	else{
		submit = "y";
		if (chk != "")	{
			if(fm.vld.value != chk){
				submit = "n";
				alert("The validation code has not been entered correctly.");
				fm.vld.focus();
			}
		}
		if (submit === "y"){
			fm.submit();
		}
		
	}
}

function showhidebusname(fm){
	if (fm.type.value == "0"){
		fm.busname.disabled=false;
	}else{
		fm.busname.value="";
		fm.busname.disabled=true;
	}
}

function selectrentalperiod(fm){
	if (fm.rentalperiod.value.substring(0,3)=="drp"){
		document.getElementById('showinsuranceselect').style.display="block";
		document.getElementById('hideinsuranceselect').style.display="none";
	}else{	
		document.getElementById('showinsuranceselect').style.display="none";
		document.getElementById('hideinsuranceselect').style.display="block";
	}
}