function reCalc() {
   coupon=document.getElementById("p_coupon").value;
   //alert ("got to recalc: coupon code= " + x);
   url = "process_online.php?coupon=" + coupon;
   //alert(url);
   document.location.href = url;
}  
	
function isNumeric(n) {
  //alert ("got to isnumeric: n=" + n);
  if(n*1==n)return true;
  else return false;
}  

function toggleDisplay(divID) {
     var item = document.getElementById(divID);
     //alert("got to unhide " + divID);
     if (item) {
       item.className=(item.className=='hidden')?'unhidden':'hidden';
     }
}

function check_sales(f) {
   var msg = "";
   var choice = false;

   //alert('got to check_sales');
   for (var i = 0; i<f.elements.length; i++) {
       if (f.elements[i].name.indexOf('qty') > 0) {
          if (f.elements[i].selectedIndex > 0) choice = true;
          //alert("check_sales:field is " + f.elements[i].name + " value=" + f.elements[i].selectedIndex.value);     
       }
   }
   if (choice == false) {
       msg += "- Please choose an item for purchase";
   }
   if (msg != "") alert(msg);
   return (msg == "");
}

function check_fulfillment(f) {
   // f=form; t=tour; s=staff
   var msg = "";
   var radio_choice = false;

   //alert('got to check_fulfillment');
   for (counter = 0; counter < f.p_option.length; counter++) {
       if (f.p_option[counter].checked) radio_choice = true; 
   }
   if (!radio_choice) {
       document.fulfill.b_continue.value = "Continue";
       msg += "- Please choose a fulfillment option ";
   }
   if (msg != "") alert(msg);
   return (msg == "");
}

function check_members(f) {
   var msg = "";
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var arr1 = new Array;

   //alert('got to check_members');
   for (var i = 0; i<f.elements.length; i++) {
       if (f.elements[i].value == "") msg = "- Please enter all values";
       if (f.elements[i].name.indexOf('email') > 0 && reg.test(f.elements[i].value) == false) msg += "- Please enter a valid email address.";
       if (f.elements[i].name.indexOf('tshirt') > 0 && f.elements[i].value == "SS") msg += "- Please choose T-shirt size";
       if (f.elements[i].name.indexOf('email') > 0 ) arr1.push(f.elements[i].value);
   }

   // check for email dups
   arr2 = arr1.slice(0); // copy array
   arr1.sort();
   for ( var i = 1; i < arr1.length; i++ ) {
       if ( arr1[i] === arr1[ i - 1 ] ) {
           arr1.splice( i--, 1 );
       }
   }
   var x = arr1.length;
   var y = arr2.length;
   //alert(x + " emails entered - x = " + x + " y = " + y);
   if (x < y)  msg += "- Please enter unique email addresses.";

   if (msg != "") alert(msg);
   return (msg == "");
}

