/* A pop up to confirm that the user indeed wants to delete! */
function confirmDelete(pk,name,page,args){
   reply = confirm( "Are you sure you want to delete " + name + " ?", "OK" );
   if(reply){
      window.location = page + "?delete=" + pk + args;
   }
}

function clearfield(element){
   if(element.value == 'yyyy'){element.value = "";}
   if(element.value == 'mm/dd/yyyy'){element.value = "";}
   if(element.value == '(###) ###-####'){element.value = "";}
}

function isempty(oField,cMessage){
   var pattern = /\S/ ;
   if ( ! pattern.test(oField.value ) ) {
      alert(cMessage);
      oField.focus();
      return true
   }
   return false;
}

function isPhoneNumberBad(oField,cMessage){
   // Check for correct phone number
   rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
   if (!rePhoneNumber.test(oField.value)) {
      alert(cMessage);
      oField.focus();
      return true;
   }
   return false;
}

function isUrlBad(oField,cMessage){
   var expr=/^http:\/\/\S+$/;
   if (!expr.test(oField.value)) {
      alert(cMessage);
      oField.focus();
      return true;
   }
   return false;
}

function isFaxNumberBad(oField,cMessage){
   // Check for correct fax number
   rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
   var pattern = /\S/ ;
   if (pattern.test(oField.value) ) {
      if (!rePhoneNumber.test(oField.value)) {
         alert(cMessage);
         oField.focus();
         return true;
      }
   }
   return false;
}

function isZipNumberBad(oField,cMessage){
   // Check for correct phone number
   reZipNumber = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
   if (!reZipNumber.test(oField.value)) {
      alert(cMessage);
      oField.focus();
      return true;
   }
   return false;
}

function isYearBad(oField,cMessage){
   var expr = /(^\d{4}$)/;
   if (!expr.test(oField.value)){
      alert(cMessage);
      oField.focus();
      return true;
   }
   return false;
}

function isCurrencyBad(oField,cMessage){
   var expr = /(^\d*$)|(^\d+\.\d{1,2}$)/;
   if (!expr.test(oField.value)){
      alert(cMessage);
      oField.focus();
      return true;
   }
   return false;
}

function isDateBad(oField,cMessage) {
   //check to see if in correct format
   var objRegExp = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
   if (!objRegExp.test(oField.value) ) {
      alert(cMessage);
      oField.focus();
      return true;
   }else{
      var strSeparator = oField.value.substring(2,3)
      var arrayDate = oField.value.split(strSeparator);
      //create a lookup for months not equal to Feb.
      var arrayLookup = { '01' : 31,'03' : 31,
                          '04' : 30,'05' : 31,
                          '06' : 30,'07' : 31,
                          '08' : 31,'09' : 30,
                          '10' : 31,'11' : 30,'12' : 31}
      var intDay = parseInt(arrayDate[1],10);

      //check if month value and day value agree
      if(arrayLookup[arrayDate[0]] != null) {
         if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0){
            return false; //found in lookup table, good date
         }
      }
   }

   //check for February
   var intMonth = parseInt(arrayDate[0],10);
   if (intMonth == 2) {
      var intYear = parseInt(arrayDate[2]);
      if (intDay > 0 && intDay < 29) {
         return false;
      } else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) ||
             (intYear % 400 == 0)) {
            // year div by 4 and ((not div by 100) or div by 400) ->ok
            return false;
         }
      }
   }
   alert(cMessage);
   oField.focus();
   return true;
}

function eMailcheck(oField,cMessage) {
   var at="@";
   var dot=".";
   var lat=oField.value.indexOf(at);
   var lstr=oField.value.length;
   var ldot=oField.value.indexOf(dot);
   if ((oField.value.indexOf(at)==-1) || (oField.value.indexOf(at)==-1
      || oField.value.indexOf(at)==0 || oField.value.indexOf(at)==lstr)
      || (oField.value.indexOf(dot)==-1 || oField.value.indexOf(dot)==0
      || oField.value.indexOf(dot)==lstr) || (oField.value.indexOf(at,(lat+1))!=-1)
      || (oField.value.substring(lat-1,lat)==dot || oField.value.substring(lat+1,lat+2)==dot)
      || (oField.value.indexOf(dot,(lat+2))==-1) || (oField.value.indexOf(" ")!=-1)){
      alert(cMessage);
      oField.focus();
      return true;
   }
   return false;
}

