// Show Hide sections of the Form to be filled out
var browserType;

if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) 
{
	browserType= "gecko"
}

function hide2(section) {
	if (browserType == "gecko" )
		document.poppedLayer = eval('document.getElementById(section)');
	else if (browserType == "ie")
		document.poppedLayer = eval('document.getElementById(section)');
	else
		document.poppedLayer = eval('document.layers[section]');
		
	document.poppedLayer.style.display = "none";
}

function show2(section) {
	if (browserType == "gecko" )
		document.poppedLayer = eval('document.getElementById(section)');
	else if (browserType == "ie")
		document.poppedLayer = eval('document.getElementById(section)');
	else
		document.poppedLayer = eval('document.layers[section]');
		
	document.poppedLayer.style.display = "inline";
}




// Trip Choices
function TripChoice()
{		
	var tripType = document.getElementById('Scat_form1_ddTripType');
	var index = tripType.selectedIndex;
	

	var startPoint = document.getElementById('Scat_form1_txtStartAddr');
	var endPoint = document.getElementById('Scat_form1_txtReturnAddr');
	
	//startPoint.innerText = tripType.options[index].value;
	
	if (tripType.options[index].value == 'One-way')
	{
		endPoint.readOnly = false;	
		endPoint.style.backgroundColor='#FFF';
		show2("drop1");
		hide2("drop1b");
		hide2("drop2");
		hide2("return");
		//endPoint.disabled = false;
	}
	else if (tripType.options[index].value == 'Round Trip') 
	{
		endPoint.readOnly = true;			
		endPoint.style.backgroundColor='#DCDCDC';
		endPoint.style.color='#000';
		endPoint.innerText = startPoint.value;
		hide2("drop2");	
		show2("drop1");
		show2("drop1b");
		show2("return");			
	}
	else if (tripType.options[index].value == 'Multiple Request') 
	{
		endPoint.readOnly = false;	
		endPoint.style.backgroundColor='#FFF';
		endPoint.innerText = startPoint.value;
		show2("drop1");			
		show2("drop1b");
		show2("drop2");				
		show2("return");
	}
	
}

function DisableDropPoint(x, tf)
{
	var dropOffAddr = document.getElementById('Scat_form1_txtDropOffAddr' + x);
	var dropOffDate = document.getElementById('Scat_form1_txtDropOffDate' + x);
	var dropOffTime = document.getElementById('Scat_form1_txtDropOffTime' + x);
	var dropOffAMPM = document.getElementById('Scat_form1_ddDropOffAMPM' + x);
	var pickUpDate = document.getElementById('Scat_form1_txtPickUpDate' + x);
	var pickUpTime = document.getElementById('Scat_form1_txtPickupTime' + x);
	var pickUpAMPM = document.getElementById('Scat_form1_ddPickUpAMPM' + x);

	dropOffAddr.disabled = tf;
	dropOffDate.disabled = tf;
	dropOffTime.disabled = tf;
	dropOffAMPM.disabled = tf;
	pickUpDate.disabled = tf;
	pickUpTime.disabled = tf;
	pickUpAMPM.disabled = tf;		
}

function CopyStarting()
{
	var tripType = document.getElementById('Scat_form1_ddTripType');
	var startPoint = document.getElementById('Scat_form1_txtStartAddr');
	var endPoint = document.getElementById('Scat_form1_txtReturnAddr');
	
	var going1 = document.getElementById('Scat_form1_txtGoingToAddr1');
	var going2 = document.getElementById('Scat_form1_txtGoingToAddr2');
	var pickup1 = document.getElementById('Scat_form1_txtPickUpFromAddr1');
	var pickup2 = document.getElementById('Scat_form1_txtPickUpFromAddr2');	

	var index = tripType.selectedIndex;
	
	if ((tripType.options[index].value == 'Round Trip') || (tripType.options[index].value == 'Multiple Request'))
	{
		pickup1.innerText = going1.value;
		
		if (tripType.options[index].value == 'Multiple Request')
		{	
			pickup2.innerText = going2.value;			
		}
		
		endPoint.innerText = startPoint.value;
	}
}

