function unHiderPick() {
	var where = document.getElementById("where");
	if (where.value == "airport") {
		document.getElementById("airport").style.display = 'block';
		document.getElementById("address").style.display = 'none';
		document.getElementById("poi").style.display = 'none';
	}

	if (where.value == "address") {
		document.getElementById("airport").style.display = 'none';
		document.getElementById("address").style.display = 'block';
		document.getElementById("poi").style.display = 'none';
	}

	if (where.value == "poi") {
		document.getElementById("airport").style.display = 'none';
		document.getElementById("address").style.display = 'none';
		document.getElementById("poi").style.display = 'block';
	}
}


function dropOff() {
	var drop = document.getElementById("diff").checked;

	if (drop) {
		document.getElementById("dropoff_diff").style.display = 'block';
	} else {
		document.getElementById("dropoff_diff").style.display = 'none';
	}
}

function fieldsHandler() {
    var drop = document.getElementById("companypref").checked;

    clearValuesDiv();
    if (drop){
        if (document.getElementById("vendor").value == 'BV'){
            //Show ABO preselected companies and coupon codes
            document.getElementById("discounts").style.display = 'block';
            preselectDiscountCompanies();
        }else{
            completeFields(1,document.getElementById("vendor").value,1);
        }
    }else{
        document.getElementById("discounts").style.display = 'none';
    }
}

function unHiderDrop() {
	var where = document.getElementById("dwhere");

	if (where.value == "doairport") {
		document.getElementById("dairport").style.display = 'block';
		document.getElementById("daddress").style.display = 'none';
		document.getElementById("dpoi").style.display = 'none';
	}

	if (where.value == "doaddress") {
		document.getElementById("dairport").style.display = 'none';
		document.getElementById("daddress").style.display = 'block';
		document.getElementById("dpoi").style.display = 'none';
	}

	if (where.value == "dopoi") {
		document.getElementById("dairport").style.display = 'none';
		document.getElementById("daddress").style.display = 'none';
		document.getElementById("dpoi").style.display = 'block';
	}
}

function change(id, newClass) {
	identity=document.getElementById(id);
	identity.className=newClass;
}

function show(idLayer) { document.getElementById(idLayer).style.display='block'; }
function showInline(idLayer) { document.getElementById(idLayer).style.display='inline'; }
function hide(idLayer) { document.getElementById(idLayer).style.display='none'; }

function newWindow(url)
{
   urlWindow = window.open(url, 'AirportCodes', 'width=450,height=350');
   urlWindow.focus;
}

function monthChange(monthSelect)
{
   if ((monthSelect.name == 'pudate_mo') &&
       (document.CarSearch.dodate_mo.selectedIndex < monthSelect.selectedIndex)
      )
   {
      document.CarSearch.dodate_mo.selectedIndex = monthSelect.selectedIndex;
   }
  
}

function dayChange(daySelect)
{
   selected = daySelect.selectedIndex;
   if ((daySelect.name == 'pudate_dy') &&
       (document.CarSearch.dodate_dy.selectedIndex < selected + 1) &&
       (document.CarSearch.dodate_mo.selectedIndex <= document.CarSearch.pudate_mo.selectedIndex)
      )
   {
      if (selected < daySelect.length - 1)
      {
         document.CarSearch.dodate_dy.selectedIndex = selected + 1;
      }
      else
      {
         document.CarSearch.dodate_mo.selectedIndex += 1;
         document.CarSearch.dodate_dy.selectedIndex = 0;
      }
   }

}

function validate(form) {
  re = /S+/;
  if (!re.test(form.pucity.value)) {
    alert('Please enter a pick-up city');
    return false;
  }
  return true;
}

function check_area()
	{
	
	var number = document.CarSearch.puwhere.selectedIndex;

	if (document.CarSearch.puwhere.options[number].value == 'CITY') 
		{
		document.CarSearch.area.selectedIndex = 1;
		}
	else if (document.CarSearch.puwhere.options[number].value == 'AIRPORT') 
		{
		document.CarSearch.area.selectedIndex = -1;
		}

	return true;
	}

function update_docity()
	{
	if (document.CarSearch.docity.value) 
		{
		document.CarSearch.same.checked = false;
		}
	return true;
	}

function clear_docity()
	{
	document.CarSearch.docity.value = "";
	return true;
	}

function check_dropoff()
	{
	if (document.CarSearch.same.checked == true) 
		{
		clear_docity();
		}
	return true;
	}

function submit_to() 
	{
	//alert('pu =' + document.CarSearch.puwhere.value);

	update_docity();
	
	var pickup = document.CarSearch.pucity.value;
	var dropoff = document.CarSearch.docity.value;

	var whitesp = /[ \t\n\r\f\v]/g;

	var newpu = pickup.replace(whitesp, "");
	var newdo = dropoff.replace(whitesp, "");

	var pucity = new String(newpu);
	var docity = new String(newdo);
	
	if (pucity.length != 3) 
		{
		alert('Invalid or missing pick up airport code');
		return false;
		}

	if (docity.length > 1) 
		{
		if (docity.length != 3) 
			{
			alert('Invalid or missing drop off airport code');
			return false;
			}
		}

	var number = document.CarSearch.puwhere.selectedIndex;
	var selvendor = document.CarSearch.vendor.selectedIndex;
	if (document.CarSearch.puwhere.options[number].value == 'CITY')
		{
		var ven = new String(document.CarSearch.vendor.options[selvendor].value);
		if (ven.length == 0) 
			{
			alert('Please select a Rental Company preference for city searches.');
			return false;
			}
		}

	var number = document.CarSearch.puwhere.selectedIndex;

	if (document.CarSearch.puwhere.options[number].value == 'CITY') 
		{
		document.CarSearch.action = "/nexres/cars/locations.cgi";
		}
	else if (document.CarSearch.puwhere.options[number].value == 'AIRPORT') 
		{
		document.CarSearch.action = "/nexres/cars/results.cgi";
		}

	document.CarSearch.submit();
	}

