addEvent(window,"load",initAlert);

String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')");
String.prototype.isEmpty = new Function("var x = this.trim(); if (x.length == 0) { return true; } else { return false; }");
String.prototype.left = function(n){
	if (n <= 0){
	    return '';
	}else if (n > this.length){
	    return str;
	}else{
	    return this.substring(0,n);
	}
}
String.prototype.right = function(n){
    if (n <= 0){
       return '';
    }else if (n > this.length){
       return str;
    }else{
       var iLen = this.length;
       return this.substring(iLen, iLen - n);
    }
}

function initAlert(){
	var alertBar = $("alertBar");

	if(alertBar){
		if (alertBar.firstChild){
			show(alertBar);
		}else{
			hide(alertBar);
		}

		function Cleanup(){
			//removeEvent(alertBar,"click",function(){ hide(alertBar); });
			alertBar = null;
			removeEvent(window,"unload",Cleanup);
		}	
		addEvent(window,"unload",Cleanup);
		//addEvent(alertBar,"click",function(){ hide(alertBar); });
	}
}

function addEvent(ctl, eventType, eventFunction){
	if (ctl.attachEvent){
		ctl.attachEvent("on" + eventType, eventFunction);
	}else if (ctl.addEventListener){
		ctl.addEventListener(eventType, eventFunction, false);
	}else{
		ctl["on" + eventType] = eventFunction;
	}
}

function removeEvent(ctl, eventType, eventFunction){
	if (ctl.detachEvent){
		ctl.detachEvent("on" + eventType, eventFunction);
	}else if (ctl.removeEventListener){
		ctl.removeEventListener(eventType, eventFunction, false);
	}else{
		ctl["on" + eventType] = function(){};
	}
}

function stopEvent(e){
	if (e.stopPropagation){
	// for DOM-friendly browsers
		e.stopPropagation();
		e.preventDefault();
	}else{
	// For IE
		e.returnValue = false;
		e.cancelBubble = true;
	}
}

function objectWrite(value){
    document.write(value);
}

function overrideDefaultSubmit(event, control){
    if (event.keyCode == 13){
        $(control).click();
        return false;
    }
}

function $(id){
	return document.getElementById(id);
}

function hide(ctl){
    if (ctl){
        ctl.style.display = 'none';
	}
}

function show(ctl){
    if (ctl){
        ctl.style.display = 'block';
    }	
}

function setCssClass(x, cssClass){
	if (window.attachEvent && x){
		x.setAttribute("className", cssClass);
	}else if (window.addEventListener && x){
		x.setAttribute("class", cssClass);
	}
}

function AsyncRequest(){
    this.method;            //This is the form method (GET or POST).
    this.url;               //The url to request.
    this.cache = false;     //GET requests are cached by default. 
    this.data;				//This variable holds the form data in the form of a querystring without the leading "?".
    this.callback;          //This will hold the callback function if the asynchronous call responds successfully.
    this.onError = null;	//This will hold a reference to a function to call in case of an error.
   
    //The XmlHttp request object. It does all of the asynchronous sending, receiving, and state handling.
    this.xmlHttp = new function(){ 
        try{
            return new ActiveXObject("Msxml2.XMLHTTP"); 
        }catch(e){}
        try{ 
            return new ActiveXObject("Microsoft.XMLHTTP"); 
        }catch(e){}
        try{ 
            return new XMLHttpRequest(); 
        }catch(e){}
        alert("XMLHttpRequest not supported. Please contact technical support.");
        return null;
    }
    
    //This function handles sending the data via an XmlHttp transport.
    //Notice that (this) is passed in with the function. 
    //This will allow the XmlHttp object to properly handle its readyState.
    this.send = function(){ with(this) { 
        if (method == "GET"){
            if (data.left(1) != '?'){
				url = url + "?" + data;
			}else{
				url = url + data;
			}
            xmlHttp.open(method, url, true);
        }else{
            xmlHttp.open(method, url, true);
        }
        xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4){
                var responseText = xmlHttp.responseText;
                switch (xmlHttp.status){
                    case 404:
                        if (onError == null){
							alert('Page Not Found. The requested url \'' + url + '\' could not be found.');
						}else{
							onError('Page Not Found. The requested url \'' + url + '\' could not be found.');
						}
                        break;
                    case 405:
						if (onError == null){
							alert('Cannot post form data to this page. The requested url \'' + url + '\' could be missing.');
						}else{
							onError('Cannot post form data to this page. The requested url \'' + url + '\' could be missing.');
						}
                        break;
                    case 500:
                        if (onError == null){
							alert(responseText); //This will spit out the entire error received from the called page.
						}else{
							onError(responseText);
						}
                        break;
                    case 200:
                        if (responseText.indexOf('Error:') > -1 || responseText.indexOf('Debug:') > -1){
							if (onError == null){
								alert(responseText); //This is for custom debugging. Insert Error: or Debug: on the called page's response.
							}else{
								onError(responseText);
							}
                        }else{
                            callback(responseText); //Success! Call the user-defined handler and pass in the response text.
                        }
                        break;
					default:
						if (onError == null){
							alert('Undefined error code: ' + xmlHttp.status + '. Please report this to technical support');
							alert(responseText);
						}else{
							onError(responseText);
						}
                        break;
                }
            }
        }
        xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        if (!cache && method != "POST"){
            xmlHttp.setRequestHeader('If-Modified-Since', 'Fri, 5 Dec 1980 00:00:00 GMT');
        }
        if (method == "GET"){
            xmlHttp.send(null);
        }else{
            xmlHttp.send(data);
        }
    }}
}

function textCounterMax(textctl, countctl, maxlimit) {
  if (textctl.value.length > maxlimit){ // if too long...trim it!
    textctl.value = textctl.value.substring(0, maxlimit);
  } else { // update 'characters left' counter
    countctl.innerHTML = maxlimit - textctl.value.length;
  } 
}

function trim(value){
    return rtrim(ltrim(value));    
}

function ltrim(value){
    var v_length = value.length;
    if(v_length < 1){
        return"";
    }
    var w_space = String.fromCharCode(32);
    var strTemp = "";

    var iTemp = 0;

    while(iTemp < v_length){
        if(value.charAt(iTemp) != w_space){
            strTemp = value.substring(iTemp, v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    
    return strTemp;
    
}
function rtrim(value){
    var w_space = String.fromCharCode(32);
    if(v_length < 1){
        return "";
    }    
    
    var strTemp = value;
    var v_length = value.length;
    var iTemp = v_length;

    while(iTemp > 0){
        if(value.charAt(iTemp) != w_space){
            strTemp = value.substring(0, iTemp);
            break;
        }else if (iTemp == 1){
            strTemp = "";
            break;
        }else{        
            iTemp = iTemp - 1;
        }
    } //End While
    
    return strTemp;
}

function isEmail(elm) {
	if (elm.value.indexOf("@") != "-1" && elm.value.indexOf(".") != "-1" && elm.value != "")
	    return true;
	return false;
} 
 
function isFilled(elm) {
	if (elm.value == "" || elm.value == null)
	    return false;
	return true;
}

function clickControlOnEnter(event, ctl){
    if (event.keyCode == 13){
        document.getElementById(ctl).click();
        return false;
    }
}
