﻿evt = ""; // Defeat the Chrome bug

function Trimmer(pVal) 
	{ 
		TRs=0; 
		for (i=0; i<pVal.length; i++) 
			{ 
			if (pVal.substr(i,1) == " ")
				{
					TRs++;
				} 
			else 
				{
					break;
				} 
			} 
	
		TRe=pVal.length-1; 
		for (i=TRe; i>TRs-1;i--) 
			{ 
			if (pVal.substr(i,1) == " ") 
				{
					TRe--;
				} 
			else 
				{
					break;
				} 
			} 

		return (pVal.substr(TRs, TRe-TRs+1)); 
	} 

function Right(str, n)
	{
		if (n <= 0)
		   return "";
		else if (n > String(str).length)
		   return str;
		else 
		{
		   var iLen = String(str).length;
		   return String(str).substring(iLen, iLen - n);
		}
	}

function Mid(str, start, len)
    {
		if (start < 0 || len < 0) return "";

		var iEnd, iLen = String(str).length;
		if (start + len > iLen)
			iEnd = iLen;
		else
			iEnd = start + len;

		return String(str).substring(start,iEnd);
	}

function Left(str, n)
	{
		if (n <= 0)
			return "";
		else if (n > String(str).length)
			return str;
		else 
			return String(str).substring(0,n);
	}

function Len(str)
    {  
        return String(str).length;  
    }

function isDigit(c)
	{
	return ((c >= "0") && (c <= "9"))
	}
	
function isInteger(s)
	{
	var i, c;
	if (s.length == 0)
		{
		return false;
		}
	for (i = 0; i < s.length; i++)
		{
		c = s.charAt(i);
		if (!isDigit(c))
			{
			return false;
			}
		}
	return true;
	}		

function disableEnterKey(e)
    {
         var key;      
         if(window.event)
              key = window.event.keyCode; //IE
         else
              key = e.which; //firefox      

         return (key != 13);
    }

function isValidCCN(ccn)
	{
	var no_digit = ccn.length;
	var oddoeven = no_digit & 1;
	var sum = 0;
	for (var count = 0; count < no_digit; count++) 
		{
		var digit = parseInt(ccn.charAt(count));
		if (!((count & 1) ^ oddoeven)) 
			{
			digit *= 2;
			if (digit > 9)
				digit -= 9;
			};
		sum += digit;
		};
	if (sum % 10 == 0)
		return true;
	else
		return false;
}

function FindControl(ctl)
    {
        var elem = document.forms(0).elements;
        for (var i=0; i<elem.length; i++)
            {
                if (elem[i].id.indexOf(ctl,0) != -1)
                    {
                    return elem[i].id
                    }
            }
    }
    
var ctlprefix = "ctl00_pagebody_"

function requireParentCompany(source, args) 
    {
    var selmainttype = document.getElementById(ctlprefix + "selUserMaintenanceType");
    var selparent = document.getElementById(ctlprefix + "selParentCompany");
    
    if ((selparent != null) && (selmainttype != null))
        {
            if (selmainttype.value != "0")
            {
                if (selparent.value == "--") 
                {
                args.IsValid = false;
                }
            }
        }
    }

function CustomPriceListCodeRequired(source, args) 
    {

    var selpricelist = document.getElementById(ctlprefix + "selPriceListType");
    var txtcustom = document.getElementById(ctlprefix + "txtCustomPriceListCode");

    if ((selpricelist != null) && (txtcustom != null))
        {
        if (selpricelist.value == "3")
            if (Trimmer(txtcustom.value) == "") 
            {
            args.IsValid = false;
            }
        }
    }

function CustomPriceListCodeNotAllowed(source, args) 
    {

    var selpricelist = document.getElementById(ctlprefix + "selPriceListType");
    var txtcustom = document.getElementById(ctlprefix + "txtCustomPriceListCode");

    if ((selpricelist != null) && (txtcustom != null))
        {
        if ((selpricelist.value == "1") || (selpricelist.value == "2"))
            if (Trimmer(txtcustom.value) != "") 
            {
            args.IsValid = false;
            }
        }
    }


function requireState(source, args) 
    {

    var selstate = document.getElementById(ctlprefix + "selCompanyState");
    var selcountry = document.getElementById(ctlprefix + "selCompanyCountry");
    if (selcountry.value == "US")
        if (selstate.value == "--") 
        {
        args.IsValid = false;
        }
    }

  
function requireZip(source, args) 
    {

    var txtZip = document.getElementById(ctlprefix + "txtCompanyZip");
    var selcountry = document.getElementById(ctlprefix + "selCompanyCountry");
    if (selcountry.value == "US")
        if (Trimmer(txtZip.value) == "") 
        {
        args.IsValid = false;
        }
    }

function requireKeyName(source, args) {

    var keyname = document.getElementById(ctlprefix + "txtKeyName");
    var modify = document.getElementById(ctlprefix + "chkModify");
    if (modify.checked == true)
        if (Trimmer(keyname.value) == "") {
        args.IsValid = false;
    }
}

//function requireOtherDescription(source, args) {
    
//    var rdo = document.getElementById(ctlprefix + "rlShippingMethod_6");
//    var other = document.getElementById(ctlprefix + "txtShippingOther");
    
//    if (rdo.checked == true)
//       if (Trimmer(other.value) == "") {
//        args.IsValid = false;
//        }
//}

function PasswordCheck(source, args) {

    var pwd = document.getElementById(ctlprefix + "txtPassword");

    if ((parseInt(Len(Trimmer(pwd.value))) > 1) && (parseInt(Len(Trimmer(pwd.value))) < 6))
        {
        args.IsValid = false;
        }
}


