function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function hideLayer(lyr){
	$(lyr).style.visibility = 'hidden';
	$(lyr).style.display = 'none';
}

function showValues(){

	var values = '<br><table>';
	var form = document.forms[0]; // the only form
	var len = form.length - 1; //Leave off Submit Button
	var valok = true; // initialize to no validation errors

	// synchronize date fields.
	if (($F("PACK_DATE_FROM") == "") && ($F("PACK_DATE_TO") != "")) $("PACK_DATE_FROM").value = $F("PACK_DATE_TO");
	if (($F("PACK_DATE_TO") == "") && ($F("PACK_DATE_FROM") != "")) $("PACK_DATE_TO").value = $F("PACK_DATE_FROM");
	if (($F("LOAD_DATE_FROM") == "") && ($F("LOAD_DATE_TO") != "")) $("LOAD_DATE_FROM").value = $F("LOAD_DATE_TO");
	if (($F("LOAD_DATE_TO") == "") && ($F("LOAD_DATE_FROM") != "")) $("LOAD_DATE_TO").value = $F("LOAD_DATE_FROM");
	if (($F("DELIVERY_DATE_FROM") == "") && ($F("DELIVERY_DATE_TO") != "")) $("DELIVERY_DATE_FROM").value = $F("DELIVERY_DATE_TO");
	if (($F("DELIVERY_DATE_TO") == "") && ($F("DELIVERY_DATE_FROM") != "")) $("DELIVERY_DATE_TO").value = $F("DELIVERY_DATE_FROM");

//	go thru all the appropriate values
	for(i=0; i<len; i++){

		var thetag = form[i].tagName.toLowerCase();
		// include INPUT, SELECT, TEXTAREA controls
		if((thetag == "input") || (thetag == "select") || (thetag == "textarea")) {
			var elval = $F(form[i]);

			// fix textarea first, replacing crlf with br.
			if (thetag == "textarea")  
				elval = replace(replace($F('SPECIAL_INSTRUCTIONS'),'\r',''),'\n','<br>');
	
			if(form[i].getAttribute("omit") == "true")
			   continue;
			
			// keep goin' if not required and not blank
			if((form[i].className != "required") && ((elval == "") || (elval == undefined)))
			   continue;
	
			// if we made it here, we will include the field.
			values += '<tr>';
			values += '<td width="400" valign="top"><b>' + form[i].title + '</b></td>';
			// if a value, show it. if not, and is required, say so.
			// first: if a date, validate it.
			if ((form[i].getAttribute("date") == "true") && (elval != "") && (elval != undefined)) {
				// a date. validate it.
				if (ValidateDate(elval, 1))
					values += '<td>' + elval + '</td>';
				else 				
					values += "<td><font color=\"#FF0000\">" + elval + " is not mm/dd/yy <a href=\"javascript:TabOn('2')\">correct it</a></font></td>"; // make it red. also, go to tab 2, home of all the required/validated fields. 
				}	
			else if ((form[i].getAttribute("phone") == "true") && (elval != "") && (elval != undefined)) {
				// a phone number. validate it.
				if (ValidatePhoneNumber(elval))
					values += '<td>' + elval + '</td>';
				else 				
					values += "<td><font color=\"#FF0000\">" + elval + " is not a valid phone number <a href=\"javascript:TabOn('2')\">correct it</a></font></td>"; // make it red. also, go to tab 2, home of all the required/validated fields. 
				}	
			else if ((form[i].getAttribute("email") == "true") && (elval != "") && (elval != undefined)) {
				// an email address. validate it.
				if (ValidateEmailAddr(elval))
					values += '<td>' + elval + '</td>';
				else 				
					values += "<td><font color=\"#FF0000\">" + elval + " is not a valid email address <a href=\"javascript:TabOn('2')\">correct it</a></font></td>"; // make it red. also, go to tab 2, home of all the required/validated fields. 
				}	

			else if ((elval != "") && (elval != undefined))
				values += '<td>' + elval + '</td>';
			else if (form[i].className == "required") {
				values += "<td><font color=\"#FF0000\">required <a href=\"javascript:TabOn('2')\">correct it</a></font></td>"; // make it red. also, go to tab 2, home of all the required fields. 
				valok = false;
				}
				
			values += '</tr>';
		}

	}
	values += '</table>';
	$("insidepage6").innerHTML = values;
	
	/* if any validation errors, disable the SUBMIT button.
	   if no errors, enable the button. */
	   if (valok)
	     $("submit").disabled = false;
	   else
	     $("submit").disabled = true;
	
}

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
} 

