var phrase = "The answer is required";
var color_on = "#D00";
var color_off = "#FFF";

var f_inv = null;

function FInv(elem)
{

	if(f_inv == null)
		f_inv = elem;
}

function validate(e)
{
	f_inv = null;
	
	if(validateGeneralCompanyInformation())
	{
		document.getElementById("incmail").submit();
	}
	f_inv.focus();
	return false;
}

function RemoveErrorMessage(id_control)
{
	id_control = id_control.concat("_tr_err");
	var cntrl = document.getElementById(id_control);
	if(cntrl != null)
	{
		var parent = cntrl.parentNode;
		parent.removeChild(cntrl);
	}
}

function InsertErrorMessage(id_control, text, colspn)
{	
	var tmp_ctrl = document.getElementById(id_control.concat("_tr_err"));
	if(tmp_ctrl == null)
	{
		id_control = id_control.concat("_tr");
		var tr = document.createElement("tr");
		var tmp = id_control;
		tr.setAttribute("id", id_control.concat("_err"));
		
		var td = document.createElement("td");
		var tmp_td = document.createElement("td");
		
		td.setAttribute("colspan", colspn);
		td.setAttribute("align","left");
		td.className = "error_message";		
		text = document.createTextNode(text);
		
		td.appendChild(text);
		
		tr.appendChild(tmp_td);
		tr.appendChild(td);
		
		var fail_control = document.getElementById(id_control);
		var parent = fail_control.parentNode;
		
		parent.insertBefore(tr,fail_control);
	}
}

