﻿// ------------------------------------------------------------------------------------------
// Copyright AspDotNetStorefront.com, 1995-2010.  All Rights Reserved.
// http://www.aspdotnetstorefront.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
function $bindMethod(object, method) {
  return function() {
    return method.apply(object, arguments);
  };
}

function $window_addLoad(handler) {
    if (window.addEventListener) { 
        window.addEventListener('load',handler,false);
    }
    else if (document.addEventListener) {
        document.addEventListener('load',handler,false);
    }
    else if (window.attachEvent) { 
        window.attachEvent('onload',handler);
    }
    else {
        if (typeof window.onload=='function') {
            var oldload=window.onload;
            window.onload = function(){
                oldload();
                handler();
            }
        } 
        else { window.onload=init; }
    }

}

function $getElement(id, handler) {
    var el = document.getElementById(id);
    return el;
}

var Keys = {
    Enter: 13
}

function $handleSearchEnterKey(id, handler) {
    var el = $getElement(id);    
    if(el) {
        var delKeypress = function(e) {

            var keyCode;
            if (e && e.which) {
                keyCode = e.which;
            }
            else if (typeof (event) != 'undefined') {
                keyCode = event.keyCode;
            }

            // we must manually invoke the Page_ClientValidate
            // method here since relying on the normal behavior
            // is not guaranteed to set the appropriate validation flags
            // to stop the postback.
            if (keyCode == Keys.Enter) {
                if (typeof (Page_ClientValidate) != 'undefined') {
                    if (Page_ClientValidate() == false) {
                        return;
                    }
                }

                handler();

                return false;
            }
        }
        
        el.onkeypress = delKeypress;
    }

}

function getCookie(name) {
	var cookieName = name + "=";
	var cookie = document.cookie;
	var start;
	var end;

	if (cookie.length > 0) {
		start = cookie.indexOf(cookieName);
		if (start != -1) {
			start += cookieName.length;
			end = cookie.indexOf(";", start);
			if (end == -1) {
				end = cookie.length;
			}
			return unescape(cookie.substring(start, end));
		}
	}
	return null;
}

function setCookie(name, value, expires) {
	document.cookie = escape(name) + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

function Trim(TRIM_VALUE) {
	if (TRIM_VALUE.length < 1) {
		return "";
	}

	TRIM_VALUE = RTrim(TRIM_VALUE);

	TRIM_VALUE = LTrim(TRIM_VALUE);

	if (TRIM_VALUE == "") {
		return "";
	}
	else {
		return TRIM_VALUE;
	}

} //End Function


function RTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if (v_length < 0) {
		return "";
	}
	var iTemp = v_length - 1;

	while (iTemp > -1) {
		if (VALUE.charAt(iTemp) == w_space) {
		}
		else {
			strTemp = VALUE.substring(0, iTemp + 1);
			break;
		}
		iTemp = iTemp - 1;

	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	if (v_length < 1) {
		return "";
	}

	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while (iTemp < v_length) {
		if (VALUE.charAt(iTemp) == w_space) {
		}
		else {
			strTemp = VALUE.substring(iTemp, v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While

	return strTemp;

} //End Function
