// some stuff
function readObj(sObj){
	var ReqObj=send2URL("POST", "php/read.php", "Obj="+sObj, null, null);
	return ReqObj.responseText;
}

function writeObj(sObj,sData){
	var ReqObj=send2URL("POST", "php/write.php", "Obj="+sObj+"&Dat="+sData, null, null);
	return ReqObj.responseText;
}

function htmlEncode(s) {
        var str = new String(s.replace(/'/g,"''"));
        str = str.replace(/&/g, "&amp;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/"/g, "&quot;");
        return str;
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}

function setCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function delCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



function removeWhitespace(xml) {
	var loopIndex;
	for (loopIndex=0;loopIndex<xml.childNodes.length;loopIndex++) {
		var currentNode=xml.childNodes[loopIndex];
		if (currentNode.nodeType==1)
			removeWhitespace(currentNode);
		if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType==3))
			xml.removeChild(xml.childNodes[loopIndex--]); 
	} 
}

function getDBxml(objAjax){
	if(objAjax==null) return null;
	var tmpXML=null;
	if(typeof objAjax.responseXML.childNodes[1]=="undefined"){
		tmpXML=objAjax.responseXML.childNodes[0];
		removeWhitespace(tmpXML);
	}else
		tmpXML=objAjax.responseXML.childNodes[1];
	return tmpXML;
}

function dbMod(sTbl, sVals, sCond){
	return dbQuery("UPDATE "+sTbl+" SET "+sVals+" WHERE "+sCond);
}

function dbGet(sTbl, sVals, sCond){
	return dbQuery("SELECT "+sVals+" FROM "+sTbl+" WHERE "+sCond);
}

function dbAdd(sTbl, sVals){
	return dbQuery("REPLACE "+sTbl+" SET "+sVals);
}

function dbQuery(sQuery){
	var ReqObj=send2URL("POST", "/xmlSQL.php", "SQL="+encodeURIComponent(sQuery), null, null);
	if(ReqObj.responseText.indexOf("<xml")>=0){
		return ReqObj;	
	}else {
		alert(ReqObj.responseText);
		return null;
	}
}

function insData(sTable, oForm){
	var sValues=frmValues(oForm);
	//alert(sValues);
	var ReqObj=send2URL("POST", "/xmlSQL.php", "SQL=REPLACE "+sTable+" SET "+sValues, null, null);
	if(ReqObj.responseText.indexOf("<xml")>=0){
		oForm.reset();	
	}else {
		alert(ReqObj.responseText);
	}

}

function frmValues(oForm){
	var sValues="";
	for(var i=0; i<oForm.elements.length; i++){
		if(oForm.elements[i].type!="button") 
			if(typeof oForm.elements[i].num!="undefined") 
				sValues+=", "+oForm.elements[i].name+"="+oForm.elements[i].value;
			else if(typeof oForm.elements[i].esc!="undefined") 
				sValues+=", "+oForm.elements[i].name+"='"+escape(oForm.elements[i].value.replace(/'/g,"''"))+"'";
			else 	
				sValues+=", "+oForm.elements[i].name+"='"+oForm.elements[i].value.replace(/'/g,"''")+"'";
			
	} 
	if(sValues!="") sValues=sValues.substr(2);
	return sValues; 
}

// Ajax rutines

function execURL(sURL){
	eval(loadURL(sURL,null,null));
}

// loadURL(sURL)
function loadURL(sURL,sUid,sPwd){
	if(sURL==null) return "";
	var ReqObj=send2URL("GET", sURL, null, sUid, sPwd);
	return ReqObj.responseText;
}

// ------------------------------------------------------------------
// GetHTTPObj() Gets the HTTP object
// ------------------------------------------------------------------
function getAjaxObj(){
 var AjaxCon=false;
 // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			AjaxCon= new XMLHttpRequest();
        } catch(e) {
			AjaxCon= false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	AjaxCon= new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		AjaxCon= new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		AjaxCon= false;
        	}
		}
    }
 return AjaxCon;
}

// ------------------------------------------------------------------
// send2URL(Method, URL, Content,User, Pass) Send reques to a URL
// Method: GET, PUT, POST
// URL: Server URL to send content to
// Content: Data that is to be send
// User & Password
// ------------------------------------------------------------------
function send2URL(sMethod, sURL, sContent, sUser, sPass){
 
 var objAjax=getAjaxObj();
 if(objAjax){
	objAjax.open(sMethod, sURL, false, sUser, sPass);
	objAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=iso-8859-9");
	objAjax.setRequestHeader("referer", window.location.host);
	objAjax.send(sContent);
 }
 
 return objAjax;
}

// ------------------------------------------------------------------
// sendASync2URL(Method, URL, Content,UID, Pass, ResHandler) Send reques to a URL
// Method: GET, PUT, POST
// URL: Server URL to send content to
// Content: Data that is to be send
// User & Password
// ResHandler: Rutine that is to handle the response
// ------------------------------------------------------------------
function sendASync2URL(Method, URL, Content,UID, Pass, ResHandler){
 var objAjax=getAjaxObj();
 if(objAjax){
	objAjax.onreadystatechange=ResHandler;
	objAjax.open(Method, URL, true, User, Pass);
	objAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=iso-8859-9");
	objAjax.setRequestHeader("referer", window.location.host);
	objAjax.send(Content);
 }
 
 return obAjax;
}