function validate_member_info(callingForm){
   if (isempty(callingForm.username,"Log In Name is a required field.")){
      return false}
   if (isempty(callingForm.password,"Pasword is a required field.")){
      return false}
   if (isempty(callingForm.password2,"Pasword (confirmation) is a required field and should match the previous field.")){
      return false}
   if (callingForm.password.value != callingForm.password2.value){
      alert("The Password fields do not match.");
      callingForm.password2.focus();
      return false}
   if (isempty(callingForm.fname,"First Name is a required field.")){
      return false}
   if (isempty(callingForm.lname,"Last Name is a required field.")){
      return false}
   if (isempty(callingForm.org,"Organization is a required field.")){
      return false}
   if (isempty(callingForm.add1,"Address 1 is a required field.")){
      return false}
   if (isempty(callingForm.city,"City is a required field.")){
      return false}
   if (isempty(callingForm.state,"State is a required field.")){
      return false}
   if (isempty(callingForm.zip,"Zip is a required field.")){
      return false}
    if (isZipNumberBad(callingForm.zip,"Zip Code Is Not Valid")){
      return false}
  if (isempty(callingForm.phone,"Phone is a required field.")){
      return false}
   if (isPhoneNumberBad(callingForm.phone,"Phone Number must be entered as: (555) 555-1234")){
      return false}
   if (isFaxNumberBad(callingForm.fax,"Fax Number must be entered in the same format as phone number or left blank.")){
      return false}
   if (isempty(callingForm.email,"Email is a required field.")){
      return false}
   if (eMailcheck(callingForm.email,"The Email Address is not valid.")){
      return false}
   return true;
}

function validate_contact_form(callingForm){
   if (isempty(callingForm.cname,"Your name is a required field.")){
      return false}
   if (isempty(callingForm.eaddress,"Your email is a required field.")){
      return false}
   if (eMailcheck(callingForm.eaddress,"The Email Address is not valid.")){
      return false}
   if (isempty(callingForm.message,"Your comment is a required field.")){
      return false}
   return true;
}

/* For the proposal form */
function validate_pro_info(callingForm){
   if (isempty(callingForm.presenter_name,"Presenter name(s) is a required field.")){
      return false}
   if (isempty(callingForm.presenter_title,"Presenter's title(s) is a required field.")){
      return false}
   if (isempty(callingForm.orginization,"Organization/Company is a required field.")){
      return false}
   if (isempty(callingForm.address,"Address is a required field.")){
      return false}
   if (isempty(callingForm.city_state_zip,"City, State, Zip is a required field.")){
      return false}
   if (eMailcheck(callingForm.email,"The Email Address field is not valid.")){
      return false}
   if (isPhoneNumberBad(callingForm.phone,"Phone Number must be entered as: (555) 555-1234")){
      return false}
   if (isempty(callingForm.session_title,"Session Title is a required field.")){
      return false}
   if (isempty(callingForm.session_description,"Session Description is a required field.")){
      return false}
   if (isempty(callingForm.learning_outcome,"A list of learning outcomes is a required field.")){
      return false}
   if (callingForm.tech_other.checked){
      if (isempty(callingForm.other_specified,"Please specify your \"Other\" Audio, Visual, Technical Needs.")){
         return false}
   }
   return true;
}

/* For the list serve form */
function validate_mailserve_form(callingForm){
   if (isempty(callingForm.subject,"Subject is a required field.")){
      return false}
   if (isempty(callingForm.message,"Message is a required field.")){
      return false}
   return true;
}

/*For the Call For Presentation page, enables a field*/
function os_enable(callingForm){
   if (callingForm.checked){
      document.getElementById("other_specified").disabled = false;
      document.getElementById("other_specified").focus();
   }else{
      document.getElementById("other_specified").disabled = true;
   }
   return true;
}

