
/****************
** ADMIN FORMS ** 
*****************/
function validateAdminIslandForm(theForm) {
var reason = "";

  reason += validateEmpty(theForm.island_name);
  reason += validateEmpty(theForm.island_mapid);
  reason += validateURL(theForm.island_website,'N');
  reason += validateNumeric(theForm.island_latitude,'N');
  reason += validateNumeric(theForm.island_longitude,'N');
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateAdminAirportForm(theForm) {
var reason = "";

  reason += validateEmpty(theForm.airport_name);
  reason += validateEmpty(theForm.airport_code);
  reason += validateEmpty(theForm.airport_desc);
  reason += validateEmpty(theForm.airport_runwaydesc);
  reason += validateURL(theForm.airport_website,'N');
  reason += validateEmpty(theForm.airport_islandid);
  reason += validateNumeric(theForm.airport_latitude,'Y');
  reason += validateNumeric(theForm.airport_longitude,'Y');
  reason += validatePhone(theForm.airport_phonetollfree,'N');
  reason += validatePhone(theForm.airport_phonelocal,'N');
  reason += validatePhone(theForm.airport_fax,'N');
  reason += validateEmail(theForm.airport_email,'N');
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateAdminFeatureForm(theForm) {
var reason = "";

  reason += validateEmpty(theForm.feature_name);
  reason += validateEmpty(theForm.feature_desc);
  reason += validateURL(theForm.feature_website,'N');
  reason += validateEmpty(theForm.feature_typeid);
  reason += validateEmpty(theForm.feature_islandid);
  reason += validateNumeric(theForm.feature_latitude,'Y');
  reason += validateNumeric(theForm.feature_longitude,'Y');
  reason += validatePhone(theForm.feature_phonetollfree,'N');
  reason += validatePhone(theForm.feature_phonelocal,'N');
  reason += validatePhone(theForm.feature_fax,'N');
  reason += validateEmail(theForm.feature_email,'N');
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateAdminFormsForm(theForm) {
var reason = "";

  reason += validateEmpty(theForm.form_name);
  reason += validateEmpty(theForm.form_desc);
  reason += validateEmpty(theForm.form_filetype);
  reason += validateEmpty(theForm.form_file);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateAdminFuelPriceForm(theForm) {
var reason = "";

  reason += validateNumeric(theForm.price_100ll,'Y');
  reason += validateDate(theForm.pricedate_100ll,'N');
  reason += validateNumeric(theForm.price_jeta,'Y');
  reason += validateDate(theForm.pricedate_jeta,'N');
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateAdminFuelPricingEditForm(theForm) {
var reason = "";

  reason += validateNumeric(theForm.price,'Y');
  reason += validateDate(theForm.price_date,'Y');
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateAdminFuelPricingForm(theForm) {
var reason = "";

  reason += validateEmpty(theForm.airport_id);
  reason += validateRadioBtn(theForm.fuel_suppliedby);
  
  radFuelSupplyVal = getCheckedValue(theForm.elements['fuel_suppliedby']);
  
  //validate airport fields
  if(radFuelSupplyVal == "airport"){
	  
	  airport_100ll = getCheckedValue(document.forms['frmFuelPrice'].elements['airport_fuel_100ll']);
	  airport_jeta = getCheckedValue(document.forms['frmFuelPrice'].elements['airport_fuel_jeta']);
	  
	  if(airport_100ll == "" && airport_jeta == ""){
		  reason += "Please select the airport pricing to be entered.";
	  }else if(airport_100ll == "on" && airport_jeta == ""){
		  reason += validateNumeric(theForm.airport_fuel_price_100ll,'Y');
		  reason += validateDate(theForm.airport_fuel_price_100ll_date,'Y');
	  }else if(airport_100ll == "" && airport_jeta == "on"){
		  reason += validateNumeric(theForm.airport_fuel_price_jeta,'Y');
		  reason += validateDate(theForm.airport_fuel_price_jeta_date,'Y');
	  }else if(airport_100ll == "on" && airport_jeta == "on"){
		  reason += validateNumeric(theForm.airport_fuel_price_100ll,'Y');
		  reason += validateDate(theForm.airport_fuel_price_100ll_date,'Y');
		  reason += validateNumeric(theForm.airport_fuel_price_jeta,'Y');
		  reason += validateDate(theForm.airport_fuel_price_jeta_date,'Y');
	  }
  }
  
  //validate supplier fields
  if(radFuelSupplyVal == "supplier"){
	  
	  reason += validateEmpty(theForm.supplier_1_id);
	  
	  supplier1_100ll = getCheckedValue(document.forms['frmFuelPrice'].elements['supplier_1_fuel_100ll']);
	  supplier1_jeta = getCheckedValue(document.forms['frmFuelPrice'].elements['supplier_1_fuel_jeta']);
	  radSupplier2 = getCheckedValue(document.forms['frmFuelPrice'].elements['supplier_2_use']);
	  
	  if(supplier1_100ll == "" && supplier1_jeta == ""){
		  reason += "Please select the supplier 1 pricing to be entered.";
	  }else if(supplier1_100ll == "on" && supplier1_jeta == ""){
		  reason += validateNumeric(theForm.supplier_1_fuel_price_100ll,'Y');
		  reason += validateDate(theForm.supplier_1_fuel_price_100ll_date,'Y');
	  }else if(supplier1_100ll == "" && supplier1_jeta == "on"){
		  reason += validateNumeric(theForm.supplier_1_fuel_price_jeta,'Y');
		  reason += validateDate(theForm.supplier_1_fuel_price_jeta_date,'Y');
	  }else if(supplier1_100ll == "on" && supplier1_jeta == "on"){
		  reason += validateNumeric(theForm.supplier_1_fuel_price_100ll,'Y');
		  reason += validateDate(theForm.supplier_1_fuel_price_100ll_date,'Y');
		  reason += validateNumeric(theForm.supplier_1_fuel_price_jeta,'Y');
		  reason += validateDate(theForm.supplier_1_fuel_price_jeta_date,'Y');
	  }
	  
	  //check supplier 2
	  if(radSupplier2 == "on"){
		  
		  supplier2_100ll = getCheckedValue(document.forms['frmFuelPrice'].elements['supplier_2_fuel_100ll']);
		  supplier2_jeta = getCheckedValue(document.forms['frmFuelPrice'].elements['supplier_2_fuel_jeta']);
		  
		  reason += validateEmpty(theForm.supplier_2_id);
		  
		  if(supplier2_100ll == "" && supplier2_jeta == ""){
		  	  reason += "Please select the supplier 2 pricing to be entered.";
		  }else if(supplier2_100ll == "on" && supplier2_jeta == ""){
			  reason += validateNumeric(theForm.supplier_2_fuel_price_100ll,'Y');
			  reason += validateDate(theForm.supplier_2_fuel_price_100ll_date,'Y');
		  }else if(supplier2_100ll == "" && supplier2_jeta == "on"){
			  reason += validateNumeric(theForm.supplier_2_fuel_price_jeta,'Y');
			  reason += validateDate(theForm.supplier_2_fuel_price_jeta_date,'Y');
		  }else if(supplier2_100ll == "on" && supplier2_jeta == "on"){
			  reason += validateNumeric(theForm.supplier_2_fuel_price_100ll,'Y');
			  reason += validateDate(theForm.supplier_2_fuel_price_100ll_date,'Y');
			  reason += validateNumeric(theForm.supplier_2_fuel_price_jeta,'Y');
			  reason += validateDate(theForm.supplier_2_fuel_price_jeta_date,'Y');
		  }
		  
	  }
	  
	  
  }
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

/****************
** WEBSITE FORMS ** 
*****************/

function validateContactForm(theForm) {
var reason = "";

  reason += validateEmpty(theForm.name);
  reason += validateEmail(theForm.email,'Y');
  reason += validateEmpty(theForm.comments);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateCurrentInfoForm(theForm) {
var reason = "";

  reason += validateEmpty(theForm.name);
  reason += validateEmail(theForm.email,'Y');
  reason += validateEmpty(theForm.info);
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

/****************
** FIELDS ** 
*****************/

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'gainsboro'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateRadioBtn(fld) {
    var error = "";
  	myOption = -1;
	
	for (i=fld.length-1; i > -1; i--) {
		if (fld[i].checked) {
			myOption = i; i = -1;
		}
	}

    if (myOption == -1) {
        error = "The required radio button has not been selected.\n"
    } else {
     	error = "";
    }
    return error;   
}

function validateCheckbox(fld) {
    var error = "";
  	myOption = -1;
	
	for (i=fld.length-1; i > -1; i--) {
		if (fld[i].checked) {
			myOption = i; i = -1;
		}
	}

    if (myOption == -1) {
        error = "The required checkbox has not been selected.\n"
    } else {
     	error = "";
    }
    return error;   
}


function trim(s){
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld, isReq) {
    var error="";
    var tfld = trim(fld.value); // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (isReq == "Y" && tfld == "") {
        fld.style.background = 'gainsboro';
        error = "You didn't enter an email address.\n";
    } else if (tfld != "" && !emailFilter.test(tfld)) { //test email for illegal characters
        fld.style.background = 'gainsboro';
        error = "Please enter a valid email address.\n";
    } else if (tfld != "" && fld.value.match(illegalChars)) {
        fld.style.background = 'gainsboro';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


function validatePhone(fld, isReq) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (isReq == "Y" && fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'gainsboro';
    } else if (fld.value != "" && isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'gainsboro';
    } else if (fld.value != "" && !(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'gainsboro';
    } 
    return error;
}

function validateURL(fld, isReq){
	var error = "";
	var tfld = trim(fld.value); 
	var urlFilter = /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/;
	
	if (isReq == "Y" && tfld == "") {
        fld.style.background = 'gainsboro';
        error = "You didn't enter a website url.\n";
    } else if (tfld != "" && !urlFilter.test(tfld)) {
        fld.style.background = 'gainsboro';
        error = "Please enter a valid full website url.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateNumeric(fld, isReq){
	var error = "";
	var tfld = trim(fld.value);
	
	if (isReq == "Y" && tfld == "") {
        fld.style.background = 'gainsboro';
        error = "You didn't enter a numeric value.\n";
    } else if (tfld != "" && isNaN(parseInt(tfld))) {
        fld.style.background = 'gainsboro';
        error = "Please enter a valid numeric value.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateDate(fld, isReq){
	var error = "";
	var tfld = trim(fld.value);
	var dateFilter = /^\d{2}\/\d{2}\/\d{4}$/;
	
	if (isReq == "Y" && tfld == "") {
        fld.style.background = 'gainsboro';
        error = "You didn't enter a date in mm/dd/yyy format.\n";
    } else if (tfld != "" && !dateFilter.test(tfld)) {
        fld.style.background = 'gainsboro';
        error = "Please enter a valid date in mm/dd/yyyy format.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateImageFileType(fld, isReq){
	var error = "";
	var tfld = trim(fld.value);
	var stripped = tfld.replace(/^\s|\s$/g, ''); 
	var imageFilter = /^[a-z][\w]+\.(gif|jpg|jpeg|png)$/;
	
	if (isReq == "Y" && tfld == "") {
        fld.style.background = 'gainsboro';
        error = "You didn't select a file to upload.\n";
    } else if (tfld != "" && !dateFilter.test(tfld)) {
        fld.style.background = 'gainsboro';
        error = "Please select a valid file type to upload.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}


