
// generic form submission
function submitForm(form) {
  form.submit();
}

function processLicOption(type, index) {
 if (type == 1) {
    document.getElementById("daySelect_" + index).style.visibility='visible';
    document.getElementById("weekSelect_" + index).style.visibility='hidden';
    document.getElementById("monthSelect_" + index).style.visibility='hidden';
    document.getElementById("textSelect_" + index).style.visibility='visible';
  }
  else if (type == 2) {
    document.getElementById("daySelect_" + index).style.visibility='hidden';
    document.getElementById("weekSelect_" + index).style.visibility='visible';
    document.getElementById("monthSelect_" + index).style.visibility='hidden';
    document.getElementById("textSelect_" + index).style.visibility='visible';
  }
  else if (type == 3) {
    document.getElementById("daySelect_" + index).style.visibility='hidden';
    document.getElementById("weekSelect_" + index).style.visibility='hidden';
    document.getElementById("monthSelect_" + index).style.visibility='visible';    
    document.getElementById("textSelect_" + index).style.visibility='visible';
  }  
  else if (type == 4 || type == 5) {
    document.getElementById("daySelect_" + index).style.visibility='hidden';
    document.getElementById("weekSelect_" + index).style.visibility='hidden';
    document.getElementById("monthSelect_" + index).style.visibility='hidden';
    document.getElementById("textSelect_" + index).style.visibility='hidden';
  }  
}


function submitBuyNow(form) {
  var appsList = "";
  //alert(form.length);
  for (i=0;i<form.length;i++) {
    e = form.elements[i]; 
    if(e.type == "checkbox" && e.checked) {
      appsList += e.name + ",";
    }
  }

  //alert("appsList = " + appsList + ", action = " + form.action);

  if(appsList == "") {
    alert("No items selected for purchase");
    return;
  }
  appsList = appsList.substring(0, appsList.length -1); 
  form.appsList.value=appsList;
  //alert("form.appsList.value = " + form.appsList.value);
  form.submit();
}


function submitPurchase(form) {
  var appsList = "";
  //alert(form.length);
  for (i=0;i<form.length;i++) {
    e = form.elements[i]; 
    //alert("e.name = " + e.name);
    if(e.type == "checkbox" && e.name == "selectedApps" && e.checked) {
      appsList += e.value + ",";
    }
  }
  if(appsList == "") {
    alert("No items selected for purchase");
    return;
  }
  appsList = appsList.substring(0, appsList.length -1);
  //alert("appsList = " + appsList);
  form.appsList.value=appsList;

  form.submit();
}



function submitCancelPurchase(form, action) {
  form.action=action;
  form.submit();
}

function checkAddToCart(form)
{
  var appsList = "";

  for (counter=0;counter<form.length;counter++) {

    e = form.elements[counter]; 

    if(e.type == "checkbox" && e.checked) 
    {
      id = e.value;
      numUsers =  eval("form.numUsers_" + id + ".value");
      startDate = eval("form.startDate_" + id + ".value");
      appName = eval("form.appName_" + id + ".value");
      vendorName = eval("form.vendorName_" + id + ".value");
      
       //alert(numUsers + " : " + startDate + " : "+appName + ": " + vendorName);
       //if(numUsers <= 0)
       //alert(numUsers);
       if(!isNumeric(numUsers))
       {
         alert("Please enter valid number of users for application "+appName);
         return false;
       } else if(isNumeric(numUsers) && numUsers == 0)
       {
         alert("Please enter valid number of users for application "+appName);
         return false;       
       }
      
       if(startDate == '' || startDate.length == 0 || startDate == '0')
       {
         alert("Please select start date for application "+appName);
         return false;        
       }
      
      }
    }
    return true;
}


function submitAddToCart(form) {

  if(!checkAddToCart(form))
  {
    return false;
  } 
    
  var appsList = "";

  for (counter=0;counter<form.length;counter++) {

    e = form.elements[counter]; 

    if(e.type == "checkbox" && e.checked) {

      id = e.value;
      //alert("id = " + id);

      appIdArray = e.name.split("_");
      appId = appIdArray[1];
      // alert("appId = " + appId);

      appsList += id + ":" + appId + ":";

      licOption = eval("form.licOption_" + id);

      for (i = 0; i < licOption.length; i++) {
        if (licOption[i].checked) {

         // alert("licOption[" + i + "] = " + licOption[i].value + " checked");

          if(licOption[i].value  == "daily") {
           // alert("days: " + eval("form.days_" + id + ".value"));
            appsList +=  "daily:" + eval("form.days_" + id + ".value") + ":";
          }
          else if(licOption[i].value == "weekly") {
           // alert("weeks: " + eval("form.weeks_" + id + ".value"));
            appsList +=  "weekly:" + eval("form.weeks_" + id + ".value") + ":";
          }
          else if(licOption[i].value == "monthly") {
           // alert("monthly: " + eval("form.months_" + id + ".value"));
            appsList +=  "monthly:" + eval("form.months_" + id + ".value") + ":";
          }
          else if(licOption[i].value == "continuous") {
            appsList +=  "continuous:1:";   
          }
          else if(licOption[i].value == "metered") {
            appsList +=  "metered:1:";   
          }          
        }
      }
      appsList +=  eval("form.numUsers_" + id + ".value") + ":";      
      appsList +=  eval("form.startDate_" + id + ".value") + ":";
      //appsList += "0" + "|";
      appsList +=  eval("form.startHour_" + id + ".value") + "|";
      //alert(appsList);
    }
  }

  // alert("appsList = " + appsList);

  if(appsList == "") {
    alert("No items selected for purchase");
    return;
  }
  form.appsList.value=appsList;
  form.submit();
}

