function replaceAll(str,from,to)
{
    var idx = str.indexOf(from);
    while (idx > -1) 
	{
        str = str.replace(from,to); 
        idx = str.indexOf(from);
    }
    return str;
}

function Trim(txt) {
	var strTmp = txt;
	//trimming from the front
	for (counter=0; counter<strTmp.length; counter++)
		if (strTmp.charAt(counter) != " ")
			break;
	//trimming from the back
	strTmp = strTmp.substring(counter,strTmp.length);
	counter = strTmp.length - 1;
	for (counter; counter>=0; counter--)
		if (strTmp.charAt(counter) != " ")
			break;
	return strTmp.substring(0, counter+1);
}

function RemoveDoubleSpace(obj)
{
    var valarr=obj.value.split(" ");
    var valstr=obj.value;
    if(valarr.length>0)
    {
        valstr="";
        for(i=0;i<valarr.length;i++)
        {
            if(valarr[i] != "" && valarr[i] != " ")
            {
                //alert(valarr[i]);
                valstr += " " + valarr[i];
            }
        }
        valstr=Trim(valstr);
    }
    obj.value=valstr;
}

function TrimValue(obj)
{
    obj.value=Trim(obj.value);
}

function IsNumeric(objval)
{
	var tempstr;
	tempstr =objval;
	var nreg= /[^0-9]/g;
	nresult = tempstr.match(nreg);
	if(nresult != null || objval=="")
	{
		return false;
	}
	return true;
}

function valid_zipcode(objval)
{
	var tempstr;
	tempstr = objval;
	var reg = /[^A-Za-z0-9 -]/g;
	result = tempstr.match(reg);
	if(result != null)
	{
		return false;
	}
	return true;
}

function checkvalidpostalcode(source, clientside_arguments)
{
    var PostalCodeVal = clientside_arguments.Value;
    var countryobj=document.getElementById("ctl00_ContentPlaceHolder1_cboCountry");
    var CountryValue="";
    if(countryobj == null) 
    {
        countryobj=document.getElementById("ctl00_ContentPlaceHolder1_HF_CCCountry");
    }
    if(countryobj== null) 
    {
        source.errormessage="Error: Country object not found. Please contact Administrator";
        clientside_arguments.IsValid=false;
        return false;  
    }
    CountryValue=countryobj.value;
    if(CountryValue=="US")
    {
       if(PostalCodeVal.length != 5)
       {
            source.errormessage="US postal code must be 5 numbers long";
            clientside_arguments.IsValid=false;
            return false;                    
       }
       if(IsNumeric(PostalCodeVal) == false)
       {
            source.errormessage="US postal code must be numeric";
            clientside_arguments.IsValid=false;
            return false;
       }
    }
    else if(CountryValue=="CA" || CountryValue=="MX")
    {
        if(valid_zipcode(PostalCodeVal) == false)
        {
            source.errormessage="Invalid Postal Code";
            clientside_arguments.IsValid=false;
            return false;
        }
    }   
    arguments.IsValid = true;
    return true;
}


function ChangeState(obj)
{
    var countryobj=document.getElementById("ctl00_ContentPlaceHolder1_cboCountry");
    if(obj.selectedIndex > 0 && obj.selectedIndex < 52)
    {
        countryobj.value="US";
    }
    else if(obj.selectedIndex > 52 && obj.selectedIndex < 66)
    {
        countryobj.value="CA";
    }
    else if(obj.selectedIndex > 66 && obj.selectedIndex < 100)
    {
        countryobj.value="MX";
    }
    else
    {
        countryobj.value="";
    }
   
}
function showModalPopupViaClient(BehaviorID) 
{
    var modalPopupBehavior = $find(BehaviorID);
    //alert(BehaviorID + " - " + modalPopupBehavior);
    modalPopupBehavior.show();
}
function hideModalPopupViaClient(BehaviorID) 
{
    var modalPopupBehavior = $find(BehaviorID);
    modalPopupBehavior.hide();
    return false;
}
function getClientBounds()
{
    var clientWidth;
    var clientHeight;
    switch(Sys.Browser.agent) {
        case Sys.Browser.InternetExplorer:
            clientWidth = document.documentElement.clientWidth;
            clientHeight = document.documentElement.clientHeight;
            break;
        case Sys.Browser.Safari:
            clientWidth = window.innerWidth;
            clientHeight = window.innerHeight;
            break;
        case Sys.Browser.Opera:
            clientWidth = Math.min(window.innerWidth, document.body.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.body.clientHeight);
            break;
        default:  // Sys.Browser.Firefox, etc.
            clientWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
            clientHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
            break;
    }
    return new Sys.UI.Bounds(0, 0, clientWidth, clientHeight);
}

function RemoveAllSpaces(obj)
{
    obj.value=replaceAll(obj.value," ","");
}