//display page with details
openWins = new Array();
curWin = 0;
keySeq = 0;

function closeWindow(session){
if (session == false){
  alert("Your session has expired. \n To continue you have to log in again.");
  window.close();
  }
  return true;
}


function closeAll() {
  if (openWins.length > 0){
   if (confirm ("Clicking OK all the pop up windows will close without saving.\n ")){
      for(i=0; i<openWins.length; i++) if (openWins[i] && !openWins[i].closed) openWins[i].close();
   }
  } else {
    return false;
  }
}

function logout(page){
  var url = page;
  var newWin = "_top";
  var  features = ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
  window.open(url, newWin, features);
}

function openPageChanges(page,key,detail_key)
{
  if (key =="null")
    var url = page;
  else
    var url = page + "?key=" + key + "&detail_key=" + detail_key;
   // alert(url);
  var height = 250;
  var width = 525;
  var newWin = "_blank"
  var features = "top=175,left=125,height=" + height + ",width=" + width;
  features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
  window.open(url, newWin, features);


} //openPageChanges

function openPageMessage(page, key){
  var url = "";
  if (key == "null"){
    url =  page;
  } else {
    url =  page + "?key=" + key ;
  }
    var height = 520;
    var width = 645;
    var newWin = "_parent"
    var features = "top=100,left=100,height=" + height + ",width=" + width;
    features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    openWins[curWin++] = window.open(url, newWin, features);
} //openPageMessage

function openPageFilter(page, query){
    var url = "";
    url =  page + "?" + query;
//alert(url);
    var height = 175;
    var width = 300;
    var newWin = "_blank"
    var features = "top=100,left=100,height=" + height + ",width=" + width;
    features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    openWins[curWin++] = window.open(url, newWin, features);
} //openPageMessage


function openPage(page, key){
  var url = "";
  if (key == "null"){
    url =  page;
  } else {
    url =  page + "?key=" + key;
  }
  //alert(url);
  if (page != "ReportOptions")
  {
    var height = 520;
    var width = 645;
  }else{
    var height = 520;
    var width = 700;
  }
    var newWin = "_blank"
    var features = "top=100,left=100,height=" + height + ",width=" + width;
    features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    openWins[curWin++] = window.open(url, newWin, features);
    //if (!openWins[curWin++].opener)
    // openWins[curWin++].opener = self;

} //openPage

function changePage(page, key)
{
  var url = "";
  if (key == "null")
  {
    url =  page;
  }
  else
  {
    url =  page + "?key=" + key ;
  }
  window.location = url;
}


function openPageInvoiceUpdates(page, key, type, seq){
  //var product = document.parms.selected_line.value;
  if (key =="null"){
    var url = page;
  }else{
    var url = page + "?key=" + key + "&" + type + "=" + seq;
  }
//alert(url);
    var height = 520;
    var width = 645;
    var newWin = "_parent"
    var features = "top=100,left=100,height=" + height + ",width=" + width;
    features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    window.open(url, newWin, features);

} //openPageInvoice2

function openPageInvoice(page,key,action)
{
  if (key =="null")
    var url = page;
  else
    var url = page + "?key=" + key + "&action=" + action;
  var height = 520;
  var width = 645;
  var newWin = "_parent"
  var features = "top=100,left=100,height=" + height + ",width=" + width;
  features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
  window.open(url, newWin, features);


} //openPageInvoice



function changeNav(page_name,page_num,page_count) {
var user_input = document.filter.user_input.value;
var nav_user = parseInt(document.filter.nav_user.value);
var page;
  if ( user_input > page_count ) {
    alert("Only " + page_count + " pages are available to view.");
    document.filter.user_input.value = "";
    document.filter.user_input.focus();
  }
  else
  {
      if (user_input != "")
      {
        page = user_input;

      }
      else
      {
        page = nav_user + page_num;
        if (page == 0)
          page = 1;
        if (page > page_count)
          page = page_count;
      }
      var url = "";
      url = page_name + "?nav_user=" + page ;
      var height = 520;
      var width = 645;
      var newWin = "_self";
      winHandle = window.open(url,newWin);
   }
} //changeNav


