function OpenPop(uniformid) {
      var popwin = window.open('Largeimageview.asp?param_uniformid=' + uniformid ,'Leather_Sport','width=598,height=520,resizable=1,scrollbars=1,left=0,screenX=0,top=0,screenY=0;');
      return false;
}
 
 
function OpenPrintableOrder(uniformid) {
      var popwin = window.open('PrintableOrder.asp' ,'Leather_Sport','width=650,height=600,resizable=1,scrollbars=1,left=0,screenX=0,top=0,screenY=0;');
      return false;
}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        anchor.onfocus = function() { this.blur(); }

        if (anchor.getAttribute("href") &&
        anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }

    var logo = document.getElementsByTagName("h1");
    logo[0].onclick = function() { window.location = "/" }
}

function loggedIn() {
    if (document.getElementById('leftCol') != null)
      document.getElementById('leftCol').style.display = 'none';
    if (document.getElementById('heading') != null)
      document.getElementById('heading').style.display = 'none';
    if (document.getElementById('rightCol') != null)
      document.getElementById('rightCol').style.width = '704px';
    if (document.getElementById('logoutButton') != null)
      document.getElementById('logoutButton').style.display = 'block';
    }

function changeHeader(which) {
    var hdrImg = document.getElementById('headerImage');
    if (hdrImg == null) return;
    
    if (which == 1) {
        hdrImg.src = "img/headers/catalog.jpg";
    }

    if (which == 2) {
        hdrImg.src = "img/headers/team3.jpg";
    }

    if (which == 3) {
        hdrImg.src = "img/headers/corporate4.jpg";
    }

    if (which == 13) {
        hdrImg.src = "img/headers/accessories2.jpg";
    }

}

function jsglobalFormatMoney(strMoney)
{
   var str = jsglobalFormatFixed(strMoney);
   var indx = str.indexOf(".");
   var strOut = str.substr(indx);
   var ptr = 0;
   while (true)
   {
     strOut = str.substr(--indx, 1) + strOut; 
     ptr++;
     if (indx <= 0) break;
     if (ptr > 2)
     {
       strOut = "," + strOut; 
       ptr = 0; 
     }
   }
   return ("$" + strOut);
}

function jsglobalFormatFixed(strFloat)
{
   return strFloat.toFixed(2);
}

//functions for forms checking

function jsglobalCheckInteger(strValue)
{
    var sValidNum='0123456789';
    var temp1;

    for (var i=0;i<strValue.length;i++)
    {
        temp1 =  strValue.substring(i,i+1);
        //alert('checkinteger: ' + temp1 + ' ' + sValidNum.indexOf(temp1));
        if(sValidNum.indexOf(temp1)==-1)
            { return false; }
    }
    return true;
}

function jsglobalCheckEmail(strValue)
{
    return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strValue);
}

function fncjsLocalMessageLength(objElem, intMaxLen)
{
   var strMessage = objElem.value;
   if (strMessage.length >= intMaxLen)
     objElem.value = strMessage.substr(0, intMaxLen);
}

function clMandatory (id, name, type)
{
    this.id = id;
    this.name = name;
    this.type = type;
}

function clError()
{
    this.fError = false;
    this.strError = "Please make sure that the following fields are entered correctly:"
}

var strPhoneFormatMessage = "the phone field should have 10 digits, for example 123-456-7890.";

