// JavaScript Document
function MM_preloadImages() { //v3.0
  var d=document; 
  if(d.images) { 
  	if(!d.MM_p) 
		d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
	for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ 
		d.MM_p[j]=new Image; 
		d.MM_p[j++].src=a[i];
		}
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  
  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
  if(!(x=d[n])&&d.all) x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
  x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementByName) x=d.getElementByName(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){
	   document.MM_sr[j++]=x; 
	   if(!x.oSrc) 
	   		x.oSrc=x.src; 
		x.src=a[i+2];
	}
}


// ******************** Home Page Animation ***********************
function changeImg() {
if (curImg == 6)
	curImg = 0; 
document.animationFM.src = imgs[curImg++];
}
function Guidelines()
{
photoWindow = window.open("form_info.html", "photoWindow", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizeable=yes, titlebar=no, height=560, width=450");

}

// ******************** Swapping images functions  ***********************

function swap_car(carNum)
{
	file = (carNum - carNum % 100)/100;
	carNum %= 100;
	if (file ==1) {
	document.car.src = "images/customers/cars/car" + carNum + ".JPG";
	switch (carNum){
		case 11:
			document.getElementById("testimonial").innerHTML = "360CS Ferrari";
			break;
		case 12:
			document.getElementById("testimonial").innerHTML = "360 Spider";
			break;
		case 13:
			document.getElementById("testimonial").innerHTML = "456 Tam";
			break;
		case 21:
			document.getElementById("testimonial").innerHTML = "512M";
			break;
		case 22:
			document.getElementById("testimonial").innerHTML = "Queen Mother";
			break;
		case 23:
			document.getElementById("testimonial").innerHTML = "Dino";
			break;
		case 31:
			document.getElementById("testimonial").innerHTML = "Red Ferrari 430";
			break;
		case 32:
			document.getElementById("testimonial").innerHTML = "Repair";
			break;
		case 33:
			document.getElementById("testimonial").innerHTML = "360 Blue Ferrari";
			break;
		case 41:
			document.getElementById("testimonial").innerHTML = "360 Spider";
			break;
		case 42:
			document.getElementById("testimonial").innerHTML = "Twin Blue Ferrari";
			break;
		case 43:
			document.getElementById("testimonial").innerHTML = "Red Ferrari";
			break;
		default:
			document.getElementById("testimonial").innerHTML = "";
		}
	}
	else 
		document.restoration.src = "images/restoration/restoration" + carNum + ".jpg";

}
// ******************** VALIDATION FUNCTIONS ***********************

function transportValidator(zForm) {
	var haveError = false;
	var noError = new Image();
	noError.src = "images/no_error.gif";
	var Error = new Image();
	Error.src = "images/error.gif";
	var ActiveErrorMassage = new Image();
	ActiveErrorMassage.src = "images/error_info_over.gif";
	
	if (isEmpty(zForm.name.value) || !validLength(zForm.name.value, 2, 60) || !onlyLetters(zForm.name.value)) {
		swapImage(document.NameError, Error.src);
		haveError = true;} 
	else
		swapImage(document.NameError, noError.src);
	
	if (isEmpty(zForm.email.value) || !validLength(zForm.email.value, 6, 30) || !checkEmail(zForm.email.value) ) {
		swapImage(document.EmailError, Error.src);
		haveError = true;} 
	else
		swapImage(document.EmailError, noError.src);
	
	if (isEmpty(zForm.phone.value) || !chkPhone(zForm.phone.value)) {
		swapImage(document.PhoneError, Error.src);
		haveError = true;} 
	else 
		swapImage(document.PhoneError, noError.src);
	
	if (isEmpty(zForm.vehicleYr.value) || !validYear(zForm.vehicleYr.value) ) {
		swapImage(document.YearError, Error.src);
		haveError = true;} 
	else 
		swapImage(document.YearError, noError.src);
	
	
	if (isEmpty(zForm.vehicleMake.value) || !validLength(zForm.vehicleMake.value, 4, 20) || !onlyLetters(zForm.vehicleMake.value)) {
		swapImage(document.MakeError, Error.src);
		haveError = true;}
	else 
		swapImage(document.MakeError, noError.src);
	
	if (isEmpty(zForm.vehicleModel.value) || !validLength(zForm.vehicleModel.value, 3, 20) || !onlyLettersNnums(zForm.vehicleModel.value)) {
		swapImage(document.ModelError, Error.src);
		haveError = true; } 
	else 
		swapImage(document.ModelError, noError.src);
	
	if (haveError)
		swapImage(document.Error_Msg, ActiveErrorMassage.src);
		
	return !haveError;
}

function clearTransportForm() {
	var noError = new Image();
	noError.src = "images/no_error.gif";
	var PassiveErrorMassage = new Image();
	PassiveErrorMassage.src = "images/error_info.gif";
	swapImage(document.NameError, noError.src);
	swapImage(document.EmailError, noError.src);
	swapImage(document.PhoneError, noError.src);
	swapImage(document.YearError, noError.src);
	swapImage(document.VehicleNameError, noError.src);
	swapImage(document.MakeError, noError.src);
	swapImage(document.ModelError, noError.src);
	swapImage(document.Error_Msg, PassiveErrorMassage.src);
}

function contactValidator(zForm) {
	var haveError = false;
	var noError = new Image();
	noError.src = "images/no_error.gif";
	var Error = new Image();
	Error.src = "images/error.gif";
	var ActiveErrorMassage = new Image();
	ActiveErrorMassage.src = "images/error_info_over.gif";
	
	if (isEmpty(zForm.firstname.value) || !validLength(zForm.firstName.value, 2, 60) || !onlyLetters(zForm.firstName.value)) {
		swapImage(document.FirstNameError, Error.src);
		haveError = true;} 
	else
		swapImage(document.FirstNameError, noError.src);
	
	if (isEmpty(zForm.lastname.value) || !validLength(zForm.lastName.value, 2, 60) || !onlyLetters(zForm.lastName.value)) {
		swapImage(document.LastNameError, Error.src);
		haveError = true;} 
	else
		swapImage(document.LastNameError, noError.src);
		
	if (isEmpty(zForm.email.value) || !validLength(zForm.email.value, 6, 30) || !checkEmail(zForm.email.value) ) {
		swapImage(document.EmailError, Error.src);
		haveError = true;} 
	else
		swapImage(document.EmailError, noError.src);
	
	if (isEmpty(zForm.phone.value) || !chkPhone(zForm.phone.value)) {
		swapImage(document.PhoneError, Error.src);
		haveError = true;} 
	else 
		swapImage(document.PhoneError, noError.src);
	
	if (haveError)
		swapImage(document.Error_Msg, ActiveErrorMassage.src);
		
	return !haveError;
}

function clearContactForm() {
	var noError = new Image();
	noError.src = "images/no_error.gif";
	var PassiveErrorMassage = new Image();
	PassiveErrorMassage.src = "images/error_info.gif";
	swapImage(document.FirstNameError, noError.src);
	swapImage(document.LastNameError, noError.src);
	swapImage(document.EmailError, noError.src);
	swapImage(document.PhoneError, noError.src);
	swapImage(document.Error_Msg, PassiveErrorMassage.src);
}

function swapImage(img, newSrc) {
	img.src = newSrc;
}

/* Checks to see if the string passed to it 
   is empty or null. If so, returns true, 
   otherwise returns false. */
function isEmpty(str) {
	var empty = (str == null || str == "") ? true : false;
	return empty;
}

/* Accepts a string and two numeric values specifying
   the minimum and maximum length desired for the 
   string. Returns true if the string passed has the
   specified length, false if not. */
function validLength(str, minLen, maxLen) {
	if(typeof(str) != "string") {
		str = str.toString();
	}
	if (str.length < minLen || str.length > maxLen) {
		return false;
	} else {
		return true;
	}
}

/* Uses a regular expression to determine if the string
   passed to the function has any illegal characters. If
   the string consists only of letters and numbers, the 
   function returns true. If the string contains any
   illegal characters, the function returns false. */
function onlyLettersNnums(str) {
	var illegalChars = /[^a-zA-Z0-9\s]/		//allow only letters and numbers

	if (str.match(illegalChars)) {
		return false;
	} else {
		return true;
	}
}

function onlyLetters(str) {
	var illegalChars = /[^a-zA-Z\s]/		//allow only letters
	if (str.match(illegalChars)) {
		return false;
	} else {
		return true;
	}
}

function onlyNums(str) {
	var illegalChars = /[^0-9]/		//allow only numbers

	if (str.match(illegalChars)) {
		return false;
	} else {
		return true;
	}
}

function checkEmail(str) {
	var x = str.indexOf("@",1);
	var y = str.lastIndexOf(".");
	var z = str.length - y - 1;
	if ((x == -1) || (y < x ) || (z > 3) || (z < 2)) 
		return false;
	else
		return true;
}

/* Ectracts digits from a string. If there are not digits 
in the string its returns -1 otherwise is returns the digits
as a string. This function is good for checking phone numbers
zip codes*/
function stripNum(str){ 
	var numbers = /[0-9]/;
	numArray = new Array();
	var num = -1;
	var j = 0;
	var outputStr = "";
	if (str.length > 10)
		while ( 0 < str.length)
		{
			num = str.match(numbers);
			if (num != null) {
				numArray[j] = num;
				j++;  }
			else
				break;
			str = str.substring(str.indexOf(num) + 1,str.length);	
		}
	
	if (numArray.length > 0)
		for (i=0; i < numArray.length; i++)
		{
			outputStr += numArray[i];
		}
	else 
		outputStr = -1;
	return outputStr;

}

/* Checks that a phone number is valid by firstly stripping any special
formatting characters then the length must be either 10 or 11. If its 11 
the first digit must be a 1*/
function chkPhone(ph) {
	var valid = false;
	ph = stripNum(ph);
	if ((ph.length == 10) || (ph.length == 11)) {
		valid = true;
		if ((ph.length == 11) && (parseInt(ph.charAt(0)) != 1))
			valid = false;
		}
	else 
		valid = false;
	return valid;
}

/* Firstly a valid year must be of length 4, then it must 
be in the 19th or 20th century and lastly the year cannot be 
futuristic*/
function validYear(year) {
	var valid = false;
	var today = new Date();
	thisYear = today.getFullYear();
	if (year.length == 4)
		valid = true;
	if (valid) {
		year = parseInt(year);
		var century = (year - year%100)/100;
		if ((century == 19) || (century == 20))
			valid = true;
		else
			valid = false;
		if (year > thisYear)
			valid = false;
	}
	return valid;
}

//********************************************END VALIDATION FUNCTIONS *****************************************

function expand(obj) {
		var oldH = document.getElementById(obj.id).offsetHeight;
		var oldW = document.getElementById(obj.id).offsetWidth;
		var newH = oldH*1.4;
		var newW = oldW*1.4;
		document.getElementById(obj.id).style.zIndex = 3;
		document.getElementById(obj.id).style.position = 'relative';
		document.getElementById(obj.id).style.height = newH + 'px';
		document.getElementById(obj.id).style.width = newW + 'px';
		document.getElementById(obj.id).style.top = -(newH - oldH)/2 + 'px';
		document.getElementById(obj.id).style.overflow = 'visible';
	}
	
function contract(obj) {
		document.getElementById(obj.id).style.zIndex = 0;
		document.getElementById(obj.id).style.float = 'left';
		document.getElementById(obj.id).style.height =  57 + 'px';
		document.getElementById(obj.id).style.width = 95 + 'px';
		document.getElementById(obj.id).style.top = 0 + 'px';
		
	}

function moveleft() {
	document.getElementById("theImages").style.position = 'relative';
	var left = document.getElementById("theImages").offsetLeft;
	if (left > -530)
	{
		left -= 95;
		document.getElementById("theImages").style.left = left + "px";
	}
}
	
function moveright() {
	document.getElementById("theImages").style.position = 'relative';
	var right = document.getElementById("theImages").offsetLeft;
	if (right < 40)
	{
		right += 95;
		document.getElementById("theImages").style.left = right + "px";
	}
}
	
	