﻿// JScript File
  var XMLreq;
  var XMLResultSpan;
  var DescSpan;
  var WeightSpan;
  var CurrPrice;
  var NextPrice; 
  var ExtPrice;
  
  var ProdNoBox;
  var QtyBox;
  var LinesTbl;
  var CurrentSSType=1;
  var CategoryTableID;
  var CancelSubmit=false;
  var ErrorMsg ="";
  function urlencode(str) {
      return escape(str).replace('+', '%2B').replace('%20', '+').replace('*', '%2A').replace('/', '%2F').replace('@', '%40');
  }

  // Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isBoolean(a) {
    return typeof a == 'boolean';
}
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
}
function clearcombo(elem){
 var i;
 for (i = elem.options.length; i > 0; i--) elem.options[i] = null;
 elem.selectedIndex = 0;
}
// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

function isDate(dateStr) {

var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{1,4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
alert("Please enter a valid date.");
return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
return true; // date is valid
}

function ValidateSearchDate(BoxWithDate){ 
if (BoxWithDate.value != "ALL")
  { 
    try
      {
      if (BoxWithDate.value == "T") 
        {
        var Today = new date();
        BoxWithDate.value = Today.toDateString();
        }
      else
        {
        if (isDate(BoxWithDate.value))
          {
          CancelSubmit = false;
          return true;
          }
        else
          {
          CancelSubmit = true;
          BoxWithDate.focus();
          return false; 
          }
        }
      }
    catch (err)
      {
      return false; 
      }
  }
else
  {return true;}
} 

function NumbersOnly()
  {
  
  if (((event.keyCode >= 48) && (event.keyCode <= 57)) || (event.keyCode == 8) || (event.keyCode == 9))
    {
    return true;
    }
  else
    {
    event.keyCode = 0;
    return false;
    }
  }


function addElement(ParentElement, ElementID, MenuText) {
//  var numi = document.getElementById('theValue');
//  var num = (document.getElementById('theValue').value -1)+ 2;
//  numi.value = num;
  var newDiv = document.createElement('div');
//  var divIdName = 'my'+num+'Div';
  newDiv.className = 'submenu';
//  newDiv.
  newDiv.setAttribute('id',ElementID);
//  addEvent(newDiv, "Click", "SwitchMenu('Menu1Ab','Menu1');", 0);
  newDiv.innerHTML = "Populate sub list";
  ParentElement.parentNode.insertBefore(newDiv,ParentElement.nextSibling);
}

function addEvent(obj,evType,fn,useCapture){
  var ret=false;
  if(obj!=null){
    if(obj.addEventListener){
      obj.addEventListener(evType,fn,useCapture);
      ret=true;
    }
  else if(obj.attachEvent){
      obj.attachEvent("on"+evType,fn);
      ret=true;
    }
    else{ alert("Can't attach");}
  }
  return ret;
}

  function fctGetElement(ElementId)
    {
    if (document.getElementById)
      {
      return document.getElementById(ElementId);
      }
    else if (document.all)
      {
      return document.all[ElementId];
      }
    }
function subBuildMenu(ClickedDiv, NewMenuName, ParentMenuName, MenuText)
  {
  if (isNull(ClickedDiv.nextSibling))
    {
    addElement(ClickedDiv, NewMenuName, MenuText);
    SwitchMenu(NewMenuName, ParentMenuName);
//    ClickedDiv.nextSibling.style.display = "block";
    }
  else
    {
    if (ClickedDiv.nextSibling.className != 'submenu')
      {
      addElement(ClickedDiv, NewMenuName, MenuText);
      SwitchMenu(NewMenuName, ParentMenuName);
//      ClickedDiv.nextSibling.style.display = "block";
      }
    else
      {
      SwitchMenu(NewMenuName, ParentMenuName);
      }
    }
    
//  var InsideHTML = 'Look at this'
//  var subDiv = ClickedDiv.getElementsByTagName("div")[0]
//  subDiv.innerHTML = InsideHTML
//  addElement()
  }
function fctExpandCategory(CategoryID, RowIndex)
  {
  var ltbl = document.all["CategoryTable"].tBodies[0]; 
  var lnewRow = document.createElement("tr")
  var lnewCell = document.createElement("td")
  var CellText = document.createTextNode("this is cool")
  lnewCell.appendChild(CellText)
  lnewRow.appendChild(lnewCell)
  }
function subSetCurrentSSType(TypeIndex)
  {
  var ltbl = document.all["tblSSTypes"];
  ltbl.rows[0].cells[0].className="SSTypeOtherTabs";
  ltbl.rows[0].cells[1].className="SSTypeOtherTabs";
//  ltbl.rows[0].cells[2].className="SSTypeOtherTabs";
  ltbl.rows[0].cells[TypeIndex].className="SSTypeCurrentTab";
  }
function fctAddLine()
  {
  var NewLine;
  
  }
function GetTextFromWebService(PNBox, DescSpan)
  {
  XMLResultSpan = DescSpan;
  //DescSpanID.innerText = "Avante Description Goes Here";
  loadXMLDoc("http://LOCALHOST/website/ORDERITEMS.asmx/ItemInfo", "ProductNbr=" + PNBox.value);
  }
  
function fctGetItemDescFromAvante()
  {
  
  }
function processReqChange() 
{
    // only if XMLReq shows "complete"
    if (XMLReq.readyState == 4) {
        // only if "OK"
        if (XMLReq.status == 200) {
            // ...processing statements go here...
//            alert(XMLReq.responseText);
            var DescNode = XMLReq.responseXML.documentElement.selectSingleNode("clsProduct/ProdDesc");
//            alert(DescNode.text);
            XMLResultSpan.innerText = DescNode.text;
        } else {
           alert("There was a problem retrieving the XML data:\n" + XMLReq.statusText);
        }
    }
}
function GetXMLRequestObj()
  {
    var ReturnObj
    if (window.XMLHttpRequest) 
      {ReturnObj = new XMLHttpRequest();}
    else if (window.ActiveXObject) 
      {ReturnObj = new ActiveXObject("Microsoft.XMLHTTP");}
  return ReturnObj
  }
function loadXMLDoc(url,formData) 
  {
  XMLReq = GetXMLRequestObj()
  XMLReq.onreadystatechange = processReqChange;
  XMLReq.open("POST", url, true);
  XMLReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  XMLReq.setRequestHeader("Content-Length", 90);
  XMLReq.send(formData);
  }
function fctSetFocus(BoxID)
  {
  FocusBox = document.getElementById(BoxID)
  FocusBox.focus();
  }
function fctSwapImage(ImgToUse, NewSrc)
  {
  document.all[ImgToUse].src = NewSrc;
  }
  function userobject(parameter){
}
function prodInfo(ProdNbr)
{		popup = window.open("prodInfo.aspx?part="+ProdNbr, "form", "width=500,height=570,resizable=1,scrollbars=1")
}
function CheckStock(ProdNbr)
  {
    __doPostBack("StockCheckLink", ProdNbr);
  }
function fctValidQty(QtyToCheck, pbolShowMsg)
  {
  //Check qty to ensure it is a positive value.
  if (QtyToCheck.value == "")
    {
    //Blanks are OK
    CancelSubmit = false;
    }
  else
      {
      if (isNaN(QtyToCheck.value))
        {
        //Not a Number valid qty fails
        CancelSubmit = true;
        //ErrorMsg="Qty must be a positive number"
        if(pbolShowMsg)
          {alert("Qty must be a positive number");}
        QtyToCheck.focus();
        }
      else
        {
        //Is a Number, now check for a + number
        if (QtyToCheck.value > 0)
          {
          //Number is positive return true
          CancelSubmit = false;
          }
        else
          {
          //negative number fails
          CancelSubmit = true;
//          ErrorMsg="Qty must be a positive number"
          if(pbolShowMsg)
            {alert("Qty must be a positive number");}
          QtyToCheck.focus();
          }
        }
    }
  }
function fctKeyCode(event)  
{
  if ((event.which))
    {return event.which;}
  else
    {return event.keyCode;}
}
  function fctTabOnEnter()
    {
    if(event.keyCode==13) 
       {
       event.keyCode=9;
       return false;
       }
     else
       {
       return true;
       }
    }
function getSourceElement(event){
var SourceElement
  if (event.srcElement)
    {
    SourceElement = event.srcElement;
    }
  else
    {
    SourceElement = event.target;
    }
return SourceElement
}

function fctSetUpNavigation(currentTab)
{
var btn1 = fctGetElement("button1");
var btn2 = fctGetElement("button2");
var btn3 = fctGetElement("button3");
var btn4 = fctGetElement("button4");
switch(currentTab)
  {
  case 1:
    btn1.setAttribute("class", "firstRounded");
    btn1.setAttribute("className", "firstRounded");

    btn2.setAttribute("class", "roundedGray");
    btn2.setAttribute("className", "roundedGray");

    btn3.setAttribute("class", "roundedGray");
    btn3.setAttribute("className", "roundedGray");

    btn4.setAttribute("class", "roundedGray");
    btn4.setAttribute("className", "roundedGray");
    break;
  case 2:
    btn1.setAttribute("class", "firstRoundedGray");
    btn1.setAttribute("className", "firstRoundedGray");

    btn2.setAttribute("class", "Goldrounded");
    btn2.setAttribute("className", "Goldrounded");

    btn3.setAttribute("class", "roundedGray");
    btn3.setAttribute("className", "roundedGray");

    btn4.setAttribute("class", "roundedGray");
    btn4.setAttribute("className", "roundedGray");
    break;
  case 3:
    btn1.setAttribute("class", "firstRoundedGray");
    btn1.setAttribute("className", "firstRoundedGray");

    btn2.setAttribute("class", "roundedGray");
    btn2.setAttribute("className", "roundedGray");

    btn3.setAttribute("class", "Goldrounded");
    btn3.setAttribute("className", "Goldrounded");

    btn4.setAttribute("class", "roundedGray");
    btn4.setAttribute("className", "roundedGray");
    break;
  case 4:
    btn1.setAttribute("class", "firstRoundedGray");
    btn1.setAttribute("className", "firstRoundedGray");

    btn2.setAttribute("class", "roundedGray");
    btn2.setAttribute("className", "roundedGray");

    btn3.setAttribute("class", "roundedGray");
    btn3.setAttribute("className", "roundedGray");

    btn4.setAttribute("class", "Goldrounded");
    btn4.setAttribute("className", "Goldrounded");
    break;
  }

}