function checkMandatory(arMandatory, obError)
{
//  var strAlert;
    var i;

    for (i = 0; i < arMandatory.length; ++i)
    {
        var strValue = document.getElementById(arMandatory[i].id).value
        if (strValue == '')
        {
            obError.fError = true;
            obError.strError = obError.strError + '\n ' + arMandatory[i].name;
        }
        else
        {
            switch (arMandatory[i].type)
            {
                case 'email':
                    if (!jsglobalCheckEmail(strValue))
                    {
                        obError.fError = true;
                        obError.strError = obError.strError + '\n ' +
                            arMandatory[i].name + " should be of the form (for example): name@server.com ";
                    }
                    break;
                case 'credit number':
                    if (!jsglobalCheckInteger(strValue) || strValue.length != 16)
                    {
                        obError.fError = true;
                        obError.strError = obError.strError + '\n ' +
                            arMandatory[i].name + " must be a 16-digit number";
                    }
                    break;
                case 'number':
                    if (!jsglobalCheckInteger(strValue))
                    {
                        obError.fError = true;
                        obError.strError = obError.strError + '\n ' +
                            arMandatory[i].name + " must be a number";
                    }
                    break;
                case 'phone':
                    if (!formatPhone(document.getElementById(arMandatory[i].id), false) )
                    {
                        obError.fError = true;
                        obError.strError = obError.strError + '\n ' +
                            arMandatory[i].name + ": " + strPhoneFormatMessage;
                    }
                    break;
                default:
                   if (arMandatory[i].type.indexOf("pcode") == 0)
                   {
                     var strCountryFld = arMandatory[i].type.substr(arMandatory[i].type.indexOf("#") + 1); 
                     switch (document.getElementById(strCountryFld).value)
                     {  
                       case "Canada" :
                         if (!checkAndFormatCanadianPostalCode(document.getElementById(arMandatory[i].id)))
                         {  
                           obError.fError = true;
                           obError.strError = obError.strError + '\n ' + arMandatory[i].name + ": " + "canadian Postal code invalid.";
                         }
                         break;
                       case "United States" :
                         if (!checkUsaZipCode(document.getElementById(arMandatory[i].id)))
                         {  
                           obError.fError = true;
                           obError.strError = obError.strError + '\n ' + arMandatory[i].name + ": " + "usa zipcode invalid.";
                         }
                         break;
                     }
                   }
              
            }
        }
    }
}

function checkForm(frm, arMandatory)
{

    var obError = new clError();

    checkMandatory(arMandatory, obError);

    if (obError.fError)
        { alert(obError.strError); }
    else
        { frm.submit(); }
}


function formatPhone(el, fAlertOnError)
{
    var strValue=el.value;
    var strNew = '';
    var strNumbers = '0123456789';
    
    var i;
    
    if (strValue.length == 0)
        return false;
    
    // remove the non-number characters
    for (i=0; i < strValue.length; ++i)
    {
        if (strNumbers.indexOf(strValue.charAt(i)) > -1)
            strNew = strNew + strValue.charAt(i);
    }
    if (strNew.length != 10)
    {
        if (fAlertOnError)
        {
            alert(strPhoneFormatMessage + ": " + el.id);
//          setTimeout(".focus()", 50);
            console.log(el);
        }
        return false;
    }
    else
    {
        el.value=strNew.substring(0,3) + '-' + strNew.substring(3,6) + '-' +
             strNew.substring(6);
        return true;
    }
}

