
var url = "ajax.php?";
var checkUsernameUrl = "CheckUsername.php?";
var xmlHttp = null;
var dropDownToBePopulated = "";
var count = 0;

function GetXmlHttpObject(handler)
{
   var objXMLHttp = null;
   if (window.XMLHttpRequest)
   {
       objXMLHttp = new XMLHttpRequest();
   }
   else if (window.ActiveXObject)
   {
       objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   return objXMLHttp;
}

function stateChanged()
{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
	   var arr = xmlHttp.responseText.split('|');
	   
	   var elSel = document.getElementById(dropDownToBePopulated);
	   var childElementToAddBack = null;
	   
	   // Grab the first option to add back after the list is cleared
	   for (i=0; i < elSel.childNodes.length; i++)
	   {
			if(elSel.childNodes[i].nodeName == "OPTION")
			{
				childElementToAddBack = elSel.childNodes[i];
			}
		}
   
		// Remove all elements from the drop down before repopulating it.
		while (elSel.firstChild) 
		{
			elSel.removeChild(elSel.firstChild);
		}
		
		// Add the first child back (the firs t child was, ex: the --selct make-- option)
		try 
		{
			elSel.add(childElementToAddBack, null); // standards compliant; doesn't work in IE
		}
		catch(ex) 
		{
			var elOptNew = document.createElement('option');
			elOptNew.text = childElementToAddBack.text;
			
			elSel.add(elOptNew, elSel.selectedIndex);
			//elSel.add(childElementToAddBack, elSel.selectedIndex); // IE only
		}

	   //alert(elSel);
	   for(var a in arr)
	   {
			if(arr[a].length != 0)
			{
				var elOptNew = document.createElement('option');
				elOptNew.text = arr[a];
				elOptNew.value = arr[a];

				try 
				{
					elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
				}
				catch(ex) 
				{
					elSel.add(elOptNew, elSel.selectedIndex); // IE only
				}
			}
	   }
   }
   else 
   {
           //alert(xmlHttp.status);
   }
}

function popData(popDropDown, qStr)
{
	if (url.length != 0)
    {
		xmlHttp = GetXmlHttpObject();
		dropDownToBePopulated = popDropDown;
				
		if (xmlHttp == null)
		{
		   alert ("Browser does not support HTTP Request");
		   return;
		}
		
		//url = formatUrl(popDropDown, qStr);		
		
		if(count == 0)
			url = url + qStr;
		else
			url = url + "&" + qStr;
		
		//url = url + "&sid=" + Math.random();
	
		xmlHttp.onreadystatechange = stateChanged;
		xmlHttp.open("GET", url, true) ;
		xmlHttp.send(null);
		count++;
    }
}

function checkSearchInput()
{
	if(document.getElementById('drpMake').selectedIndex == 0)
		alert("You must select a make to search.");
	else
		document.form1.submit();
}

function validateForm()
{
	var errors = new Array();
	var index = 0;
	
	if(document.getElementById("txtUsername").value.length == 0)
	{
		errors[index] = "You must specify a username";
		index++;
	}
	
	if(document.getElementById("txtPassword").value.length == 0)
	{
		errors[index] = "You must specify a password";
		index++;
	}
	
	if(document.getElementById("txtConfirmPassword").value.length == 0)
	{
		errors[index] = "You must confirm your password";
		index++;
	}
	
	if(document.getElementById("txtEmail").value.length == 0)
	{
		errors[index] = "You must specify an email";
		index++;
	}
	
	if(document.getElementById("txtUserAddress").value.length == 0)
	{
		errors[index] = "You must provide your address";
		index++;
	}
	
	if(document.getElementById("txtUserCity").value.length == 0)
	{
		errors[index] = "You must provide your city";
		index++;
	}
	
	if(document.getElementById("txtUserState").value.length == 0)
	{
		errors[index] = "You must specify a state";
		index++;
	}
	
	if(document.getElementById("txtUserZip").value.length == 0)
	{
		errors[index] = "You must provide your zip code";
		index++;
	}
	
	if(document.getElementById("txtUserPhone").value.length == 0)
	{
		errors[index] = "You must provide your phone number";
		index++;
	}

	if(document.getElementById("rdoOwnBodyShopN").checked == false && document.getElementById("rdoOwnBodyShopY").checked == false)
	{
		errors[index] = "You must specify whether you own a body shop";
		index++;
	}
	
	if(document.getElementById("rdoOwnBodyShopY").checked == true)
	{
		if(document.getElementById("txtBodyShopName").value.length == 0)
		{
			errors[index] = "You must specify the name of the body shop";
			index++;
		}
		
		if(document.getElementById("txtBodyShopOwner").value.length == 0)
		{
			errors[index] = "You must specify the owner of the body shop";
			index++;
		}
		
		if(document.getElementById("txtBodyShopAddress").value.length == 0)
		{
			errors[index] = "You must specify the address of your body shop";
			index++;
		}		
		
		if(document.getElementById("txtBodyShopCity").value.length == 0)
		{
			errors[index] = "You must specify the city of your body shop";
			index++;
		}
		
		if(document.getElementById("txtBodyShopState").selectedIndex == 0)
		{
			errors[index] = "You must specify the state of the body shop";
			index++;
		}
		
		if(document.getElementById("txtBodyShopZip").value.length == 0)
		{
			errors[index] = "You must specify the zip code of your body shop";
			index++;
		}
		
		if(document.getElementById("txtBodyShopPhone").value.length == 0)
		{
			errors[index] = "You must specify a phone number for the body shop";
			index++;
		}
		
		var phone = document.getElementById('txtBodyShopPhone');
		var filter2 = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
		if (!filter2.test(phone.value)) 
		{
			errors[index] = "You must specify a valid body shop phone number";
			index++;
		}
	}
	
	if(document.getElementById("txtPassword").value.length > 0 && document.getElementById("txtConfirmPassword").value.length > 0)
	{
		if(document.getElementById("txtPassword").value != document.getElementById("txtConfirmPassword").value)
		{
			errors[index] = "The two provided passwords do not match";
			index++;
		}
	}
	
	var email = document.getElementById('txtEmail');
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

	var userPhone = document.getElementById('txtUserPhone');
	var filter3 = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
	
	if (!filter.test(email.value)) 
	{
		errors[index] = "You must specify a valid email address";
		index++;
	}
	

	
	if (!filter3.test(userPhone.value)) 
	{
		errors[index] = "You must specify a valid phone number";
		index++;
	}
	
	if(errors.length > 0)
	{
		var temp = "";
		for (var x in errors)
		{
			temp += "<li>" + errors[x] + "</li>";
		}
		
		var valErrors = document.getElementById("validationErrors");
		valErrors.innerHTML = temp;
		var nr = "validationErrors";

		if (document.layers)
		{
			document.layers[nr].visibility = "show";
		}
		else if (document.all)
		{
			document.all[nr].style.visibility = "visible";
		}
		else if (document.getElementById)
		{
			document.getElementById(nr).style.visibility = "visible";
		}			
	}
	else
	{
		document.frmRegister.submit();	
	}
}