function copyHomeToWork(readBox) {
	var HomeA = document.getElementById('Scat_form1_txtHomeArea');
	var HomeB = document.getElementById('Scat_form1_txtHomeA');
	var HomeC = document.getElementById('Scat_form1_txtHomeB');
	var WorkA = document.getElementById('Scat_form1_txtWorkArea');
	var WorkB = document.getElementById('Scat_form1_txtWorkA');
	var WorkC = document.getElementById('Scat_form1_txtWorkB');
	
	if (readBox.checked) {
		WorkA.value = HomeA.value;
		WorkB.value = HomeB.value;
		WorkC.value = HomeC.value;
	} else {
		WorkA.value = "";
		WorkB.value = "";
		WorkC.value = "";
	}
	
}

function copyHomeToCell(readBox) {
	var HomeA = document.getElementById('Scat_form1_txtHomeArea');
	var HomeB = document.getElementById('Scat_form1_txtHomeA');
	var HomeC = document.getElementById('Scat_form1_txtHomeB');
	var CellA = document.getElementById('Scat_form1_txtCellArea');
	var CellB = document.getElementById('Scat_form1_txtCellA');
	var CellC = document.getElementById('Scat_form1_txtCellB');
	
	if (readBox.checked) {
		CellA.value = HomeA.value;
		CellB.value = HomeB.value;
		CellC.value = HomeC.value;
	} else {
		CellA.value = "";
		CellB.value = "";
		CellC.value = "";
	}
	
}	




// Validation Functions
function validateFormOnSubmit() {
	var reason = "";


	reason += validateEmpty(document.getElementById('Scat_form1_txtName'), "The Clients name is required.");
	reason += validatePhone(document.getElementById('Scat_form1_txtHomeArea'),document.getElementById('Scat_form1_txtHomeA'), document.getElementById('Scat_form1_txtHomeB'), "Home phone number (including area code) is required.");
	reason += validateEmpty(document.getElementById('Scat_form1_txtEmail'), "Email address is required.");	
	reason += validateDate(document.getElementById('Scat_form1_txtDate'));
	reason += validateDropDown(document.getElementById('Scat_form1_ddTripType'), "You must choose what type of request.");
	reason += validateEmpty(document.getElementById('Scat_form1_txtStartAddr'), "Your starting address is required.");
	reason += validateEmpty(document.getElementById('Scat_form1_txtGoingToAddr1'), "The address you are going to is required.");
	reason += validateTime(document.getElementById('Scat_form1_ddGoingToHour1'),document.getElementById('Scat_form1_ddGoingToMin1'),document.getElementById('Scat_form1_ddGoingToAMPM1'), "Appointment time is required.");
	reason += validateDropDown(document.getElementById('Scat_form1_ddGoingToPurpose1'), "Your purpose of your trip is required.");
	//reason += validateEmpty(document.getElementById('Scat_form1_txtDate'), "Your travel date is required.");


	var tripType = document.getElementById('Scat_form1_ddTripType');
	var index = tripType.selectedIndex;
		
	if (tripType.options[index].value == 'Round Trip') 
	{
		reason += validateTime(document.getElementById('Scat_form1_ddPickUpFromHour1'),document.getElementById('Scat_form1_ddPickUpFromMin1'),document.getElementById('Scat_form1_ddPickUpFromAMPM1'), "Pick up time is required.");
		//reason += validateEmpty(document.getElementById('Scat_form1_txtReturnAddr'), "The address you are returning to is required.");
		//validateTimeDifference()
	}
	else if (tripType.options[index].value == 'Multiple Request') 
	{
		reason += validateEmpty(document.getElementById('Scat_form1_txtGoingToAddr2'), "The address you are going to is required.");
		reason += validateTime(document.getElementById('Scat_form1_ddGoingToHour2'),document.getElementById('Scat_form1_ddGoingToMin2'),document.getElementById('Scat_form1_ddGoingToAMPM2'), "Appointment time is required.");
		reason += validateDropDown(document.getElementById('Scat_form1_ddGoingToPurpose2'), "Your purpose of your trip is required.");		
		
		reason += validateTime(document.getElementById('Scat_form1_ddPickUpFromHour2'),document.getElementById('Scat_form1_ddPickUpFromMin2'),document.getElementById('Scat_form1_ddPickUpFromAMPM2'), "Pick up time is required.");	
		reason += validateEmpty(document.getElementById('Scat_form1_txtReturnAddr'), "The address you are returning to is required.");
	}	

	//reason += validateTimeDifference(
	//	document.getElementById('Scat_form1_ddGoingToHour1'), 
	//	document.getElementById('Scat_form1_ddGoingToMin1'), 
	//	document.getElementById('Scat_form1_ddGoingToAMPM1'), 		
	//	document.getElementById('Scat_form1_ddPickUpFromHour1'),
	//	document.getElementById('Scat_form1_ddPickUpFromMin1'),
	//	document.getElementById('Scat_form1_ddPickUpFromAMPM1'), 
	//	"Pick up time is required.");	

	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}

	return true;
}

