/****************************************************************************
 *   Radio Control Framework Personal Use and Evaluation License (PUEL)     *
 *                                                                          *
 *   View COPYING for full license details.                                 *
 *                                                                          *
 *   In summary, your allowed to use Radio Control Framework                *
 *      * for personal use or, alternatively,                               *
 *      * for product evaluation.                                           *
 *                                                                          *
 *   In summary, your not allowed to use Radio Control Framework            *
 *      * for commercial, distribution, or modification purposes.           *
 *      * for government purposes.                                          *
 *                                                                          *
 *   RemoteHams provides this product "as is" without warranty of any kind, *
 *   either expressed or implied, including, but not limited to, the        *
 *   implied warranties of merchantability and fitness for a particular     *
 *   purpose. The entire risk as to the quality and performance of the      *
 *   product is with you. Should it prove defective, you assume the cost    *
 *   of all necessary servicing, repair, or correction. In addition,        *
 *   RemoteHams shall be allowed to provide updates to the product in       *
 *   urgent cases. You are then obliged to install such updates.            *
 *                                                                          *
 *   Copyright (C) 2008 by Brandon Hansen                                   *
 *   kg6ypi@remotehams.com                                                  *
 *                                                                          *
 ****************************************************************************/

//cross-browser supported XML Handler
var getHttpRequest = false;
if(window.XMLHttpRequest) {
     getHttpRequest = new XMLHttpRequest();
} else if(window.ActiveXObject) {
     getHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
//cross-browser supported XML Handler
var postHttpRequest = false;
if(window.XMLHttpRequest) {
     postHttpRequest = new XMLHttpRequest();
} else if(window.ActiveXObject) {
     postHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}

// deptrecated, to be removed here for backword compatibility
function loadURL(szURL, szID) {
	getURL(szURL, szID);
}

function redirectURL(szURL) {
    // if the browser can do it, use replace to preserve back button
    if(javascriptVersion1_1) {
		window.location.replace(szURL);
    } else {
		window.location = szURL;
    }
}

function getURL(szURL, szID) {
	if(getHttpRequest) {
		var data = document.getElementById(szID);
		data.innerHTML = "<div class='box_info'><img src='css/images/icons/loading.gif' alt='Loading AJAX Call...' /></div>";

		getHttpRequest.open("GET",szURL);

		getHttpRequest.onreadystatechange = function() {
			if(getHttpRequest.readyState==4)
				data.innerHTML = getHttpRequest.responseText;
		}
		getHttpRequest.send(null);
	} else {
		var data = document.getElementById(szID);
		data.innerHTML = "Sorry, AJAX calls not supported by your web browser.";
	}
}

function postURL(szParams, szURL, szID, szLoadURL) {
	if(postHttpRequest) {
		var data = document.getElementById(szID);
		var oldHtml = data.innerHTML;
		data.innerHTML = "<div class='box_info'><img src='css/images/icons/posting.gif' alt='Loading AJAX Call...' /></div>";
	
		postHttpRequest.open("POST", szURL, true);

		postHttpRequest.onreadystatechange = function() {
			if(postHttpRequest.readyState==4 && postHttpRequest.status == 200) {
					getURL(szLoadURL,szID);
			}
		}		
		//Send the proper header information along with the request
		postHttpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		postHttpRequest.setRequestHeader("Content-length", szParams.length);
		postHttpRequest.setRequestHeader("Connection", "close");
		postHttpRequest.send(szParams);
	} else {
		var data = document.getElementById(div);
		data.innerHTML = "<div class='box_info'>Sorry, AJAX calls not supported by your web browser.</div>";
	}
}

function GetParms(szType,szID,szData) {
	if (szType=='setFrequency') {
		return "setFrequency="+GetFormInput(szID);
	} else if (szType=='setButton') {
		return "setButton="+GetFormInput(szID)+"&setData="+szData;
	} else if (szType=='setDropdown') {
		return "setDropdown="+GetFormInput(szID)+"&setData="+szData;
	} else if (szType=='setSlider') {
		return "setSlider="+GetFormInput(szID)+"&setData="+szData;
	} else if (szType=='setLogin') {
		return "setLogin=true&"+szData;
	} else if (szType=='setTuneMode') {
		return "setTuneMode=true&setData="+szData;		
	}
	return "error";
}

function SetIMG(szURL, szID) {
	var obj = document.getElementById(szID);
	obj.src=szURL;
}

function GetFormInput(szID) {
	var obj = document.getElementById(szID);
	return obj.value;
}

function SetFormInput(szID, szData) {
	var obj = document.getElementById(szID);
	obj.value = szData;   // Set new value
}

function GetFormDropdown(obj) {
	var n = obj.selectedIndex;
	var text = obj[n].text;
	var val = obj[n].value; 
	return val;
}

function GetFormDropdownKey(obj) {
	var n = obj.selectedIndex;
	return n;
}

function hideDiv(szDivID) {
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = "hide";
	   document.layers[szDivID].style.width = 0 + 'px';
	   document.layers[szDivID].style.height = 0 + 'px';
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = "hidden";
		
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = "hidden";
	    document.all[szDivID].style.width = 0 + 'px';
	    document.all[szDivID].style.height = 0 + 'px';		
    }	
	if(document.getElementById)	  {
		var obj = document.getElementById(szDivID);
		obj.style.width = 0 + 'px';
		obj.style.height = 0 + 'px';
		obj.style.padding = 0;
		obj.style.margin = 0;
	}		
}

function showDiv(szDivID, szWidth, szHeight) {
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = "show";
	   document.layers[szDivID].style.width = szWidth + 'px';
	   document.layers[szDivID].style.height = szHeight + 'px';
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = "visible";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = "visible";
	    document.all[szDivID].style.width = szWidth + 'px';
	    document.all[szDivID].style.height = szHeight + 'px';
    }	
	if(document.getElementById)	  {
		var obj = document.getElementById(szDivID);
		obj.style.width = szWidth + 'px';
		obj.style.height = szHeight + 'px';
	}
}
