/**
 *  --------------------------------------------------------------------------------------
 *  Navigator Scripts
 *  ----------------------------------------------------
 *  Modified	April 12, 2010
 *  @version	1.0.0
 *  @author	Chris Donalds <chrisd@navigatormm.com>
 *  ----------------------------------------------------
 *  --------------------------------------------------------------------------------------
 */
function ajaxFunction(srcobj, targetobj, action, table, idfld, flds, values, crit, orderby, topoptions, selectval){
	var ajaxRequest;  // The variable that makes Ajax possible!

	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e) {
			try{
		 		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}catch (e){
		 		// Something went wrong
		 		alert("Your browser failed to handle the AJAX code!");
		 		return false;
			}
		}
	}

	// Create a function that will receive data
	// sent from the server and will update
	// element in the same page.
    // A one way process is assumed if either target or src are null (eg. update record)
	ajaxRequest.onreadystatechange = function(){
        //alert(ajaxRequest.readyState);
		if(ajaxRequest.readyState == 4){
            if(targetobj != null && targetobj != '' && srcobj != null && srcobj != ''){
                var ajaxDisplay = document.getElementById(targetobj);
                //alert(ajaxDisplay.type);

                if (ajaxDisplay.type == "selectmenu" || ajaxDisplay.type == "select-multiple" || ajaxDisplay.type == "select-one") {
                    // single or multiple select selectmenus

                    var retnval = ajaxRequest.responseText;
                    //alert(retnval);

                    // clear the list first
                    while(ajaxDisplay.length > 0){
                        ajaxDisplay.remove(ajaxDisplay.length-1);
                    }

                    // add preset options at top of list
                    if (retnval != "") {
                        if(topoptions != null){
                            var toparray = topoptions.split(",");
                            var nr = toparray.length;
                            for (var i = 0; i < nr; ++i) {
                                var item = toparray[i];
                                var val = item.split("|");
                                ajaxDisplay.options[ajaxDisplay.options.length] = new Option(val[1], val[0]);
                            }
                        }

                        // add rest of retnval items to list
                        var list = ajaxRequest.responseText.split("\n");
                        var nr = list.length;
                        var selectindx = -1;
                        for (var i = 0; i < nr; ++i) {
                            var item = list[i];
                            var val = item.split("|");
                            var ok = true;
                            for (var i2 = 0; i2 < ajaxDisplay.length; i2++) {
                                if (ajaxDisplay.options[i2].value == val[0] && ajaxDisplay.options[i2].value != '') {
                                    ok = false;
                                    break;
                                }
                            }
                            if(ok == true){
                                ajaxDisplay.options[ajaxDisplay.options.length] = new Option(val[1], val[0]);
                                if(selectval == val[0]) selectindx = ajaxDisplay.options.length - 1;
                            }
                        }
                        if(selectindx > -1) ajaxDisplay.selectedIndex = selectindx;
                        return;
                    }
                }
                else {
                    ajaxDisplay.value = ajaxRequest.responseText;
                }
            } else {
                var ajaxRetn = ajaxRequest.responseText;
                return;
            }
		}
	}

	// Now get the value from user and pass it to the server ajaxprocessor.
	var host = window.location.hostname;
    if(host == 'stonehenge' || host == 'badger' || host == 'www.navigatordns.com' || host == 'www.navigatormultimedia.com'){
        var fpath = window.location.pathname;
        var parts = fpath.split('/');
        var dir  = 'http://'+host+'/'+parts[1];
    }else{
        dir = '';
    }

    var pathtoajax = "/tiilib/ajax";

	//var srcval = document.getElementById(srcobj).value;
	var queryString = "?action=" + action + "&table=" + table + "&idfld=" + idfld + "&flds=" + flds + "&vals=" + values + "&crit=" + crit + "&orderby=" + orderby;
    //alert(dir+pathtoajax+"/ajaxprocessor.php"+queryString);

	try {
		ajaxRequest.open("GET", dir + pathtoajax + "/ajaxprocessor.php" + queryString, true);
		
		//alert(dir + pathtoajax + "/ajaxprocessor.php" + queryString);
		ajaxRequest.send(null);
	}
	catch (e) {
		alert("There was a problem accessing the AJAX processor.");
	}
}

function server_http_host(){
    var url = window.location.href;
    if(url.substr(0, 5) == "http:"){
        var protocol = "http://";
    }else if(url.substr(0, 5) == "https"){
        var protocol = "https://";
    }
    url = url.replace(protocol, "");

    var urlExplode = url.split("/");
    var serverName = urlExplode[0];

    serverName = protocol+serverName;
    return serverName;
}



