

// Usage: var result = "Hello {0}! This is {1}.".format("world","foo bar");
String.prototype.format = function() {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function(capture) { return args[capture.match(/\d+/)]; });
}
function RegionSucceededCallback(result, eventArgs) {
    document.location = result;
    return false;
}
function RegionFailedCallback(error) {
    // Do nothing.
    debugger;
}

var e = null;
function RegionLink(e, objID, regionId) {
    e = e || window.event;
    var obj = document.getElementById(objID);
    var xpos = 0;
    var ypos = 0;
    var ewidth = obj.offsetWidth;
    var eheight = obj.offsetHeight;
    if (document.all) {
        xpos = e.offsetX;
        ypos = e.offsetY;
    } else {
        xpos = e.pageX - findPosX(obj);
        ypos = e.pageY - findPosY(obj);
    }
    BrandsMart.WebLinkRegionNavigator.GetWebLinkRegionLinkURL(regionId, xpos, ypos, ewidth, eheight, RegionSucceededCallback, RegionFailedCallback)
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft += obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    } else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

function RunSearch(e) {
    var evt = e || window.event;
    var keyPressed = evt.which || evt.keyCode;
    if (keyPressed == 13) {
        document.getElementById(AspNetUnmangleId('GoSearch')).click();
        return false;
    }
    return true;
}

function openPricingPolicyWindow() {
    var strUrl;
    strUrl = "Source/Services/PricingPolicy.aspx";
    var objWin = window.open(strUrl, "PricingPolicy", "width=638,height=375,location=no,menubar=no,resizable=no,titlebar=no,toolbar=no,scrollbars=no");
}	

function SetFocusTo(sObjId)
{
	try
	{
		var oTarget = document.getElementById(sObjId)
		if(oTarget)
		{
			oTarget.focus();
			if(oTarget.tagName == "INPUT") oTarget.select();
		}
	}
	catch(oEx)
	{
		// do nothing
	}
}
function BackLight(oEvent)
{
	if(!oEvent) oEvent = window.event;
	oEvent.srcElement.style.backgroundColor="#e8e8e8";
}
function UnBackLight(oEvent)
{
	if(!oEvent) oEvent = window.event;
	oEvent.srcElement.style.backgroundColor = "";
}
function Trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}
function Blank(strText) {
	try {
		if(strText) {
			return (Trim(strText).length == 0); 
		}
		else { return true; }
	}
	catch(objEx){}
}
function IsInteger(strValue, bNegativeSign){
	try{					
		if(Blank(strValue)) return false;							// blank or null
		var objRegEx = new RegExp(/^-[0-9]+$|^[0-9]+$/);			// any chars must be 0-9 from start to end of string							
		if(!objRegEx.test(strValue)) return false;
		
		if(bNegativeSign){			
			if(strValue.length > 11) return false;					// length
			if((strValue < Math.pow(-2, 31)) || (strValue > (Math.pow(2, 31) - 1))) return false;	// range 2^-31 to 2^31 - 1
		}
		else{			
			if(strValue.length > 10) return false;					// length
			if(strValue > (Math.pow(2, 31) - 1)) return false;		// range 0 - 2^31 -1
		}														
		return true;
	}	
	catch(objEx){}
}
function ValidEmail(sEmail)
{
	return(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(sEmail));
}
function TabField(oCaller, sTargetId, oEvent)
{
	if(!isNaN(oCaller.getAttribute("maxlength"))) {
		if(oEvent.keyCode != 9 && oEvent.keyCode != 16)	{
			if(oCaller.value.length >= oCaller.getAttribute("maxlength")) SetFocusTo(sTargetId);
		}
	}
}
function ClearFormFields(oForm) {
	for(var i=0; i < oForm.elements.length; i++) {
		if(oForm.elements[i].type == "text" || oForm.elements[i].type == "textarea") {
			oForm.elements[i].value = "";
		}
		else if(oForm.elements[i].type == "select-one") {
			oForm.elements[i].selectedIndex = 0;
		}
	}
}
function trim(strValue) {
  while (strValue.substring(0,1) == ' ') {
    strValue = strValue.substring(1,strValue.length);
  }
  while (strValue.substring(strValue.length-1,strValue.length) == ' ') {
    strValue = strValue.substring(0,strValue.length-1);
  }
  return strValue;
}
function IsDate(dateStr) {
    try
    {
        var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
        var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
        if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
        var parts = dateStr.split(RegExp.$1);
        var mm = parts[0];
        var dd = parts[1];
        var yy = parts[2];
        if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
        if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
        var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
        if (parseFloat(dd) != dt.getDate()) { return false; }
        if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
        return true;
   }
   catch(objEx){}
}