//function validateTimeDifference(a1, a2, a3, b1, b2, b3, msg)
//{
//	var error = "";
//	aTime = new Date();
//	btime = new Date();


//	if (a3.options[a3.selectedIndex].value == "PM")	
//	{
//		aTime.setHours((a1.options[a1.selectedIndex].value*1)+12);
//	} else if (a3.options[a3.selectedIndex].value == "AM")
//	{
//		aTime.setHours(a1.options[a1.selectedIndex].value);
//	} else {
//		error = msg;
//	}
		
//	aTime.setMinutes(a2.options[a2.selectedIndex].value);

//	if (b3.options[b3.selectedIndex].value == "PM")	
//	{
//		bTime.setHours((b1.options[b1.selectedIndex].value*1)+12);
//	} else if (a3.options[b3.selectedIndex].value == "AM")
//	{
//		bTime.setHours(b1.options[b1.selectedIndex].value);
//	} else {
//		error = msg;
//	}
		
//	bTime.setMinutes(b2.options[b2.selectedIndex].value);	
	//var aTimeInt = ((aTime.getHours()*1)*60) + aTime.getMinutes();
	
//	return m;

//}

function validateEmpty(fld, msg) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = msg + "\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePhone(a,b,c, msg) {
    var error = "";
 
	error = validateEmpty(a, msg);
	error = validateEmpty(b, msg);
	error = validateEmpty(c, msg);
    
    return error;  
}

function validateTime(a, b, c, msg)
{
	var error = "";
 
	error = validateDropDown(a, msg);
	error = validateDropDown(b, msg);
	error = validateDropDown(c, msg);
    
    return error;	
}

function validateDropDown(fld, msg)
{
	var error = "";
	var index = fld.selectedIndex;
	
	
	if (index == 0)
	{
        fld.style.background = 'Yellow'; 
        error = msg + "\n"
	} else {
		fld.style.background = 'White';
	}
	
	return error;  
}

function trim(s)
{
	return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validateDate(fld)
{
	var error="";
	
	error = validateEmpty(document.getElementById('Scat_form1_txtDate'), "Your travel date is required.");
	
	if (error != "") {		
		return error;
	}
	
	var validformat=/^\d{2}\/\d{2}\/\d{4}$/ 
	//Basic check for format validity
	
	
	if (!validformat.test(fld.value))
		error = "Invalid Date Format. Date format should be mm/dd/yyyy.\n";
	else
	{	//Detailed check for valid date ranges
		var monthfield=fld.value.split("/")[0];
		var dayfield=fld.value.split("/")[1];
		var yearfield=fld.value.split("/")[2];
		var dayobj = new Date(yearfield, monthfield-1, dayfield);
		
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
			error = "Invalid Day, Month, or Year. Please correct and submit again.\n"
	}
	
	return error;	
	
	
	return error;
}