/*
For the Members administration page when a user submits a request
for a export of data.  We need to disable any other form submissions
untill the user is done exportin.
*/
function memform_off(){
   document.getElementById("edit").disabled = true;
   document.getElementById("searchfor").disabled = true;
   document.getElementById("find").disabled = true;
   document.getElementById("reset").disabled = true;
   document.getElementById("orderby").disabled = true;
   document.getElementById("mem_list").style.display='none';
   document.getElementById("exported").style.visibility ='visible';
}

/*
For the Conferences administration page when a user submits a request
for a export of data.  We need to disable any other form submissions
untill the user is done exportin.
*/
function conform_off(){
   document.getElementById("con_list").style.display='none';
   document.getElementById("exported").style.visibility ='visible';
}

/* For the conference edit form */
function validate_conference(callingForm){
   if (isempty(callingForm.conf_date,"Please enter a value in the \"Conference Dates\" field!")){
      return false}
   if (isempty(callingForm.conf_theme,"Please enter a value in the \"Confernece Theme\" field!")){
      return false}
   if (isDateBad(callingForm.early_reg ,"The format for the \"Early Reg Deadline\" field is incorrect \n(Ex. mm/dd/yyyy).")){
      return false}
   if (isCurrencyBad(callingForm.early_price,"Please enter a valid currency in the \"Early Reg Price\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isCurrencyBad(callingForm.reg_price,"Please enter a valid currency in the \"Regular Reg Price\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isCurrencyBad(callingForm.grp_price,"Please enter a valid currency in the \"Group Reg Price\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isempty(callingForm.loc_title,"Please enter a value in the \"Location Title\" field!")){
      return false}
   if (isempty(callingForm.loc_add1,"Please enter a value in the \"Location Address\" field!")){
      return false}
   if (isempty(callingForm.loc_city,"Please enter a value in the \"Location City\" field!")){
      return false}
   if (isempty(callingForm.loc_state,"Please enter a value in the \"Location State\" field!")){
      return false}
   if (isZipNumberBad(callingForm.loc_zip ,"Please enter a valid Zip Code in the \"Location Zip Code\" field!")){
      return false}
   if (isPhoneNumberBad(callingForm.loc_phone  ,"The format for the \"Location Phone\" field is incorrect \n(Ex. (406)123-1234).")){
      return false}
   if (isUrlBad(callingForm.loc_link  ,"The format for the \"Location Link\" field is incorrect \n(Ex. http:\\\\www.mydomain.com).")){
      return false}
   if (isCurrencyBad(callingForm.room_rate ,"Please enter a valid currency in the \"Room Rate\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isCurrencyBad(callingForm.meal1_price ,"Please enter a valid currency in the \"Guest Meal Option 1\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isCurrencyBad(callingForm.meal2_price ,"Please enter a valid currency in the \"Guest Meal Option 2\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isCurrencyBad(callingForm.meal3_price ,"Please enter a valid currency in the \"Guest Meal Option 3\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isCurrencyBad(callingForm.meal4_price ,"Please enter a valid currency in the \"Guest Meal Option 4\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if (isCurrencyBad(callingForm.meal5_price ,"Please enter a valid currency in the \"Guest Meal Option 5\" field.\n(Ex. 1.23 or just 1)")){
      return false}
   if(!callingForm.meal1_banquet.checked &&
      !callingForm.meal2_banquet.checked &&
      !callingForm.meal3_banquet.checked &&
      !callingForm.meal4_banquet.checked &&
      !callingForm.meal5_banquet.checked){
      alert("Please check at least one 'Guest Meal Option' as the banquet.");
      return false}
}

/* For the document edit form */
function validate_doc(callingForm){
   if (isempty(callingForm.title,"Please enter a value in the \"Title\" field!")){
      return false}
}

