//Copyright 2003-2010 by Ars Datum, Inc. All rights reserved.

/* ******************* */
/* SETUP AJAX OBJECTS
/* ****************** */

//AJAX finish command before returning
function ajaxReturnRequest(url, aObject, aType) {
    var http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            //http_request.overrideMimeType('text/xml');
            // Don't know why but the above is causing problem
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        //alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }

    http_request.open('GET', url, false);
    http_request.send(null);

    if (aType == 'text')
    	aObject.text = http_request.responseText;
    else if (aType == 'xml')
	 	aObject.xml = getXMLDOMObject(http_request.responseText);
	 else if (aType == 'html')
		document.getElementById(aObject).innerHTML = http_request.responseText;
}

//create a XMLDOM object, passed a string variable that is xml format
function getXMLDOMObject(xmlFormat) {
	var xml;

	if (window.ActiveXObject) { // IE
		//store the XML format into .xml field i created in the object
		xml = new ActiveXObject("Microsoft.XMLDOM");
		xml.async="false";
		xml.loadXML(xmlFormat);		//loads a string that is xml format
	} else { // Mozilla, Safari, ...
		var parser = new DOMParser();
		xml = parser.parseFromString(xmlFormat, "text/xml");
	}

	return xml;
}

//*******************************************************************************

//check if image file exists
function CheckImageExists(url) {
	var blnReturn = true;
	var img = new Image;
	img.src = url;
	img.onerror = function () { blnReturn = false; };

	return blnReturn;
}

//checks if a string is numeric value
function IsNumeric(strString) {
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;

	if (strString.length == 0) return false;

	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
			break;
		}
	}

	return blnResult;
}

//special form submission ajax javascript
function AjaxSafeParameter(theParameter) {
	if (theParameter > '') {
		theParameter = escape(theParameter);
	}

	return theParameter;
}