function checkAndFormatCanadianPostalCode(field)
{
  var strPcode = field.value.replace(/ /g,"").toUpperCase();
  if ((strPcode.length != 6) || !isNaN(strPcode)) return false;
  strPcode = strPcode.substr(0,3) + " " + strPcode.substr(3);
  field.value = strPcode; 
  
  var regex = /[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d/;
  return regex.test(strPcode);
}

function checkUsaZipCode(field)
{
  var strZipCode = field.value.replace(/ /g,"");
  if (isNaN(strZipCode)) return false;
  if (strZipCode.length != 5) return false;
  field.value = strZipCode; 
  
  var regex = /[0-9][0-9]{4}/;
  return regex.test(strZipCode);
}


function checkPositiveInteger(field, msg, boolClearOnError) 
{
    var val = field.value;
    if (isNaN(val) || (!isNaN(val) && ((val.indexOf(".") > -1) || (val.indexOf("-") > -1))))
    {
        if ((typeof(msg) != "undefined") && (msg != ""))
          alert(msg);
        if ((typeof(boolClearOnError) != "undefined") && (boolClearOnError == true))
          field.value = "";
        return false;
    }
    return true;
}


function checkPositiveDecimal(field, decimalPlaces, msg, boolClearOnError) 
{
    var val = field.value;
    if (isNaN(val) || (!isNaN(val) && (((val.indexOf(".") > -1) && val.substr(val.indexOf(".")).length > (decimalPlaces + 1)) || (val.indexOf("-") > -1))))
    {
        if ((typeof(msg) != "undefined") && (msg != ""))
          alert(msg);
        if ((typeof(boolClearOnError) != "undefined") && (boolClearOnError == true))
          field.value = "";
        return false;
    }
    return true;
}

// end of functions for forms checking


function gotoUrlOnConfirm(url, msg)
{
    if (confirm(msg))
      location.href = url;
}


function fetchNumericValue(field, fieldFromID, expr)
{
    var fieldFrom = document.getElementById(fieldFromID);
    if (fieldFrom == null) return;
    
    var val = fieldFrom.value;
    if ((val == "") || isNaN(val)) return;

    if (typeof(expr) != "undefined")
      val = eval("parseInt(val) " + expr);
    
    field.value = val;
}


function clearFieldOnValueEmpty(field, val, msg)
{
  if (val == null) return true;
  
  if (val == "")
  {
    if ((typeof(msg) != "undefined") && (msg != ""))
      alert(msg);
    field.value = "";
    return false;
  }
  return true;
}


function clearFieldOnError(field, msg, boolError)
{
  boolError = typeof(boolError) != "undefined" ? boolError : true;
  if (boolError == false) return true;
  
  if ((typeof(msg) != "undefined") && (msg != ""))
    alert(msg);
  field.value = "";
  return false;
}


function restoreFieldOnError(field, msg, boolRestorePrevValue, boolError)
{
  boolError = typeof(boolError) != "undefined" ? boolError : true;
  if (boolError == false)
  {
    if (field.getAttribute("orig_value") == null)
      field.setAttribute("orig_value", field.value); 
    field.setAttribute("prev_value", field.value);
    return true; 
  }
  
  if ((typeof(msg) != "undefined") && (msg != ""))
    alert(msg);
  
  if ((typeof(boolRestorePrevValue) != "undefined") && (boolRestorePrevValue == true))
    field.value = field.getAttribute("prev_value") == null ? "" : field.getAttribute("prev_value"); 
  else
    field.value = field.getAttribute("orig_value") == null ? "" : field.getAttribute("orig_value"); 
  return false;
}

//-----------------------//
//                       //
// ASYCN CALLS FUNCTIONS //
//                       //
//-----------------------//

function sendWebRequest(url)
{
//    alert ("sendWebRequest: " + url);
  if ((typeof(url) == "undefined") || (url == "")) return "";
  
  var xmlhttp=false;
  var resp = "";
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
  try 
  {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) 
  {
   try 
   {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) 
   {
     xmlhttp = false;
   }
  }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
  {
   try 
   {  
     xmlhttp = new XMLHttpRequest();
   } 
   catch (e) 
   {
     xmlhttp=false;
   }
  }

  if (!xmlhttp && window.createRequest) 
  {
   try 
   {
      xmlhttp = window.createRequest();
   } 
   catch (e) 
   {
     xmlhttp=false;
   }
  }
  
  xmlhttp.open("GET", url, false);
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState == 4)
    {
      if (xmlhttp.responseText == "")
      {
        alert("Asynch call error");
        return "";
      }
      resp = xmlhttp.responseText; 
    }
  }
  xmlhttp.send(null);
  return resp;
}


function jsTextareaLength(strElemId, iMaxLen)
{

    var elText = document.getElementById(strElemId);
    var strText = elText.value.replace("'", "''");

    if (strText.length >= iMaxLen)
    {
        elText.value = elText.value.substr(0, iMaxLen - strText.length + elText.value.length - 20);
        //alert ("length: " + elText.value.length);
    }

}