function reset_form()
	{

	document.CarSearch.pucity.value ="";
	document.CarSearch.docity.value ="";
	document.CarSearch.area.selectedIndex = -1;
	document.CarSearch.vendor.selectedIndex = -1;
	document.CarSearch.carcode.selectedIndex = -1;
	document.CarSearch.currency.selectedIndex = -1;
	
	return true;
	}

//Completes all the fields with the correct data, also handles which set to show/hide
function completeFields (id, company_id, rental){
    var text;
    var values;

    values = fieldValues(company_id);
    text = completeText(id, values, company_id);

    if (rental){
        clearValuesDiv();

        if(document.getElementById("vendor").value != 'BV'){
            document.getElementById("discounts").style.display = 'none';
            document.getElementById("discounts_rental").innerHTML = text;

            if (document.getElementById("companypref").checked){
                document.getElementById("discounts_rental").style.display = 'block';
            }else{
                document.getElementById("discounts_rental").style.display = 'none';
            }

        }else{
            if (document.getElementById("companypref").checked){
                document.getElementById("discounts").style.display = 'block';
                preselectDiscountCompanies();
            }
        }
    }else{
        if(company_id){
            document.getElementById("providerFields" + id).innerHTML = text;
        }else{
            clearValuesDiv(id);
        }
    }
    return 1;
}
// Returns the values for a company code. Use the companyDiscount Array. Dpending on the data, it completes the
// required fields
function fieldValues (code) {
    var returnArray = new Array();
    for (var i=0; i < companyDiscounts.length; i = i + 4){
        if(companyDiscounts[i] == code){
            returnArray[0] = companyDiscounts[i+1];
            returnArray[1] = companyDiscounts[i+2];
            returnArray[2] = companyDiscounts[i+3];
            break;
        }
    }
    return returnArray;
}

// Generates the three main fields depending on the company and the field number
function completeText (id, values, company_id){
    var headlines = "";
    var input_fields = "";

    //the order for the values are = frequentRenter|discountNumber|promoCode
    if (values[0] != 'null'){
        headlines += "<td>" + values[0] + "</td>";
        input_fields += "<td><input ID=\"frequentRenter" + id + "\" NAME=\"frequentRenter" + id + "\" type=\"text\" onfocus=\"change('frequentRenter" + id + "','focused');\" onblur=\"change('frequentRenter" + id + "','UNfocused');\" style=\"width:90px\"></td>";
    }

    if (values[1] != 'null'){
        var code = searchDiscountCode(company_id);
        headlines += "<td>" + values[1] + "</td>";
        input_fields += "<td><input ID=\"discountNumber" + id + "\" NAME=\"discountNumber" + id + "\" type=\"text\" onfocus=\"change('discountNumber" + id + "','focused');\" onblur=\"change('discountNumber" + id + "','UNfocused');\" value = \"" + code + "\" style=\"width:90px\"></td>";
    }

    if (values[2] != 'null'){
        headlines += "<td>" + values[2] + "</td>";
        input_fields += "<td><input ID=\"promoCode" + id + "\" NAME=\"promoCode" + id + "\" type=\"text\" onfocus=\"change('promoCode" + id + "','focused');\" onblur=\"change('promoCode" + id + "','UNfocused');\" style=\"width:90px\"></td>";
    }

    var main_text = "<table><tr>" + headlines + "</tr><tr>" + input_fields + "</tr></table>";

    return main_text;
}
// Clear all or a specific field id
function clearValuesDiv(id){

    if (id){
        document.getElementById('providerFields' + id).innerHTML = "";
        document.getElementById('discountProvider' + id).value = "";
    }else{
        for (var i = 1; i<=5; i++){
            document.getElementById('providerFields' + i).innerHTML = "";
            document.getElementById('discountProvider' + i).value = "";
        }
        document.getElementById('discounts_rental').innerHTML = "";
    }
}
// Fills the discount code field depending on the company id 
function searchDiscountCode(company_id){
    for (var i=0; i<corporateDiscountCodes.length; i = i + 2){
        if (corporateDiscountCodes[i] == company_id){
            return corporateDiscountCodes[i+1];
        }
    }
    return "";
}

// Completes the fields with the data from the ABO
function preselectDiscountCompanies(){
    var count = 1;
    for (var i=0; i<corporateDiscountCodes.length; i = i + 2){
        document.getElementById("discountProvider" + count).value = corporateDiscountCodes[i];
        completeFields(count, corporateDiscountCodes[i]);
        count++;
    }
    return 0;
}