// show border around boxes when there's focus
addEvent(window, 'load', function() {
 var input, textarea;
 var inputs = document.getElementsByTagName('input');
 for (var i = 0; (input = inputs[i]); i++) {
   addEvent(input, 'focus', oninputfocus);
   addEvent(input, 'blur', oninputblur);
 }
 var textareas = document.getElementsByTagName('textarea');
 for (var i = 0; (textarea = textareas[i]); i++) {
   addEvent(textarea, 'focus', oninputfocus);
   addEvent(textarea, 'blur', oninputblur);
 }
});
function oninputfocus(e) {
 /* Cookie [9]-cutter code to find the source of the event */
 if (typeof e == 'undefined') {
   var e = window.event;
 }
 var source;
 if (typeof e.target != 'undefined') {
    source = e.target;
 } else if (typeof e.srcElement != 'undefined') {
    source = e.srcElement;
 } else {
   return;
 }
 /* End cookie-cutter code */
 source.style.border='2px solid #000';
}
function oninputblur(e) {
 /* Cookie-cutter code to find the source of the event */
 if (typeof e == 'undefined') {
   var e = window.event;
 }
 var source;
 if (typeof e.target != 'undefined') {
    source = e.target;
 } else if (typeof e.srcElement != 'undefined') {
    source = e.srcElement;
 } else {
   return;
 }
 /* End cookie-cutter code */
 source.style.border='2px solid #ccc';
} 


// for required fields

function checkRequired(id) {
 var formfield = $(id);
 if (formfield.value.length == 0) {
   formfield.className = 'problem';
 } else {
   formfield.className = 'completed';
 }
} 


function checkPhone(phoneid) {
 var phone = $(phoneid);
 var digits = phone.value.replace(/[^0-9]/ig, '');
 if (!digits) {
   return;
 }
 if (digits.length == 10) {
   phone.value = '(' + digits.substring(0, 3) + ') ' +  
     digits.substring(3, 6) + '-' +  
     digits.substring(6, 10);
 } else {
   phone.value = digits;
 }
} 

function getServiceFlag() {
// find out if service is for Origin, Destination, or both.
// return "O", "D", or "OD".
 if ($F('PERFORM_SERVICE_ORIGIN') == 'ORIGIN') return 'O';
 if ($F('PERFORM_SERVICE_DESTINATION') == 'DESTINATION') return 'D';
 if ($F('PERFORM_SERVICE_ORIGIN_DEST') == 'BOTH') return 'OD';
 return 'OD'; 
}

function setupServices() {
 var ret = getServiceFlag();
 
 switch (ret){
 case 'O':
  // origin only
  $('perform_service_at').innerHTML = 'Perform Service at Origin Only.';
  break;

 case 'D':
  // destination only
  $('perform_service_at').innerHTML = 'Perform Service at: Destination Only.';
  break;

 default : 
  // could be OD (both orig. and dest.) or some other value.
  $('perform_service_at').innerHTML = 'Perform Service at: Origin and Destination (uncheck destination if not required).';

 } 


}


function destFromOrigin(obj) {
// derive destination checkbox object from origin.
   var originID = obj.id;
   var destID = originID.replace("ORIGIN", "DEST");
   return $(destID); 
}

function originFromDest(obj) {
// derive origin checkbox object from dest.
   var destID = obj.id;
   var originID = destID.replace("DEST", "ORIGIN");
   return $(originID); 
}

function qtyFromCheck(obj) {
// derive qty field from checkbox object.
   var checkID = obj.id;
   var qtyID = checkID.replace("DEST","QTY"); // try this
   qtyID = qtyID.replace("ORIGIN","QTY"); // try this
   return $(qtyID); 
}



function originServiceClicked(originCheckBox) {
 // possibly check the dest checkbox too, if O + D is selected.
 if (getServiceFlag() == 'OD') {
   if ($F(originCheckBox) == "Y") {
   // derive name of dest box.
   destFromOrigin(originCheckBox).checked = true;
  }
 }
}

function serviceClicked(obj) {
 // if a service is checked, enable the qty box. otherwise disable.
 // also: via originServiceClicked, under certain conditions,
 // if the origin is checked, we also check the dest.
 
 // init
 var somethingChecked = false;
  
 // origin?
 var is_it_origin = obj.id.indexOf('ORIGIN');
 if (is_it_origin != -1) {
 // origin
  originServiceClicked(obj);
  if (($F(obj) == "Y") || ($F(destFromOrigin(obj)) == "Y")) 
   somethingChecked = true;
  }
 else {
  // destination
  if (($F(obj) == "Y") || ($F(originFromDest(obj)) == "Y")) 
   somethingChecked = true;
 }
 
 // if either is checked now, enable Qty input box.
 // otherwise, disable it.
 
 var qtyObj = qtyFromCheck(obj);
 if (somethingChecked == true) {
  // if it's disabled, enable it
  if (qtyObj.disabled == true) {
  qtyObj.disabled = false;
  qtyObj.className = "enabled";
  // default to a value of 1.
  qtyObj.value = "1";
  }
 }
  else {
   qtyObj.disabled = true;
   qtyObj.className = "disabled";
   qtyObj.value = ""; // blank it out
 }
}
