

//---------------General AJAX call -----------------------------
function GetXmlHttpObject(handler)
{ 
    var objXmlHttp=null;

    if (navigator.userAgent.indexOf("MSIE")>=0)
    { 
    var strName="Msxml2.XMLHTTP";
    if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
    {
    strName="Microsoft.XMLHTTP";
    } 
    try
    { 
    objXmlHttp=new ActiveXObject(strName);
    objXmlHttp.onreadystatechange=handler;
    return objXmlHttp;
    } 
    catch(e)
    { 
    alert("Error. Scripting for ActiveX might be disabled");
    return;
    } 
    } 
    if (navigator.userAgent.indexOf("Mozilla")>=0)
    {
    objXmlHttp=new XMLHttpRequest();
    objXmlHttp.onload=handler;
    objXmlHttp.onerror=handler;
    return objXmlHttp;
    }
} 



//-----------------------------------------------------------------------------------------
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}



//-----------------------------------------------------------------------------------------
// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
		
		if (radioObj.type == "checkbox" ) {
		    if (newValue != "") {
		        radioObj.checked = true;
		    
		    }
		    
		} else {
	        var radioLength = radioObj.length;
	        if(radioLength == undefined) {
		        radioObj.checked = (radioObj.value == newValue.toString());
		        return;
	        }
	        for(var i = 0; i < radioLength; i++) {
		        radioObj[i].checked = false;
		        if(radioObj[i].value == newValue.toString()) {
			        radioObj[i].checked = true;
		        }
	        }
	
		}
		
		
		
	
}


//-----------------------------------------------------------------------------------------
function Mid(str, start, len)
{
// Make sure start and len are within proper bounds
    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 instr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length; i++)
            {
                  if (charSearchFor == Mid(strSearch, i, 1))
                  {
                        return i;
                  }
            }
            return -1;
}

//-----------------------------------------------------------------------------------------
function stateChanged() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        populateAddressFields(xmlHttp.responseText);
        
    }
} 



function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}



//-----------------------------------------------------------------------------------------
function setSelectedValues(obj, selectedOptions) {
    var arrSelectedOptions = new Array()
    if (!hasOptions(obj)) { return; }
	
	if (obj.multiple == true) {
	    arrSelectedOptions = selectedOptions.split(",");
	} else {
	    arrSelectedOptions[0] = selectedOptions;
	}
	
	
	for (var i=(obj.options.length-1); i>=0; i--) 
	{ 
	    
	    for (j=0; j <= arrSelectedOptions.length -1; j++) {
	        if (obj.options[i].value == arrSelectedOptions[j]) { obj.options[i].selected = true;}
	    }
	} 
}

//-----------------------------------------------------------------------------------------
function getSelectedValues(obj) {

    var selectedValues = "";
    var arrSelectedValues = []
    for (var i=0; i<=(obj.options.length-1); i++) { 

		if (obj.options[i].selected) {
		    arrSelectedValues[arrSelectedValues.length] = obj.options[i].value;
		}
    }
        return arrSelectedValues;
}

//-----------------------------------------------------------------------------------------
function addOption(obj,text,value,selected) {
	if (obj!=null && obj.options!=null) {
		obj.options[obj.options.length] = new Option(text, value, false, selected);
		}
	}

//-----------------------------------------------------------------------------------------
function clearSelectBox(obj) { 
    
	if (!hasOptions(obj)) { return; }
	for (var i=(obj.options.length-1); i>=0; i--) { 
		obj.options[i] = null; 
		} 
	obj.selectedIndex = -1; 
	} 

//-----------------------------------------------------------------------------------------
function hasOptions(obj) {
	if (obj!=null && obj.options!=null) { return true; }
	return false;
	}
//-----------------------------------------------------------------------------------------

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 left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}



function setVisibility(obj, state) {

    obj.style.visibility = state;


}

//-----------------------------------------------------------------------------------------

function toggleVisibility(obj) {
    if (obj.style.visibility == "hidden") { obj.style.visibility = "visible"; } else { obj.style.visibility = "hidden"; }
}

//------------------------ END OF UTILITY FUNCTIONS ---------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------


















