//******************************************************//
//              js - partselection.php		        //
//******************************************************//

function getFormNum (formName) {
	
	var formNum = -1;
	for (i=0;i<document.forms.length;i++) {
		tempForm = document.forms[i];
		if (formName == tempForm) {
			formNum = i;
			break;
		}
	}

	return formNum;
}

function msg(msg) {
	alert (msg);
}

function submission() {
	document.f2.submit();
}

v = false;
if (typeof(Option)+"" != "undefined") v=true;
if(v){a=new Array(22);}

function relate(formName,j,elementName,add){
	
	//alert(elementName);
	//alert(formName);
	//alert(j);
	
	j = j + add;
	
	//alert(j);
	
	//to resolve extra option in selects
	//j = j-1;
	
	//
	
	//alert(j);	
	
	if(v){
		var formNum = getFormNum(formName);
		
		//alert(formNum);
		//var elementName = "prtgrp";
		
		if (formNum>=0){
		
			//formNum++; // reference next form, assume it follows in HTML
			//alert("element: " + document.forms[formNum].elements[elementName]);
			
			with (document.forms[formNum].elements[elementName]){			
			
				for(i=options.length-1;i>0;i--)
					options[i] = null; // null out in reverse order (bug workarnd)
					
				for(i=0;i<a[j].length;i++){
				
					options[i] = new Option(a[j][i].text, a[j][i].value); 
					//options[i] = new Option(a[j][i].text); 
					//options[i].value = a[j][i].value;
				}
				
				options[0].selected = true;
			}
		}
	}else{
	
		jmp(formName,elNum);
	
	}
}



// The first parameter is a reference to the form.
function jmp(form, elt) {
	if (form != null) {
		with (form.elements[elt]) {
			if (0 <= selectedIndex)
				location = options[selectedIndex].value;
		}
	}
}

function getFormNum (formName) {
	var formNum = -1;
	for (i=0;i<document.forms.length;i++) {
		tempForm = document.forms[i];
		if (formName == tempForm) {
			formNum = i;
			break;
		}
	}
	return formNum;
}

var catsIndex = -1;
var itemsIndex;
var a;

// if (v) { // ns 2 fix
function newCat() {
	catsIndex++;
	a[catsIndex] = new Array();
	itemsIndex = 0;
}

function O(txt,modelcode) {
	a[catsIndex][itemsIndex]= new myOptions(txt,modelcode);
	itemsIndex++;
}

function myOptions(text,modcode) {
	this.text = text;
	this.value = modcode;
}

function more_info(description,sh_code,window_number,uniquen,Width,Height) {

	var popName = window_number;

	popURL="details_popup_frames.php?descript="+description+"&sh_code="+sh_code+"&uniquen="+uniquen;
	
	//alert(popURL);
	
	pop_up(popURL,popName,Width,Height);
                      
}

function pop_up(URL,popName,Width,Height) 

{

	if (Width =="" || Width == null) Width =486;

	if (Height == "" || Height == null) Height =500;



	var features =

	',directories=0'+

	',location=0'   +

	',menubar=0'   +

	',scrollbars=1'+

	',status=0'   +

	',toolbar=0'   +

	',resizable=1' +

	',width=' + Width +

	',height=' + Height +

	',screenX=15' +

	',screenY=15' +

	',top=15'+

	',left=15';

	URL = URL.replace(/\s/,'%20');

	wind=window.open (URL, popName, features);

	wind.focus();

}


//****************************************************************
//***********************FORM VALIDATION**************************
//****************************************************************


//FORM VALIDATION

var CCNre = /^\s*\d(\d|\s)*$/;//regular expression for the credit card number field

//var EMAILre=/^[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}$/;//regular expression for the email address field

var EMAILre  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;



function genericTest(anyRE,title,stringToTest) {

	if(anyRE.test(stringToTest)) {

		return true;

	} else {

		return false;

	}

}



//CHECK FIELDS FOR SPACES

function spaces(elm) {

	if (elm.value.indexOf(" ") != "-1"){

	return true;

	}else {

	return false;

	}

}

// CHECKS FOR BLANKS FIELDS

//check for blank fields

function isfilled(elm) {

	if (elm.value=="" ||elm.value==null){

	return false;

	}

	else{

	return true;

	}

}


function  checkPartSearchForm(form){

	var errorfields= "";//used to stored the error messages of the form
	
	// **DETAILS SECTION VALIDATION**
	
	
	if (form.manu.options[form.manu.selectedIndex].value=="def"){
	
	errorfields+="\n [+] Make not Selected";
	
	}
	
	if (form.model.options[form.model.selectedIndex].value=="def"){

	errorfields+="\n [+] Model not Selected";

	}
	
	if (form.year.options[form.year.selectedIndex].value=="def"){

	errorfields+="\n [+] Year not Selected";

	}
	
	if (form.group.options[form.group.selectedIndex].value=="def"){

	errorfields+="\n [+] Part Group not Selected";

	}
	
	if (form.part.options[form.part.selectedIndex].value=="def"){

	errorfields+="\n [+] Part not Selected";
	
	}

	// ******** FORMATTED FEEDBACK ********
	
	if (errorfields != "") {
	
	errorfields="The form contains error or invalid information \n"+"________________________________________\n"+errorfields+"\n________________________________________\n"+"\nPlease enter correct information into the above fields.\n"+"\nAfter you have completed press Search again.\n";
	
	window.alert(errorfields);
	
	return false;
	
	}
	
	else{
	
	return true;
	
	}
	
	
	
	return true;
	
}