function check_user_credit(f) {

	var msg = "";
        //alert("got to check_credit_form");
	
	//if ( f.p_ordertype.selectedIndex == 0 ) msg += "- Please choose the order type";
	if ( f.p_card_type.selectedIndex == 0 ) msg += "- Please choose the type of card ";
	if (f.p_email.value == "") msg += "- Please enter your email address ";
	if (f.p_fname.value == "") msg += "- Please enter your name ";
	if (f.p_lname.value == "") msg += "- Please enter your name ";
	if (f.p_street.value == "") msg += "- Please enter your address ";
	if (f.p_city.value == "") msg += "- Please enter your city ";
	if ( f.p_state.selectedIndex == 0 ) msg += "- Please enter your state ";
	if (f.p_zip.value == 0) msg += "- Please enter your zip ";
	if (f.p_phone.value == "") msg += "- Please enter your phone# ";
        var stripped = f.p_phone.value.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
        if (isNaN(parseInt(stripped))) msg += "The phone number contains illegal characters.";
        if (!(stripped.length == 10))  msg += "The phone number is the wrong length.  Make sure you included an area code.\n";

	if (f.p_card_name.value == "") msg += "- Please enter your name as it appears on the card. ";
	if (f.p_card_number.value == 0 || !isNumeric(f.p_card_number.value)) msg += "- Please enter your credit card# ";
	//if (f.p_card_cvv2.value == 0 || !isNumeric(f.p_card_cvv2.value)) msg += "- Please enter the 3-digit cvv2# on the back of your card";
	if (!isNumeric(f.p_card_cvv2.value)) msg += "- Please enter the 3-digit cvv2# on the back of your card";
	if ( f.p_card_exp_month.selectedIndex == 0 ) msg += "- Please enter the expiration month ";
	if ( f.p_card_exp_year.selectedIndex == 0 ) msg += "- Please enter the expiration year ";

        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if (reg.test(f.p_email.value) == false) {
           msg += "- Please enter a valid email address.";
        }

        // check card type match card number
        switch (f.p_card_number.value.substr(0,1)) {
        case '3': if (f.p_card_type.value != 'American Express') msg += "- ERROR: the card number is not valid for this card type. Please choose the correct card type.";
                  break; 
        case '4': if (f.p_card_type.value != 'Visa') msg += "- ERROR: the card number is not valid for this card type. Please choose the correct card type.";
                  break; 
        case '5': if (f.p_card_type.value != 'MasterCard') msg += "- ERROR: the card number is not valid for this card type. Please choose the correct card type.";
                  break;
        case '6': if (f.p_card_type.value != 'Discover') msg += "- ERROR: the card number is not valid for this card type. Please choose the correct card type.";
                  break;
        default: alert("ERROR: this card cannot be read properly by the card swiper. Please ask for a different card or enter the card information manually");
                  break;
        }
 
	if (msg != "") alert(msg);
	return (msg == "");

} // end of check_credit_form

function parse_credit(s) {

	var msg = "";
        var raw = "";
        var ccnum = "";
        var rawname = "";
        var fname = "";
        var lname = "";
        var expmo = 0;
        var expyr = 0;
	
        // parse CC string into fields
        //alert("s=" + s.name);
        raw = s.value;
        //alert("raw=" + raw);
        //alert("x=" + x);
        ccnum = raw.substring(2,raw.indexOf('^'));
        //alert("CC=" + ccnum);
        rawname = raw.substring(raw.indexOf('^')+1);
        //alert("rawname=" + rawname);
        expyr = rawname.substr(rawname.indexOf('^') +1, 2);
        expmo = rawname.substr(rawname.indexOf('^') +3, 2);
        //alert("expmo=" + expmo + " expyr=" + expyr);
        rawname = rawname.substring(0,rawname.indexOf('^'));
        fname = rawname.substring(rawname.indexOf('/') +1) 
        fname = fname.replace(/\s*$/, ""); // remove any blanks
        lname = rawname.substring(0,rawname.indexOf('/'));
        //alert("CC=" + ccnum + " Name=" + custname);
        document.credit.p_card_number.value = ccnum;
        document.credit.p_fname.value = fname;
        document.credit.p_lname.value = lname;
        document.credit.p_card_name.value = fname + " " + lname;
        document.credit.p_card_type.selectedIndex = 2;
        document.credit.p_card_exp_month.selectedIndex = expmo;
        for(var i = 0; i < document.credit.p_card_exp_year.length; i++) {
           if(document.credit.p_card_exp_year.options[i].value == expyr)
              document.credit.p_card_exp_year.selectedIndex = i;
        }
        document.getElementById('p_card_cvv2').focus();
	return;
}
        
function check_credit(f) {
	var msg = "";
        var name_array = document.credit.p_card_name.value.split(" ");
        //alert("array length=" + name_array.length);
        if (name_array.length == 2) {
           document.credit.p_fname.value = name_array[0];
           document.credit.p_lname.value = name_array[1];
        }
        else if (name_array.length == 3) {
           document.credit.p_fname.value = name_array[0] + ' ' + name_array[1];
           document.credit.p_lname.value = name_array[2];
        }

	if ( f.p_card_type.selectedIndex == 0 ) msg += "- Please choose the type of card ";
	if (f.p_card_name.value == "") msg += "- Please enter the customer name ";
	if (f.p_card_number.value == 0 ||
            !isNumeric(f.p_card_number.value)) msg += "- Please enter the credit card# ";
	if (f.p_card_cvv2.value == 0 ||
            !isNumeric(f.p_card_cvv2.value)) msg += "- Please enter the 3-digit verification # on the back of your card";
	if ( f.p_card_exp_month.selectedIndex == 0 ) msg += "- Please enter the expiration month ";
	if ( f.p_card_exp_year.selectedIndex == 0 ) msg += "- Please enter the expiration year ";

	if (msg != "") alert(msg);
	return (msg == "");

} // end of credit_form