function CheckUsernameAvailability()
{
	if (checkUsernameUrl.length != 0)
    {
		if(document.getElementById('txtUsername').value.length > 0)
		{			
			xmlHttp = GetXmlHttpObject();
					
			if (xmlHttp == null)
			{
			   alert ("Browser does not support HTTP Request");
			   return;
			}
			var tempUrl = checkUsernameUrl;
			var username = document.getElementById('txtUsername').value;
			
			tempUrl = tempUrl + "un=" + username + "&sid=" + Math.random();
			
			xmlHttp.onreadystatechange = stateChanged2;
			xmlHttp.open("GET", tempUrl, true) ;
			xmlHttp.send(null);
		}
    }
}

function stateChanged2()
{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
	   //var elem = document.getElementById(dropDownToBePopulated);
	   document.getElementById("usernameStatus").innerHTML = "";
	   document.getElementById("usernameStatus").innerHTML = xmlHttp.responseText;
   }
   else 
   {
		//alert(xmlHttp.status);
   }
}

function expandBodyShop(filter)
{
	var rowArray = new Array();
	rowArray[0] = "shop-info-name-row";
	rowArray[1] = "shop-info-owner-row";
	rowArray[2] = "shop-info-address-row";
	rowArray[3] = "shop-info-city-row";
	rowArray[4] = "shop-info-state-row";
	rowArray[5] = "shop-info-zip-row";
	rowArray[6] = "shop-info-phone-row";
	
	if(filter == "show")
	{
		if (document.layers)
		{
			for(var x in rowArray)
			{
				document.layers[rowArray[x]].display = "table-row";
			}
		}
		else if (document.all)
		{
			for(var x in rowArray)
			{
				document.all[rowArray[x]].display = "table-row";
			}
		}
		else if (document.getElementById)
		{
			for(var x in rowArray)
			{
				document.getElementById(rowArray[x]).style.display = "table-row";
			}
		}
	}
	else
	{
		if (document.layers)
		{
			for(var x in rowArray)
			{
				document.layers[rowArray[x]].display = "none";
			}
		}
		else if (document.all)
		{
			for(var x in rowArray)
			{
				document.all[rowArray[x]].display = "none";
			}
		}
		else if (document.getElementById)
		{
			for(var x in rowArray)
			{
				document.getElementById(rowArray[x]).style.display = "none";
			}
		}
	}
}

function ValidateGuestRegForm()
{
	var errors = new Array();
	var index = 0;
	
	if(document.getElementById("txtFirstName").value.length == 0)
	{
		errors[index] = "You must provide a first name";
		index++;
	}
	
	if(document.getElementById("txtLastName").value.length == 0)
	{
		errors[index] = "You must provide a last name";
		index++;
	}
	
	if(document.getElementById("txtGuestAddress").value.length == 0)
	{
		errors[index] = "You must provide an address";
		index++;
	}
	
	if(document.getElementById("txtGuestCity").value.length == 0)
	{
		errors[index] = "You must provide a city";
		index++;
	}
	
	if(document.getElementById("txtGuestState").value.length == 0)
	{
		errors[index] = "You must specify a state";
		index++;
	}
	
	if(document.getElementById("txtGuestZip").value.length == 0)
	{
		errors[index] = "You must provide a zip code";
		index++;
	}
	
	if(document.getElementById("txtGuestPhone").value.length == 0)
	{
		errors[index] = "You must provide a phone number";
		index++;
	}
	
	if(errors.length > 0)
	{
		var temp = "";
		for (var x in errors)
		{
			temp += "<li>" + errors[x] + "</li>";
		}
		
		var valErrors = document.getElementById("validationErrors");
		valErrors.innerHTML = temp;
		var nr = "validationErrors";

		if (document.layers)
		{
			document.layers[nr].visibility = "show";
		}
		else if (document.all)
		{
			document.all[nr].style.visibility = "visible";
		}
		else if (document.getElementById)
		{
			document.getElementById(nr).style.visibility = "visible";
		}
		
		return false;
	}
	
	return true;	
}