function validateChauffeurInformation()
{
	var result = true;
	if(! hasValue(document.getElementById("chauffeurDressCode")))
	{
		//document.getElementById("chauffeurDressCodeS").style.color=color_on;
		InsertErrorMessage("chauffeurDressCode", phrase, 1);
		FInv(document.getElementById("chauffeurDressCode"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("chauffeurDressCode");
	}
	if(!hasValue(document.getElementById("dispatcherChauffeurCommunicate")))
	{

		InsertErrorMessage("dispatcherChauffeurCommunicate", phrase, 1);
		FInv(document.getElementById("dispatcherChauffeurCommunicate"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("dispatcherChauffeurCommunicate");
	}
	
	if(! hasValue(document.getElementById("chauffeurScreening")))
	{
		InsertErrorMessage("chauffeurScreening", phrase, 1);
		FInv(document.getElementById("chauffeurScreening"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("chauffeurScreening");
	}
	
	if(! hasValue(document.getElementById("chauffeurInitTraining")))
	{
		InsertErrorMessage("chauffeurInitTraining", phrase, 1);
		FInv(document.getElementById("chauffeurInitTraining"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("chauffeurInitTraining");
	}
	
	if(! hasValue(document.getElementById("chauffeurOngoingTraining")))
	{
		InsertErrorMessage("chauffeurOngoingTraining", phrase, 1);
		FInv(document.getElementById("chauffeurOngoingTraining"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("chauffeurOngoingTraining");
	}
	return result;
}

function validateServiceInformation()
{
	var result = true;
	if(ifYes(document.forms["incmail"].You_Provide_Greeters) && !yesNo(document.forms["incmail"].Greeter_Charge))
	{
		
		InsertErrorMessage("youProvideGreeters", phrase, 1);
		FInv(document.forms["incmail"].You_Provide_Greeters[0]);
		result = false;
	}
	else
	{
		RemoveErrorMessage("youProvideGreeters");
	}
	if(ifYes(document.forms["incmail"].Your_Own_Greeters))
	{
		RemoveErrorMessage("useGreeterSvc");
		if(!hasValue(document.getElementById("meetingLocations")))
		{
			result = false;
			InsertErrorMessage("meetingLocations", "List name of greeter service is required", 1);
			FInv(document.getElementById("meetingLocations"));
		}
		else
		{
			RemoveErrorMessage("meetingLocations");
		}
	}
	else
	{ 
		if(document.forms["incmail"].Your_Own_Greeters[1].checked)
		{
			RemoveErrorMessage("meetingLocations");
			if(!yesNo(document.forms["incmail"].Use_Greeter_Svc))
			{
				InsertErrorMessage("useGreeterSvc", phrase, 1);
				FInv(document.forms["incmail"].Use_Greeter_Svc[0]);
				result = false;
			}
			else
			{
				RemoveErrorMessage("useGreeterSvc");
			}
		}
	}	
	
	if(ifNo(document.forms["incmail"].Res_Confirmed) && !hasValue(document.getElementById("howConfirmed")))
	{ 
	
		result = false;
		FInv(document.getElementById("howConfirmed"));
		InsertErrorMessage("howConfirmed", phrase, 1);
	}
	else
	{
			
		RemoveErrorMessage("howConfirmed");
	}
	
	if(ifYes(document.forms["incmail"].Flights_Tracked) && !hasValue(document.getElementById("flightTrackingSystem")))
	{
		result = false;
		FInv(document.getElementById("flightTrackingSystem"));
		InsertErrorMessage("flightTrackingSystem", "Tracking system is required", 1);
	}
	else
	{
		RemoveErrorMessage("flightTrackingSystem");
	}
	if(ifYes(document.forms["incmail"].Charge_Canc_Delays) && !hasValue(document.getElementById("cancellationCharge")))
	{
		result = false;
		FInv(document.getElementById("cancellationCharge"));
		InsertErrorMessage("cancellationCharge", "Charge is required", 1);
	}
	else
	{
		RemoveErrorMessage("cancellationCharge");
	}	
	
	if(ifYes(document.forms["incmail"].Vanity_LP) &&!hasValue(document.getElementById("vanityLPText")))
	{
		result = false;
		FInv(document.getElementById("vanityLPText"));
		InsertErrorMessage("vanityLPText", phrase, 1);
		
	}
	else
	{
		RemoveErrorMessage("vanityLPText");
	}
	return result;
}

function validateGeneralCompanyInformation()
{
	var result = true;
	if(! hasValue(document.getElementById("company_name")))
	{
		InsertErrorMessage("company_name", "Company name is required", 1);
		FInv(document.getElementById("company_name"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("company_name");
	}
	if(!hasValue(document.getElementById("address")))
	{
		InsertErrorMessage("address", "Addres is required", 1);
		FInv(document.getElementById("address"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("address");
	}
	if(! hasValue(document.getElementById("city")))
	{
		InsertErrorMessage("city", phrase, 1);
		FInv(document.getElementById("city"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("city");
	}
	if(! hasValue(document.getElementById("state")))
	{
		InsertErrorMessage("state", "State is required", 1);
		FInv(document.getElementById("state"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("state");
	}
	if(!isMail(document.getElementById("mail")))
	{
		InsertErrorMessage("mail", "Mail is required", 1);
		FInv(document.getElementById("mail"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("mail");
	}
	if(! hasValue(document.getElementById("fedtax")))
	{
		InsertErrorMessage("fedtax", "Federal Tax ID# is required", 1);
		FInv(document.getElementById("fedtax"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("fedtax");
	}
	
	var reg = new RegExp("^(([1-9]([0-9]|-){5,})[0-9])$","g");
	if(!reg.test(document.getElementById("phoneLocal").value) || !has800Number(document.getElementById("phone800")))
	{
		InsertErrorMessage("phoneLocal", "Local and 800 Numbers is required", 1);
		if(!reg.test(document.getElementById("phoneLocal").value))
		{
			document.getElementById("phoneLocal").style.color = "#FF0000";
			FInv(document.getElementById("phoneLocal"));
		}
		if(!has800Number(document.getElementById("phone800")))
		{
			document.getElementById("phone800").style.color = "#FF0000";
			FInv(document.getElementById("phone800"));
		}
		result = false;
	}		
	else
	{
		RemoveErrorMessage("phoneLocal");
		document.getElementById("phone800").removeAttribute("style");
		document.getElementById("phoneLocal").removeAttribute("style");
	}
		
/*	if(!has800Number(document.getElementById("phone800")))
	{
		InsertErrorMessage("phone800", phrase, 1);
		result = false;
	}		
	else
	{
		RemoveErrorMessage("phone800");
	}*/
	if(!hasHoursOfOperations())
	{
		InsertErrorMessage("mon", "Hours of Operations is required", 3);
		FInv(document.getElementById("phone800"));
		result = false;
	}		
	else
	{
		RemoveErrorMessage("mon");
	}
	if(!validateChauffeurInformation());
		if(!validateServiceInformation())
			result = false;
			
	return result;
}


function You_Provide_Greeters_Yes()
{
	document.getElementById("youProvideGreeters").style.color = color_on;
}

function You_Provide_Greeters_No()
{
	document.getElementById("youProvideGreeters").style.color = color_off;
}
function Your_Own_Greeters_Yes()
{
	document.getElementById("useGreeterSvc").style.color = color_off;
	document.getElementById("meetingLocationsS").style.color = color_on;
}
function Your_Own_Greeters_No()
{
	
	document.getElementById("meetingLocationsS").style.color = color_off;
	document.getElementById("useGreeterSvc").style.color = color_on;
}
function Res_Confirmed_Yes()
{
	document.getElementById("howConfirmedS").style.color = color_off;
}
function Res_Confirmed_No()
{
	document.getElementById("howConfirmedS").style.color = color_on;
}
function Flights_Tracked_Yes()
{
	document.getElementById("flightTrackingSystemS").style.color = color_on;
}
function Flights_Tracked_On()
{
	document.getElementById("flightTrackingSystemS").style.color = color_off;
}
function Charge_Canc_Delays_Yes()
{
	document.getElementById("cancellationChargeS").style.color = color_on;
}
function Charge_Canc_Delays_No()
{
	document.getElementById("cancellationChargeS").style.color = color_off;
}

function Vanity_LP_Yes()
{
	document.getElementById("vanityLPTextE").style.color = color_on;
}

function Vanity_LP_No()
{
	document.getElementById("vanityLPTextE").style.color = color_off;
}



function ifYes(radioCollection)
{
	return radioCollection[0].checked;
}

function ifNo(radioCollection)
{
	return radioCollection[1].checked;
}

function yesNo(radioCollection)
{
	if(radioCollection[0].checked || radioCollection[1].checked)
		return true;
	return false;		
}

function hasHoursOfOperations()
{
	var result = false;
	if(hasValue(document.getElementById("mon")) || hasValue(document.getElementById("tue")) || hasValue(document.getElementById("wed")) || hasValue(document.getElementById("thu")) || hasValue(document.getElementById("fri")) || hasValue(document.getElementById("sat")))
		result = true;
	return result;
}

function has800Number(element)
{
	var reg = new RegExp("^[1-9](([0-9]|-){5,}[0-9]$)","g");
	return reg.test(element.value); 
}

function isMail(element)
{
	while(element.value.match(" ") != null)
		element.value = element.value.replace(" ", "");
	var reg = new RegExp("([a-zA-Z_0-9])+(@)([a-zA-Z0-9])+(.)*");
	var tmp = element.value;
	return reg.test(tmp);
}

function hasValue(element)
{
	var tmp_str = trim(element.value); 
	if(tmp_str.length > 0)
		return  true;
	else
		return false;
}

function whichBrowser()
{
	var nescape = "Netscape ";
	var IE = "Microsoft Internet Explorer";
	if(navigator.appName == IE)
		return false;
	return true;
}

function trim(inputString) 
{
   if (typeof inputString != "string") 
	 { 
	 		return inputString; 
	 }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   return retValue; 
}