function handleEvent(page, keycode) ///capture keystroke
{
  var keycode;
  var url = "";
  var height = 520;
  var width = 645;
  var newWin = "_self";
  var page_count = parseInt(document.filter.page_count.value);
  var nav_user = parseInt(document.filter.nav_user.value);


  if (page == 'ListOrders')
  {
   // alert(keycode);

    switch (keycode)
    {
      case 16:
        if (keySeq==0) {keySeq++}
        break
      case 65:
        if (keySeq==1)
        {
          document.filter.action_range.value = "add_range";
          keySeq = 0
        }
        break
      case 68:
        if (keySeq==1)
        {
          document.filter.action_range.value = "remove_range";
          keySeq = 0
        }
        break
    }
  }


  if (keycode == 13)
  {
    if (document.filter.user_input && document.filter.user_input.value.length > 0)
    {
      var user_input = document.filter.user_input.value;
      if ( user_input > page_count )
      {
        alert("Only " + page_count + " pages are available to view.");
        document.filter.user_input.value = "";
        document.filter.user_input.focus();
      }
      else
      {
        url = page + "?nav_user=" + user_input ;
        winHandle = window.open(url,newWin);
      }
    }
    else
    {
      document.filter.apply.value = 'y';
      document.filter.submit();
    }
    return false
  }
  else
  {
    if (keycode == 39)
    {
      if(nav_user < page_count)
      {
        url = page + "?nav_user=" + (nav_user + 1);
        winHandle = window.open(url,newWin);
      }
    }
    else if (keycode == 37)
    {
      if(nav_user > 1)
      {
        url = page + "?nav_user=" + (nav_user - 1);
        winHandle = window.open(url,newWin);
      }
    }
  }
  return true;
}//handleEvent



function setFocus(myForm)
{
  if( myForm.elements[0]!=null)
  {
    var i;
    var max = myForm.length;
    for( i = 0; i < max; i++ )
    {
      if( myForm.elements[ i ].type != "hidden" &&
        !myForm.elements[ i ].disabled &&
        !myForm.elements[ i ].readOnly )
        {
          myForm.elements[ i ].focus();
          break;
        }
    }
  }
}



/**


function openPageProduct(page,key,detail_key,display_key,action){
if (detail_key == "" ){
  detail_key = display_key;
  //action = "false";
  }


  if (key =="null"){
    var url = page;
  }else{
    var url = page + "?key=" + key + "&detail_key=" + detail_key + "&display_key=" + display_key + "&action=" + action;
  }
    var height = 520;
    var width = 645;
    var newWin = "_parent"
    var features = "top=100,left=100,height=" + height + ",width=" + width;
    features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    window.open(url, newWin, features);

} //openPageProduct


function openPage_back(page,key,distribution,assortment,action){

  if (key =="null"){
    var url = page;
    document.filter.task.selectedIndex = 0;
  }else{
    var url =  page + "?key=" + key + "&distribution=" + distribution + "&assortment=" + assortment + "#dists_line";
  }
    var height = 520;
    var width = 645;
   if ((distribution == "null" || assortment == "null") && (action != "close")) {
      var newWin = "_blank"
   }else if (distribution == "false" || assortment == "false"){
    var newWin = "_blank"
   }else if (action == "close"){
    var newWin = "_parent"
   }else{
    var newWin = "_parent"
   }
    var features = "top=100,left=100,height=" + height + ",width=" + width;
    features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    window.open(url, newWin, features);
} //openPage

function openPage2(page,key,detail_key,display_key,amount,price,shipDate,action,display,status,distribution){

  if (document.detail.old_amount){
    var old_value = document.detail.old_amount.value;
    var new_value = document.detail.amount.value;
    if ( new_value > old_value) {
      alert("The new Ordered Qty must be less then the old Qty ");
      document.detail.amount.value = "";
      return false;
    }
  }


  if (detail_key == "" || detail_key == "null"){
  detail_key = display_key;
  action = "false";
  }
  if (key == "null"){
     var url = page;
  } else if (key != "null" && detail_key == "false"){
      var url = page + "?key=" + key + "&action=" + action + "&status=" + status + "&distribution=" + distribution;
  } else {
      var url = page + "?key=" + key + "&detail_key=" + detail_key + "&display_key=" + display_key +"&action=" + action +"&display=" + display + "&amount=" + amount + "&price=" + price + "&shipDate=" + shipDate + "&status=" + status + "&distribution=" + distribution  +"#current_line";
  }
      var height = 520;
      var width = 645;
      var newWin = "_parent";
      var features = "top=100,left=100,height=" + height + ",width=" + width;
  features = features + ",status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
  window.open(url, newWin, features);
} //openPage2


function openPage2Invoice(page,key,detail_key,display_key,amount,display_edit_key,edit_key,action,display,status,distribution,invoice_no){

var str = document.detail.invoice_status.options[document.detail.invoice_status.selectedIndex].value;
//document.detail.invoiceStatus.value = str;

  if (display_edit_key == ""){
  display_edit_key = edit_key;
  }

  if (action == "apply"){
     if (str == "Ready"){
       if (confirm ("Click OK to accept this invoice.\n Accepting cannot be undone.")){
        if (key == "null"){
          var url = page;
        } else if (key != "null" && (detail_key == "" )){
            var url = page + "?key=" + key + "&action=" + action + "&status=" + str + "&distribution=" + distribution + "&invoice_no=" + invoice_no;
        } else {
            var url = page + "?key=" + key + "&detail_key=" + detail_key + "&display_key=" + display_key +"&action=" + action +"&display=" + display + "&amount=" + amount + "&display_edit_key=" + display_edit_key + "&edit_key=" + edit_key + "&status=" + str + "&distribution=" + distribution + "&invoice_no=" + invoice_no + "#current_line";
        }
      } else {
        return false;
      }
    } else {

     if (key == "null"){
        var url = page;
      } else if (key != "null" && (detail_key == "" )){
          var url = page + "?key=" + key + "&action=" + action + "&status=" + str + "&distribution=" + distribution + "&invoice_no=" + invoice_no;
      } else {
          var url = page + "?key=" + key + "&detail_key=" + detail_key + "&display_key=" + display_key +"&action=" + action +"&display=" + display + "&amount=" + amount + "&display_edit_key=" + display_edit_key + "&edit_key=" + edit_key + "&status=" + str + "&distribution=" + distribution + "&invoice_no=" + invoice_no + "#current_line";
      }
    }
  } else {

    if (detail_key == ""){//clean it
    detail_key = display_key;
    action = "false";
    }///
    if (key == "null"){
      var url = page;
    } else if (key != "null" && (detail_key == "" || detail_key == "")){
        var url = page + "?key=" + key + "&action=" + action + "&status=" + str + "&distribution=" + distribution;
    } else {
        var url = page + "?key=" + key + "&detail_key=" + detail_key + "&display_key=" + display_key +"&action=" + action +"&display=" + display + "&amount=" + amount + "&display_edit_key=" + display_edit_key + "&edit_key=" + edit_key + "&status=" + str + "&distribution=" + distribution + "&invoice_no=" + invoice_no + "#current_line";
    }
  }
      var height = 520;
      var width = 645;
      var newWin = "_parent";
      var features = "top=100,left=100,height=" + height + ",width=" + width;
  features = features + ",status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
  window.open(url, newWin, features);
  //setTimeout('parent.close()',5000);
} //openPage2Invoice
**/