function check_customer(f) {

        //alert("got to check_customer");
	var msg = "";
	
	if (f.p_fname.value == "") msg += "- Please enter your first name ";
	if (f.p_lname.value == "") msg += "- Please enter your last name ";
	if (f.p_street.value == "") msg += "- Please enter your address ";
	if (f.p_city.value == "") msg += "- Please enter your city ";
	if ( f.p_state.selectedIndex == 0 ) msg += "- Please enter your state ";
	if (f.p_zip.value == 0) msg += "- Please enter your zip ";
	if (f.p_phone.value == "") msg += "- Please enter your phone number ";
	if (f.p_pass1.value != f.p_pass2.value) msg += "- Passwords do not match";
	
	if (msg != "") alert(msg);
	return (msg == "");

} // end of check_customer_form


function nocc(show,n,ck) {
   if (n == "no") document.credit.p_card_name.value = 'POS customer';
   document.credit.p_card_number.value = '4242424242424242';
   document.credit.p_card_cvv2.value = '123'
   document.credit.p_card_exp_month.selectedIndex = 1;
   document.credit.p_card_exp_year.selectedIndex = 5;
   document.credit.p_card_type.selectedIndex = 1;
   if (ck == "check") {
      document.credit.p_checkno.style.display = show;
      document.credit.p_checkno.focus();
   }
   else document.credit.p_checkno.style.display = 'none';

   return true;
}

function reset_pay() {
     document.getElementById("p_card_name").value='';
     document.getElementById("p_card_type").selectedIndex=0;
     document.getElementById("p_card_number").value='';
     document.getElementById("p_card_exp_month").selectedIndex=0;
     document.getElementById("p_card_exp_year").selectedIndex=0;
     document.getElementById("p_card_cvv2").value='';
     document.credit.p_checkno.style.display = 'none';
}

function check_wb(f,d,q,a,w,c) {
   /*
    * f=field - use to clear if necessary
    * d=day of WB box
    * q=original qty 
    * a=available qty
    * w=# of wristbands
    * c=current day
    */
   var msg = "";
   var current = c;
   var gap = false;

   //alert("check_wb: day " + d + ": qty=" + q + " avail=" + a + " wb=" + w + " current day=" + c);
   var max_avail=1000; // ridiculously large for start

   if (parseInt(w) > parseInt(a)) {
       msg = "ERROR: Wristbands exceeds available tickets";
       f.value = '';
   }
   else {
      // check for gap in days
     for (var i = 0; i<document.willcall.elements.length; i++) {
       if (document.willcall.elements[i].name.indexOf('avail') > 0) {
          var x = parseInt(document.willcall.elements[i].name.substr(8));
          //alert("check_wb: field=" + document.willcall.elements[i].name + " x=" + x + " d=" + d + " current=" + current);
	  if (x > d) break;
	  else if (current != x) {
		  msg = "ERROR: Gap in days - can only give out wristband for days up to gap";
		  gap=true;
                  f.value = '';
		  break;
          }
	  else current = x+1;
       }
     }
   }

   if (gap === false) {
     for (var i = 0; i<document.willcall.elements.length; i++) {
       if (document.willcall.elements[i].name.indexOf('avail') > 0) {
          var x = parseInt(document.willcall.elements[i].name.substr(8));
          //alert("check_wb: field=" + document.willcall.elements[i].name + " x=" + x + " d=" + d);
          if (x > d) break;

          var avail = parseInt(document.willcall.elements[i].value);
          if (avail < max_avail) max_avail = avail;
          //alert("check_wb: value="+  " avail=" + avail + " max=" + max_avail);
       }
     } // for
   } // if gap

   if (w > max_avail) {
        msg = "ERROR: Maximum wristbands you can give is " + max_avail;
        f.value = '';
   }
   else if (gap === false) {
     for (var i = 0; i<document.willcall.elements.length; i++) {
         if ( (document.willcall.elements[i].name.indexOf('avail') > 0) &&
           (parseInt(document.willcall.elements[i].name.substr(8)) <= d) ) {
               if (parseInt(document.willcall.elements[i].value) >= max_avail) document.willcall.elements[i].value = parseInt(document.willcall.elements[i].value) - w;
               else document.willcall.elements[i].value = 0;
         }
     } // end of for 
   } // else

   if (msg != "") alert(msg);
   return (msg == "");
}