/* For the newsletter edit form */
function validate_nl(callingForm){
   if (isempty(callingForm.title,"Please enter a value in the \"Title\" field!")){
      return false}
   if (isempty(callingForm.date,"Please enter a value (mm/dd/yyyy) in the \"Date\" field!")){
      return false}
   if (isDateBad(callingForm.date ,"The format for the \"Date\" field is incorrect \n(Ex. mm/dd/yyyy).")){
      return false}
}

/* For the members document edit form */
/* And the gallery document edit form */
function validate_dl(callingForm){
   if (isempty(callingForm.title,"Please enter a value in the \"Title\" field!")){
      return false}
}

/* For the job posting edit form */
function validate_jp(callingForm){
   if (isempty(callingForm.title,"Please enter a value in the \"Title\" field!")){
      return false}
   if (isempty(callingForm.close_date,"Please enter a value (mm/dd/yyyy) in the \"Date\" field!")){
      return false}
   if (isDateBad(callingForm.close_date ,"The format for the \"Date\" field is incorrect \n(Ex. mm/dd/yyyy).")){
      return false}
   if (isempty(callingForm.contact,"Please enter a value in the \"Contact\" field!")){
      return false}
   if (isempty(callingForm.contact_email,"Contact Email is a required field.")){
      return false}
   if (eMailcheck(callingForm.contact_email,"The Contact Email Address is not valid.")){
      return false}
   if (isempty(callingForm.description,"Please enter a description in the \"Description\" field!")){
      return false}
}

/* For the sponsors edit form */
function validate_spon(callingForm){
   if (isempty(callingForm.name,"Please enter a value in the \"Name\" field!")){
      return false}
   if (isempty(callingForm.web_link,"Please enter a description in the \"Web Link\" field!")){
      return false}
   if (isUrlBad(callingForm.web_link  ,"The format for the \"Web Link\" field is incorrect \n(Ex. http:\\\\www.mydomain.com).")){
      return false}
}

/* For the committee edit form */
function validate_com(callingForm){
   if (isempty(callingForm.name,"Please enter a value in the \"Name\" field!")){
      return false}
   if (isempty(callingForm.descript,"Please enter a value in the \"Description\" field!")){
      return false}
}

/* For the committee interest form */
function validate_int(callingForm){
   if (isempty(callingForm.name,"Please enter a value in the \"Name\" field!")){
      return false}
}

/* For the contact us admin form */
function validate_contact(callingForm){
   if (isempty(callingForm.contact_to,"Please enter a value in the \"Our Name\" field!")){
      return false}
   if (isempty(callingForm.contact_email,"Please enter a value in the \"Our Address\" field!")){
      return false}
   if (eMailcheck(callingForm.contact_email,"The Email Address is not valid.")){
      return false}
   if (isempty(callingForm.contact_subject,"Please enter a value in the \"Subject Line\" field!")){
      return false}
}

/*Collaps and hide onbjects for use on the registration.php page*/
function dropControl(display_obj,control_obj){
   var display = document.getElementById(display_obj);
   var control = document.getElementById(control_obj);
   if (control.checked){
      display.style.display = '';
      control.checked=true;
   }else{
      display.style.display = 'none';
      control.checked=false;
   }
}

/*For use on the registration.php page*/
function disableDiet_other(callingobj,objtochange){
   var thisobj = document.getElementById(callingobj);
   var thatobj = document.getElementById(objtochange);
   if (thisobj.value == "other" ){
      thatobj.disabled = false;
      thatobj.focus();
   }else{
      thatobj.disabled = true;
      thatobj.value = "";
   }
}

/*Uncheck all for use on the admin_conference.php page*/
function uncheckBans(obj1,obj2,obj3,obj4){
   document.getElementById(obj1).checked=false;
   document.getElementById(obj2).checked=false;
   document.getElementById(obj3).checked=false;
   document.getElementById(obj4).checked=false;
}

/*Collaps and hide onbjects for use on the registration.php page*/
function dropMeal(display_obj,control_obj){
   var display = document.getElementById(display_obj);
   var control = document.getElementById(control_obj);
   display.style.display = '';
   if (control.value != ''){
      display.style.display = '';
   }else{
      display.style.display = 'none';
   }
}