function openReceiptWindow(url, receipt)
{
   var fileNumber=Math.round(Math.random()*9999);
   ew=window.open(url + "?receiptId=" + receipt, 'rcptWindow'+fileNumber,'height=600,width=680, scrollbars=yes, resizable=yes');
   ew.focus();
}

function submitChangeStartDate(form, appRentalId, startDate, startHour, licOption, timeUnits) {
  //  alert("appRentalId = " + appRentalId + "\nstartDate = " + startDate + "\nstartHour = " + startHour + 
  //         "\n licOption = " + licOption+ "\n timeUnits = " + timeUnits);
  form.selectedAppRentalId.value=appRentalId;
  form.rentalStartDate.value=startDate;
  form.rentalStartHour.value=startHour;
  form.licOption.value=licOption;
  form.timeUnits.value=timeUnits;
  form.changeStartDate.value='true';  
  form.submit();
}

function endContinuous(form, appRentalId) {
  form.selectedAppRentalId.value=appRentalId;
  form.endContinuous.value='true';
  alert(form.selectedAppRentalId.value);
  form.submit();
}

function startSession(form, appRentalId, startDate, startHour, licOption, timeUnits, appName, isFirstUse) {
//  alert("appRentalId = " + appRentalId + "\nstartDate = " + startDate + "\nstartHour = " + startHour + "\n licOption = " + licOption+ "\n timeUnits = " + timeUnits + "\nisFirstUse = " + isFirstUse);

 if (isFirstUse == 'true') {
   if (!confirm("If you start your session for \"" + appName + "\" now, the start date" +
            " will automatically\nbe updated to the current hour and will not" +
            " be able to be changed")) {
      return;
    }
    form.appRentalId.value=appRentalId;
    form.rentalStartDate.value=startDate;
    form.rentalStartHour.value=startHour;
    form.licOption.value=licOption;
    form.timeUnits.value=timeUnits;
    form.changeStartDate.value='true';
    form.startSessionNow.value='true';
    form.submit();
  }  
  else {
    form.appRentalId.value=appRentalId;
    form.startSessionNow.value='true';
    form.submit();
  }
}  

function openWebInterfaceWindow(url) {
   var fileNumber=99;
   //var fileNumber=Math.round(Math.random()*9999);
   ew=window.open(url, 'webInterfaceWindow', 'height=600,width=680, scrollbars=yes, resizable=yes');
   ew.focus();
}

function calculateTotalPrice(form) {

  totalPrice = 0;

  for (i=0;i<form.length;i++) {
    e = form.elements[i]; 
    if(e.type == "checkbox" && e.checked) {
      for(j=0;j<cartItems.length;j++) {
        if(cartItems[j] == e.value) {
          totalPrice += cartItemPrices[j]; 
        } 
      }
    }
  }
  var object = document.getElementById('totalPriceString');
	object.innerHTML = totalPrice;
}

function preventBackButtonUsage(backButtonOk) {
  //  alert(location.pathname.substring(location.pathname.lastIndexOf("/")+1));
  // alert("backButtonOk = " + backButtonOk);
  if(backButtonOk == 'no') {
    window.history.forward(1);
  }
}

function getObject(name, form)
{
  for (i=0;i<form.length;i++) {
    e = form.elements[i]; 
    if(e.name == name) {
      return e;
    }
  }  
}

function disableOngoing()
{
  var obj1 = document.getElementById("ongoingprepay");
  var obj2 = document.getElementById("ongoingrecharge");
  var obj3 = document.getElementById("ongoinginitialrecharge");
  var obj4 = document.getElementById("onetimeprepay");
  if(obj1 != null)
    obj1.disabled = true;
    
  if(obj2 != null)
    obj2.disabled = true;
    
  if(obj3 != null)
    obj3.disabled = true;
    
  if(obj4 != null)
    obj4.disabled = false;  
}

function disableOnetime()
{
  var obj1 = document.getElementById("ongoingprepay");
  var obj2 = document.getElementById("ongoingrecharge");
  var obj3 = document.getElementById("ongoinginitialrecharge");
  var obj4 = document.getElementById("onetimeprepay");
  if(obj1 != null)
    obj1.disabled = false;
    
  if(obj2 != null)
    obj2.disabled = false;
    
  if(obj3 != null)
    obj3.disabled = false;
    
  if(obj4 != null)
    obj4.disabled = true;  
}

function isNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   if(sText.length == 0)
   {
     return false;
   }
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;   
}


function submitAccount()
{
  document.VendorUsageReportForm.accountChanged.value="true";
  document.VendorUsageReportForm.submit();
}

function submitReportForm(action)
{
   var period = document.getElementById("timePeriod");
   var timeVal = period.options[period.selectedIndex].value;
   if(timeVal == 'weekly' || timeVal =='daily')
   {
     var from = document.getElementById("from").value;
     var to = document.getElementById("to").value;
     
     if(from.length == 0 && to.length == 0)
     {
       alert("Please select From Date and To Date");
       return false;
     }
     if(from.length > 0 && !isDate(from, 'MM/dd/yyyy'))
     {
       alert("Please select a valid From date");
       return false;
     }
     if(to.length > 0 && !isDate(to, 'MM/dd/yyyy'))
     {
       alert("Please select a valid To date");
       return false;
     }     
   }
   document.VendorUsageReportForm.selectedAction.value=action;
   document.VendorUsageReportForm.submit();
}

function goback(){
  history.back();
}