function check_willcall_form(f) {
   var wb=0;
   //alert("checking willcall form");
   for (var i = 0; i<document.willcall.elements.length; i++) {
       if ( f.elements[i].name.indexOf('wb') > 0) {
          if (f.elements[i].value != "") {
	     //alert(f.elements[i].name + "=" + f.elements[i].value);
	     wb=1;
          }
       }
   }
   if (wb === 0) {
      alert("ERROR: No wristbands entered");
      return false;
   }
   else return true;
}

function posbackfill(d,q) {
   var msg = "";
   //alert("posbackfill: disable other buttons");
   //alert("posbackfill: d=" + d + " q=" + q);
   if (d.indexOf('Park') > 0) return;
   //alert("posbackfill: day= " + dy + " qty=" + q);
   //document.pos.p_day.value = d;
   //document.pos.p_wbs.value = q;
   for (var i = 0; i<document.pos.elements.length; i++) {
       if (document.pos.elements[i].name.indexOf('qty') > 0) {
          var x = parseInt(document.pos.elements[i].name.substr(13));
          if (x < d) {
             //alert("posbackfill: field=" + document.pos.elements[i].name + " x =" + x);
             document.pos.elements[i].value = q;
          }
       }
     } // for

     for (var i = 0; i<document.pos.elements.length; i++) {
       if (document.pos.elements[i].name.indexOf('_qty_') > 0) {
          //alert("field:" + document.pos.elements[i].name);
          if (q != 0 && document.pos.elements[i].name != 'p_qty_kffday_' + d ) document.pos.elements[i].readOnly=true;
          else document.pos.elements[i].readOnly=false;
       }
     }
   if (msg != "") alert(msg);
   return (msg == "");
}

function plus_minus(sign,dayitem) {
   var x=0;
   x=parseInt(document.getElementById(dayitem).value);
   //alert('plus_minus: sign:' + sign + ' field:' + field + ' value=' + x);
   if (sign == "+") x++;
   else x--;
   //alert('plus_minus: new value = ' + x);
   if (x < 0) x = 0;
   document.getElementById(dayitem).value = x;

   // get day
   day = dayitem.substr(dayitem.lastIndexOf('_') +1);
   //alert('plus_minus: sign:' + sign + ' day:' + day);
   //posbackfill(day,x);

   return true;
}

function check_pos_sales(f) {
   var msg = "";
   var choice = false;

   //alert('got to check_sales');
   for (var i = 0; i<f.elements.length; i++) {
       if (f.elements[i].name.indexOf('qty') > 0) {
          if (f.elements[i].value > 0) choice = true;
       }
   }
   if (choice == false) {
       msg += "- Please choose an item for purchase";
   }
   if (msg != "") alert(msg);
   return (msg == "");
}

function check_refund(f) {

	var msg = "";
        //alert("got to check_refund");
	
	if (f.p_card_name.value == "") msg += "- Please enter name as it appears on the card";
	if (f.p_card_number.value == 0 || !isNumeric(f.p_card_number.value)) msg += "- Please enter credit card#";
	if (f.p_card_cvv2.value == 0 || !isNumeric(f.p_card_cvv2.value)) msg += "- Please enter the 3-digit cvv2# on the back of card";
	if ( f.p_card_exp_month.selectedIndex == 0 ) msg += "- Please enter the expiration month";
	if ( f.p_card_exp_year.selectedIndex == 0 ) msg += "- Please enter the expiration year";

        if (msg != "") alert(msg);
        return (msg == "");
}

function unhide(divID) {
     var item = document.getElementById(divID);
     //alert("got to unhide " + divID);
     if (item) item.className=(item.className=='hidden')?'unhidden':'hidden';
}