/*Checks to make sure they selected a banquet meal on the registration.php pag*/
function check_bm(obj, bnqt_sel_id, meal_sel_id){
   if (meal_sel_id == ''){
      if (isempty(eval('obj.bnqt_meal'+bnqt_sel_id),
                  "Please make sure all your \"Banquet Meals\" are selected!")){
         return true;
      }
   }else{
      var banq_obj = document.getElementById(meal_sel_id);
      if(banq_obj.value != ''){
         if (isempty(eval('obj.bnqt_meal'+bnqt_sel_id),
                     "You have selected "+banq_obj.value+" for a guest so you must make a \"Banquet\" Meal selection")){
            return true;
         }
      }
   }
   return false;
}
/* For the py pal form */
function validate_paypal(callingForm){
if (isempty(callingForm.firstName,"Please enter a value in the \"First Name\" field!")){
      return false}
   if (isempty(callingForm.lastName,"Please enter a value in the \"Last Name\" field!")){
      return false}
   if (isempty(callingForm.creditCardNumber,"Please enter a value in the \"Card Number\" field!")){
      return false}
   if (isempty(callingForm.address1,"Please enter a value in the \"Address 1\" field!")){
      return false}
   if (isempty(callingForm.city,"Please enter a value in the \"City\" field!")){
      return false}
   if (isempty(callingForm.zip,"Please enter a value in the \"Zip\" field!")){
      return false}
   if (isZipNumberBad(callingForm.zip,"Zip Code Is Not Valid")){
      return false}
   if (isempty(callingForm.cvv2Number,"Please enter a value in the \"CVV Code\" field!")){
      return false}
}
/* For the invoice admin form */
function validate_inv(callingForm){
   if (isempty(callingForm.new_bal,"Please enter a value in the \"Invoice Balance\" field!")){
      return false}
   if (isCurrencyBad(callingForm.new_bal,"Please enter a valid currency in the \"Invoice Balance\" field.\n(Ex. 1.23 or just 1)")){
      return false}
}
/* Conference Registration, Sponsorship Area */
function val_spon_extra(){
   var spon           = document.getElementById("rtypes");
   var trad           = document.getElementById("rtypet");
   var plat_radio     = document.getElementById("platspontype");
   var gold_radio     = document.getElementById("gldspontype");
   var sil_radio      = document.getElementById("silspontype");
   var exh_radio      = document.getElementById("exhspontype");
   var free_mem_name  = document.getElementById("fm_name");
   var free_mem_email = document.getElementById("fm_email");
   var free_mem_phone = document.getElementById("fm_phone");
   var gold_radio     = document.getElementById("gldspontype");
   var event_name     = document.getElementById("fs_event");
   var underwriter    = document.getElementById("fs_undr");
   var exh_service    = document.getElementById("exh_service");
   var exh_require    = document.getElementById("exh_require");
   var tre_radio      = document.getElementById("rtone");
   var trr_radio      = document.getElementById("rttwo");
   var trg_radio      = document.getElementById("grp");
   var t_atg          = document.getElementById("add2group");
   if(spon.checked){
      if(plat_radio.checked){
         if (isempty(free_mem_name,"Please enter a value in the \"Name\" field under \"Free Membership Details\"!")){
            return false}
         if (isempty(free_mem_email,"Please enter a value in the \"Email\" field under \"Free Membership Details\"!")){
            return false}
         if (eMailcheck(free_mem_email,"The Email Address in  \"Free Membership Details\" is not valid!")){
            return false}
         if (isempty(free_mem_phone,"Please enter a value in the \"Phone\" field under \"Free Membership Details\"!")){
            return false}
         if (isPhoneNumberBad(free_mem_phone  ,"The format for the \"Phone\" field under \"Free Membership Details\" is incorrect \n(Ex. (406)123-1234).")){
            return false}
      }
      if(gold_radio.checked){
         if (isempty(event_name,"Please select a value in the \"Event\" drop down under \"Event Sponsorship\"!")){
            return false}
      }
      if(sil_radio.checked){
         if (isempty(underwriter,"Please select a value in the \"Event\" drop down under \"Event Sponsorship\"!")){
            return false}
      }
      if(exh_radio.checked){
         if (isempty(exh_service,"Please enter a value in the \"Describe Product/Services\" field down under \"Event Exhibitor\"!")){
            return false}
      }
      if(!plat_radio.checked && !gold_radio.checked && !sil_radio.checked && !exh_radio.checked){
         alert("You must select one of the sponsorship/exhibitor options.");
         return false;
      }
   }else{
      if(!tre_radio.checked && !trr_radio.checked && !trg_radio.checked){
         alert("You must select one of the traditional registration options.");
         return false;
      }
      if(trg_radio.checked && t_atg.value == ''){
         alert("Please select a group size.");
         t_atg.focus();
         return false;
      }
   }
   return true;
}
/* For the invoice admin form */
function validate_exh(callingForm){
   if (isempty(callingForm.contact_name,"Please enter a value in the \"Contact Name\" field!")){
      return false}
   if (isempty(callingForm.telephone,"Please enter a value in the \"Telephone\" field!")){
      return false}
   if (isPhoneNumberBad(callingForm.telephone,"Phone Number must be entered as: (555) 555-1234")){
      return false}
   if (isFaxNumberBad(callingForm.fax,"Fax Number must be entered in the same format as phone number or left blank.")){
      return false}
   if (eMailcheck(callingForm.email,"A Email Address is not valid.")){
      return false}
   if (isempty(callingForm.fm_name,"Please enter a value in the \"Free Member Name\" field!")){
      return false}
   if (eMailcheck(callingForm.fm_email,"A Email Address is not valid.")){
      return false}
   if (isPhoneNumberBad(callingForm.fm_phone,"Phone Numbers must be entered as: (555) 555-1234")){
      return false}
}
/* For the call for proposal form */
function validate_cfp(callingForm){
   if (eMailcheck(callingForm.prop_email,"The Email Address is not a format.")){
      return false}
   if (isDateBad(callingForm.prop_deadline ,"The format for the \"Deadline\" field is incorrect \n(Ex. mm/dd/yyyy).")){
      return false}
}