function  checkPartRequestForm(form){

	var errorfields= "";//used to stored the error messages of the form
	
	// **DETAILS SECTION VALIDATION**
	
	
	//NAME
	
	//if (isfilled(form.customer_name) == false){
	
	//errorfields+="\n [+] Name not filled in";
	
	//}
	
	//EMAIL
	
	if(genericTest(EMAILre,"useremail",form.customer_email.value) == false) {
	
	errorfields+="\n [+] Email Address must be in the format someone@domainname.com";
	
	}
	
	//PHONE NUMBER
		
	if (isfilled(form.customer_phonenum) == false){

	errorfields+="\n [+] Phone Number not filled in";

	}
	
	//CUSTOMER MESSAGE
			
	if (isfilled(form.customer_addmsg) == false){

	errorfields+="\n [+] Additonal Message not filled in";

	}
	
	
	


	// ******** FORMATTED FEEDBACK ********
	
	if (errorfields != "") {
	
	errorfields="The form contains error or invalid information \n"+"________________________________________\n"+errorfields+"\n________________________________________\n"+"\nPlease enter correct information into the above fields.\n"+"\nAfter you have completed press Search again.\n";
	
	window.alert(errorfields);
	
	return false;
	
	}
	
	else{
	
	return true;
	
	}
	
	
	
	return true;
	
}

/*     END OF FORM VALIDATION    */

function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}


function validateForm(theForm)
{
	// functionName(theForm.fieldName,"fieldName")

	//if (!validRequired(theForm.firstname,"Name"))
	//	return false;
		
	//if (!validRequired(theForm.surname,"Surname",true))
	//	return false;
		
	if (!validRequired(theForm.address,"Address",true))
		return false;

	if (!validRequired(theForm.city,"City",true))
		return false;
		
	if (!validRequired(theForm.postcode,"Postcode",true))
		return false;

	if (!validRequired(theForm.companyname,"Company Name",true))
		return false;
		
	if (!validEmail(theForm.email,"Email Address",true))
		return false;
		
	if (theForm.seal.options[theForm.seal.selectedIndex].value=="All"){
		
			
		alert("Please select a seal rating.");
		theForm.seal.focus();
		return false;
		
	}

					
	return true;
}

function validateForm_index(theForm,selectedType)
{
	// functionName(theForm.fieldName,"fieldName")

	if (selectedType == 'car'){
		
		if (theForm.manu.options[theForm.manu.selectedIndex].value=="def"){		
			//alert("Please select a Make.");
			alert("Selecteer merk.");
			theForm.manu.focus();
			return false;
		}
		
	}else{
	
	
		if (!validRequired(theForm.manu,"Make"))
		return false;
		
	
	}
	
	if (selectedType == 'car'){
	
		if (theForm.model.options[theForm.model.selectedIndex].value=="def"){


			//alert("Please select a Model.");
			alert("Selecteer model.");
			
			theForm.model.focus();
			return false;

		}
	
	}else{
	
		if (!validRequired(theForm.model,"Model"))
		return false;
	
	}
	
	if (selectedType == 'car'){
	
		if (theForm.group.options[theForm.group.selectedIndex].value=="def"){


			//alert("Please select a Part Group.");
			alert("Selecteer onderdeel groep.");
			theForm.group.focus();
			return false;

		}
	
	}
	
	if (selectedType == 'car'){
	
		if (theForm.part.options[theForm.part.selectedIndex].value=="def"){


			//alert("Please select a Part.");
			alert("Selecteer onderdeel.");
			theForm.part.focus();
			return false;

		}
		
	}else{
	
					
		if (!validRequired(theForm.part,"Part"))
		return false;
	
	
	}
	
	if (theForm.year.options[theForm.year.selectedIndex].value=="def"){
		
			
		//alert("Please select a Year.");
		alert("Selecteer bouwjaar.");
		theForm.year.focus();
		return false;
		
	}
	

	
	if (!validRequired(theForm.contact_person,"ContactPersoon"))
		return false;
		
	if (!validRequired(theForm.name,"Naam"))
		return false;
			
	if (!validRequired(theForm.tele,"Telefoon"))
		return false;

	if (!validEmail(theForm.email,"Email",true))
		return false;	
	
	//confirm 
	//return confirm('Please check over the information and press OK to send the part request or CANCEL to go back and amend details');
	return confirm('Controleer de informatie en druk op OK om deze aanvraag te versturen of op ANNULEREN om aan te passen');

					
	return true;
}

function validRequired(formField,fieldLabel)
{
	
	//alert ('hello - validrequired');
	
	//alert(formField);
	
	var result = true;
	
	if (formField.value == "" || formField.value == "def")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function validEmail(formField,fieldLabel){

//regular expression for the email address field
//var EMAILre=/^[\w\-\.]+@([a-z0-9]+\.){1,3}[a-z]{2,6}$/;

var EMAILre  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(EMAILre.test(formField.value)) {

		return true;

	} else {

		//alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		alert("Vul hier aub u complete email adres in:   yourname@yourdomain.com");
		formField.focus();
		return false;

	}

}