// Populate by William Martin, a simple script I created
// to take a value in one input box "ttltxt" and post
// the first 25 characters into another "item".  I do
// realize that this function needs to be standardized
// and robust enough for "drop and use" but this is v1
// and I must move on.  More work on the next time I use
// it. ~bill
function populate(item){
   if(document.getElementById("lnktxt").value == ''){
      var title = document.getElementById("ttltxt").value;
      item.value=title.substring(0,25);
   }
}

// Functions for the registration pages! ~bill
function fee_update_total(){
   // The New Selection!
   var newv = document.getElementById("mancost").value;
   // The span html field that displays in totals column!
   var mtotal = document.getElementById("mtotal");
   var newsetv = newv.split(",",1);
   mtotal.innerHTML = newsetv;
   // Old selection!
   var oldv = document.getElementById("oldvalue");
   var oldseltot = oldv.value;
   oldv.value = newsetv;
   // Old sub-total!
   var subtv = document.getElementById("subtotal");
   var oldsubtot = subtv.value;
   // Calculate new sub-total!
   var newseltot = newsetv;// New select total!
   var upsubtot = (parseFloat(oldsubtot)-parseFloat(oldseltot))+parseFloat(newseltot);
   // Update subtotal!
   var stotals = document.getElementById("subtot");
   stotals.innerHTML = formatCurrency(upsubtot);
   subtv.value = upsubtot;
   // Update total!
   var perdown = document.getElementById("pmtopt");
   var total = document.getElementById("tot");
   var topaybycard = parseFloat(upsubtot)*(parseFloat(perdown.value)/parseFloat(100));
   total.innerHTML = formatCurrency(topaybycard);
   document.getElementById("total").value = topaybycard;
   //alert("Website is currently experiencing technical issues "+topaybycard);
}

function opt_update_total(id,amt){
   // Old sub-total!
   var subtv = document.getElementById("subtotal");
   var oldsubtot = parseFloat(subtv.value);
   // Add || Subtract new value
   var thischeck = document.getElementById(id+"opt");
   var thisline = document.getElementById(id);
   if(thischeck.checked == true){
      var newsubtot = oldsubtot + parseFloat(amt);
      // Update Totals!
      thisline.innerHTML = formatCurrency(amt);
   }else{
      var newsubtot = oldsubtot - parseFloat(amt);
      // Update Totals!
      thisline.innerHTML = '';
   }
   // Update subtotal!
   var stotals = document.getElementById("subtot");
   stotals.innerHTML = formatCurrency(newsubtot);
   subtv.value = newsubtot;
   // Update total!
   var perdown = document.getElementById("pmtopt");
   var total = document.getElementById("tot");
   var topaybycard = parseFloat(newsubtot)*(parseFloat(perdown.value)/parseFloat(100));
   total.innerHTML = formatCurrency(topaybycard);
   document.getElementById("total").value = topaybycard;
   //alert("Website is currently experiencing technical issues "+topaybycard);
}

function formatCurrency(num) {
   num = num.toString().replace(/\$|\,/g,'');
   if(isNaN(num))
   num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if(cents<10)
   cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
   num = num.substring(0,num.length-(4*i+3))+','+
   num.substring(num.length-(4*i+3));
   return (((sign)?'':'-') + '$' + num + '.' + cents);
}

/* 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;
   }
}
/* Clears a field with instructional characters */
function clearfield(element){
   if(element.value == 'yyyy'){element.value = "";}
   if(element.value == 'mm/dd/yyyy'){element.value = "";}
   if(element.value == '(###) ###-####'){element.value = "";}
}

/* For the stats forms */
function isempty(oField,cMessage){
   var pattern = /\S/ ;
   if ( ! pattern.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;
}

/* For the stats edit forms */
function validate_statmain(callingForm){
   if (isempty(callingForm.year,"Please enter a value in the \"Year\" field!")){
      return false}
   if (isempty(callingForm.season,"Please enter a value in the \"Season\" field!")){
      return false}
}
function val_name_fld(callingForm){
   if (isempty(callingForm.name,"Please enter a value in the \"Name\" field!")){
      return false}
}
function val_game(callingForm){
   if (isempty(callingForm.ateam_key,"Please enter a value in the \"Away Team\" field!")){
      return false}
   if (isempty(callingForm.hteam_key,"Please enter a value in the \"Home Team\" field!")){
      return false}
}