/*Collaps and hide onbjects for use on the registration.php page*/
function sponDropControl(toDrop,clearOne,clearTwo,clearThree,clearFour){
   var c1 = document.getElementById(clearOne);
   var c2 = document.getElementById(clearTwo);
   var c3 = document.getElementById(clearThree);
   if(toDrop != ''){
      var drop = document.getElementById(toDrop);
      drop.style.display = '';
   }
   if(clearFour != ''){
      var c4 = document.getElementById(clearFour);
      c4.style.display = 'none';
   }
   c1.style.display = 'none';
   c2.style.display = 'none';
   c3.style.display = 'none';
}

/* For the call for admin sponsor options form */
function validate_sof(callingForm){
   if (isCurrencyBad(callingForm.spon_p_prc,"The 'Price' for Platinum is incorrectly formatted.")){
      return false}
   if (isCurrencyBad(callingForm.spon_g_prc,"The 'Price' for Gold is incorrectly formatted.")){
      return false}
   if (isCurrencyBad(callingForm.spon_s_prc,"The 'Price' for Silver is incorrectly formatted.")){
      return false}
   if (isCurrencyBad(callingForm.spon_e_prc,"The 'Price' for Exhibitor is incorrectly formatted.")){
      return false}
   if (isCurrencyBad(callingForm.spon_p_mems,"The 'Memberships' for Platinum is incorrectly formatted.")){
      return false}
   if (isCurrencyBad(callingForm.spon_g_mems,"The 'Memberships' for Gold is incorrectly formatted.")){
      return false}
   if (isCurrencyBad(callingForm.spon_s_mems,"The 'Memberships' for Silver is incorrectly formatted.")){
      return false}
   if (isCurrencyBad(callingForm.spon_e_mems,"The 'Memberships' for Exhibitor is incorrectly formatted.")){
      return false}
}