//display reports
var reporting = false;

function runReport(box, magic, servlet, filterActive, list, page , key )
{
    var name = ""
    if( box.options != null)
      name = box.options[box.selectedIndex].value;
    else
      name = box;

  var features = "status=yes,toolbar=no,menubar=no,location=no,resizable=yes";

  if (name == 'print')
  {
    //alert(name);
    window.print();
  }
  else
  {
    //alert("list" + list);
    if ((filterActive == 'true') || (list != 0))
    {
       if (name == 'ordersPackingSlips')
      {
        var token, useNoPartner;
        usePartner = parent.body.document.filter.show_filter.value;
        var select = parent.nav.document.selectForm.partnerSel;
        token = select.options[select.selectedIndex].value;
        if (usePartner != 'false')
        {
          if ((token == '[PN.28650]') || (token == '[PN.28250]') || (token == '[PN.25850]'))
          {
            window.open(servlet + '?name=' + name + '&magic=' + magic, null, features);
            setTimeout('location.reload(true)',5);
          }
          else
          {
            alert("Please select one of the following partners \"Bluelight.com\", \"QVC.com\" or \"Target.com\".");
            parent.nav.document.selectForm.partnerSel.focus();
          }
        }
        else
        {
          window.open(servlet + '?name=' + name + '&magic=' + magic, null, features);
          setTimeout('location.reload(true)',5);
        }
      }
      else if (name == 'productInfoList')
      {
        var select = parent.nav.document.selectForm.partnerSel;
        token = select.options[select.selectedIndex].value;
        if (token == '[PN.0]')
        {
          alert("For Partner Product List report you have to use the Partner Filter");
          parent.nav.document.selectForm.partnerSel.focus();
          return false;
        }
        else
          window.open(servlet + '?name=' + name + '&magic=' + magic, null, features);
      }
      else
        window.open(servlet + '?name=' + name + '&magic=' + magic, null, features);
    }
    else
    {
      alert("Before running a report please apply a filter or make a selection from the available list.");
      document.filter.report.selectedIndex = 0;
      return false;
    }
  }

  if (page != null)
  {
    if (key =="null")
      var url = page;
    else
      var url = page + "?key=" + key + "&act=print";
    var height = 520;
    var width = 645;
    var newWin = "_parent"
    var features = "top=100,left=100,height=" + height + ",width=" + width;
    features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
    window.open(url, newWin, features);
  }
  box.selectedIndex = 0;
}
/*
function runReport(box, magic, page, key)
{
  if (!reporting)
  {

    reporting = true;
    var name = box.options[box.selectedIndex].value;
    name = name.toString();
   // if (name == '20450_pack_list');
    var features = "status=yes,toolbar=no,menubar=no,location=no,resizable=yes";
    if (name == 'detail_orders')
      window.open('ListOrdersReport?name=orderDetail&magic=' + magic, null, features);
    else if (name == 'detail_orders_list')
      window.open('ListOrdersReport?name=orderList&magic=' + magic, null, features);
    else if (name == 'distribution_orders_list')
      window.open('ListOrdersReport?name=distributionList&magic=' + magic, null, features);
    else if (name == 'production_orders_list')
      window.open('ListOrdersReport?name=productionList&magic=' + magic, null, features);
    else if (name == 'production_orders_list_no_price')
      window.open('ListOrdersReport?name=productionListNoPrice&magic=' + magic, null, features);
    else if (name == 'production_orders_schedule')
      window.open('ListOrdersReport?name=productionSchedule&magic=' + magic, null, features);
    else if (name == 'production_orders_schedule_no_price')
      window.open('ListOrdersReport?name=productionScheduleNoPrice&magic=' + magic, null, features);
    else if (name == 'order_picking_list')
      window.open('ListOrdersReport?name=orderPickingList&magic=' + magic, null, features);
    else if (name == 'detail')
      window.open('DisplayOrderReport?name=orderDetail&magic=' + magic, null, features);
    else if (name == 'distribution_list')
      window.open('DisplayOrderReport?name=distributionList&magic=' + magic, null, features);
    else if (name == 'distribution_changes')
      window.open('DisplayOrderReport?name=distributionChanges&magic=' + magic, null, features);
    else if (name == 'distribution_shipped')
      window.open('DisplayOrderReport?name=distributionShipped&magic=' + magic, null, features);
    else if (name == 'store_list')
      window.open('DisplayOrderReport?name=storeList&magic=' + magic, null, features);
    else if (name == 'order_store_details')
      window.open('DisplayOrderReport?name=orderStoreDetail&magic=' + magic, null, features);
    else if (name == 'detail_list_invoice')
      window.open('ListInvoiceReport?name=invoiceDetail&magic=' + magic, null, features);
    else if (name == 'detail_invoice_list')
      window.open('ListInvoiceReport?name=invoiceList&magic=' + magic, null, features);
    //////new
    else if (name == 'detail_invoices')
      window.open('ListInvoicesReport?name=invoiceDetail&magic=' + magic, null, features);
    /////
    else if (name == 'distribution_invoice_list')
      window.open('ListInvoiceReport?name=distributionList&magic=' + magic, null, features);
    else if (name == 'detail_invoice')
      window.open('DisplayInvoiceReport?name=invoiceDetail&magic=' + magic, null, features);
    else if (name == 'distribution_invoce')
      window.open('DisplayInvoiceReport?name=distributionList&magic=' + magic, null, features);
    else if (name == 'shipment_dist')
      window.open('DisplayShipmentReport?name=distributionList&magic=' + magic, null, features);
    else if (name == 'packing_slips_by_store')
      window.open('DisplayShipmentReport?name=packingSlipsByStore&magic=' + magic, null, features);
    else if (name == '20450_bol')
      window.open('DisplayShipmentReport?name=20450_bol&magic=' + magic, null, features);
    else if (name == '20450_pack_list')
      window.open('DisplayShipmentReport?name=20450_packList&magic=' + magic, null, features);
    else if (name == '20450_manifest')
      window.open('DisplayShipmentReport?name=20450_manifest&magic=' + magic, null, features);
    else if (name == 'bol')
      window.open('DisplayShipmentReport?name=bol&magic=' + magic, null, features);
    else if (name == 'inbound_edi_list')
      window.open('DisplayInboundEdiReport?name=inboundEdiDocumentList&magic=' + magic, null, features);
    else if (name == 'outbound_edi_list')
      window.open('DisplayOutboundEdiReport?name=outboundEdiDocumentList&magic=' + magic, null, features);
    else if (name == 'print'){
      window.print();

      if (page != null){
        if (key =="null")
          var url = page;
        else
          var url = page + "?key=" + key + "&act=print";
        var height = 520;
        var width = 645;
        var newWin = "_parent"
        var features = "top=100,left=100,height=" + height + ",width=" + width;
        features = features + ",dependent=yes,status=yes,toolbar=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
        window.open(url, newWin, features);
      }
    }

    box.selectedIndex = 0;
    reporting = false;
  }
} // runReport
*/
// ************************
// layer utility routines *
// ************************

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
  // W3C DOM
  return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
  // MSIE 4 DOM
  return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
  // NN 4 DOM.. note: this won't find nested layers
  return document.layers[objectId];
    } else {
  return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
  styleObject.visibility = newVisibility;
  return true;
    } else {
  // we couldn't find the object, so we can't change its visibility
  return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
  styleObject.left = newXCoordinate;
  styleObject.top = newYCoordinate;
  return true;
    } else {
  // we couldn't find the object, so we can't very well move it
  return false;
    }
} // moveObject





function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
        var dtCh= "/";
        var minYear=1900;
        var maxYear=2100;
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
