var ajax = new sack();

function ping(){
	alert("hello");
}

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(FormRef,Broker){
	if (Broker === undefined ) {
		Broker = "";
	}

	if (FormRef.type.value == "0"){
		FormRef.busname.disabled=false;
	}else{
		FormRef.busname.value="";
		FormRef.busname.disabled=true;
	}
	var obj = FormRef.quotetype;
	obj.length = 0;
	if (FormRef.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 (FormRef.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(FormRef,chk){ /* used for call back req & franchise req */
	if ( chk === undefined ) {
		chk = "";
	}else{
		chk = chk.substring(9,14);
	}
	if (FormRef.name.value == ""){
		alert("You must enter your name");
		FormRef.name.focus();
	}
	else if (!validatetelnumber(FormRef.tel.value)){
		alert("You must enter a valid telephone number");
		FormRef.tel.focus();
	}
	else{
		submit = "y";
		if (chk != "")	{
			if(FormRef.vld2.value != chk){
				submit = "n";
				alert("The validation code has not been entered correctly.");
				FormRef.vld.focus();
			}
		}
		if (submit === "y"){
			FormRef.submit();
		}
		
	}
}

function SubmitSendtoafriend(FormRef,chk){ /* used for call back req & franchise req */
	if ( chk === undefined ) {
		chk = "";
	}else{
		chk = chk.substring(9,14);
	}
	if (!emailCheck(FormRef.emailfrom.value)){
		alert("Your email address is not valid.");
		FormRef.email.focus();

	}else if (!emailCheck(FormRef.emailto.value)){
		alert("Your friend's email address is not valid.");
		FormRef.email.focus();
	}
	else{
		submit = "y";
		if (chk != "")	{
			if(FormRef.vld.value != chk){
				submit = "n";
				alert("The validation code has not been entered correctly.");
				FormRef.vld.focus();
			}
		}
		if (submit === "y"){
			FormRef.submit();
		}
		
	}
}

function SubmitTestimonial(FormRef){ 
	if (FormRef.name.value == ""){
		alert("You must enter your name");
		FormRef.name.focus();
	}
	else if (!emailCheck(FormRef.email.value)){
		alert("The email address is not valid.");
		FormRef.email.focus();
	}
	else if (FormRef.comments.value == ""){
		alert("You must enter some comments");
		FormRef.comments.focus();
	}
	else{
		FormRef.submit();
	}
}

function submitquote(FormRef,chk){
	if ( chk === undefined ) {
		chk = "";
	}else{
		chk = chk.substring(9,14);
	}

	if (FormRef.enqsource===undefined){
		enqSource="999";
	}else{
		enqSource=FormRef.enqsource.value;
	}

	if (FormRef.make.value == ""){
		alert("Please select a make");
		FormRef.make.focus();
	}
	else if (FormRef.model.value == ""){
		alert("Please select a model");
		FormRef.model.focus();
	}
	else if (FormRef.conlen.value == ""){
		alert("Please select a contract period");
		FormRef.conlen.focus();
	}
	else if (FormRef.milage.value == ""){
		alert("Please select a mileage");
		FormRef.milage.focus();
	}
	else if (FormRef.maint.value == ""){
		alert("Please pick your maintenance requirements.");
		FormRef.maint.focus();
	}
	else if (FormRef.type.value == ""){
		alert("Please pick a type of quote.");
		FormRef.type.focus();
	}
	else if (FormRef.type.value == "0" && FormRef.busname.value == ""){
		alert("Please enter a business name.");
		FormRef.type.focus();
	}
	else if ((FormRef.firstname.value + FormRef.surname.value) == ""){
		alert("Please enter a name");
		FormRef.surname.focus();
	}
	else if (!validatetelnumber(FormRef.tel.value) && !validatetelnumber(FormRef.mobile.value)){
		alert("Please enter a valid telephone number");
		FormRef.tel.focus();
	}
	else if (!emailCheck(FormRef.email.value)){
		alert("The email address is not valid.");
		FormRef.email.focus();
	}
	else if (enqSource == "" ){
		alert("Please select where you heard about us from.");
		FormRef.enqsource.focus();
	}
	else{
		submit = "y";
		if (chk != "")	{
			if(FormRef.vld.value != chk){
				submit = "n";
				alert("The validation code has not been entered correctly.");
				FormRef.vld.focus();
			}
		}
		if (submit === "y"){
			FormRef.submit();
		}
		
	}
}
function validatetelnumber(tel){
	while (tel.indexOf(" ") > -1){
		tel=tel.replace(" ","");
	}
	while (tel.substr(0,1)=="0"){
		tel=tel.substr(1);
	}
	if (parseInt(tel) != tel){
		return false;
	}
	if (tel.length < 9){
		return false;
	}
	return true;
}
function restricttonumber(fld, evt){
	// eg. onKeyPress='return restricttonumber(this, event)'
	var key;
	var keychar;

	if (window.event){
		key = window.event.keyCode;
	}else if (evt){
		key = evt.which;
	}else{
		return true;
	}
	keychar = String.fromCharCode(key);

	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ){ // control keys
		return true;
	}else if ((("0123456789 ").indexOf(keychar) > -1)){ // numbers
		return true;
	}else{
		return false;
	}
}

