
/*
#########################################################
# ajax.js
#
# javascript functions to handle AJAX requests
#
# Copyright:		S-Inside
# Project:			CMS
# Platform:			Javascript
# Date created : 	22-10-08
#
#########################################################
*/

var http = createRequestObject();
var $m_objFormField, $m_objAjaxAction
var $p_sReturn = "";
var $m_bExternalLinkValid = false;

function getURLFromPage () {
	var $vID = document.forms['menu'].PaginaID.value;
	$m_objFormField = document.forms['menu'].URL
	$m_objFormField.value = "";
	$m_objFormField.readOnly = true;
	
	if ((isNaN($vID)) || ($vID == "")) {
		// text id is given, use that for value
		if ($vID.length == 0) {
			alert("Verwijst naar mag niet leeg zijn!");
		} else {
			if ($vID.substring(0, 4) == "www.") {
				$sURL = "ajax.php?sAction=getpagesexternal&id="+$vID;
				// alert($sURL);
				http.open('post', $sURL);
			    http.onreadystatechange = handleResponse;
			    http.send(null);
			} else if ($vID == "artikelen.php") {
				
				$m_objFormField.value = $vID;
				$sURL = "ajax.php?sAction=getarticlelists";
				// alert($sURL);
				http.open('post', $sURL);
			    http.onreadystatechange = handleResponse;
			    http.send(null);
			    
			} else {
				$m_objFormField.value = $vID;
			}
		}
	} else {
		// numeric value, check for value
		if ($vID > 0) {
			// get url from the dbase
			$sURL = 'ajax.php?sAction=urlfrompage&id='+$vID;
			// alert($sURL);
			http.open('post', $sURL);
		    http.onreadystatechange = handleResponse;
		    http.send(null);
		    
	    } else if (($vID == 0) || ($vID == -2)) {
		    // new page or form, set url like name
		    $m_objFormField.value = "index.php?id="+document.forms['menu'].Naam.value;
		    
	    } else if ($vID == -1) {
		    // external link, url is writable
		    $m_objFormField.readOnly = false;
	    }
	}
}

function getURLFromExternalPage ($oSelectebox) {
	$m_objFormField = document.forms['menu'].URL
	$m_objFormField.readOnly = true;
	$m_objFormField.value = "index.php?extid="+$oSelectebox.options[$oSelectebox.selectedIndex].text;
	
	var $aOption = $oSelectebox.value.split('#');
	document.forms['menu'].ExternalSite.value = $aOption[0];
	document.forms['menu'].ExternalID.value = $aOption[1];
}

function alertContents() {
   if (http.readyState == 4) {	   
       if (http.status == 200) {
           if (http.responseText) {
	           $m_bExternalLinkValid = true;
           }
       } else {
	       alert('De opgegeven URL bij Verwijst naar is niet geldig!');
	       $m_bExternalLinkValid = false;
       }
   }
}

function sendRequest($p_sAction, $p_vWhat, $p_sName, $p_sScript) {
	var $sGetName = "", $sURL = "", $sScript = "ajax.php";
	if (($p_sName) && ($p_sName.length > 0)) {
		$sGetName = '&name='+$p_sName;
	}
	if (($p_sScript) && ($p_sScript.length > 0)) {
		$sScript = $p_sScript;
	}
	if ($p_vWhat.value) {
		$sURL = $sScript+'?sAction='+$p_sAction+'&id='+$p_vWhat.value+$sGetName;	
	} else {
		$sURL = $sScript+'?sAction='+$p_sAction+'&id='+$p_vWhat+$sGetName;	
	}
	// alert($sURL);
	// window.location = $sURL;
	
	http = createRequestObject();
	http.open('post', $sURL);
    http.setRequestHeader("connection", "close");
	http.onreadystatechange = handleResponse;
    http.send(null);
}

function setActionViaAjax ($p_sAction, $p_vWhat, $p_sName, $p_sScript) {
	var $sGetName = "", $sURL = "", $sScript = "ajax.php";
	$m_objAjaxAction = "return";
	if (($p_sName) && ($p_sName.length > 0)) {
		$sGetName += '&name='+$p_sName;
	}
	/*if (($p_sCustomField) && ($p_sCustomField.length > 0)) {
		$sGetName += '&customfield='+$p_sCustomField;
	}*/
	if (($p_sScript) && ($p_sScript.length > 0)) {
		$sScript = $p_sScript;
	}
	if (($p_vWhat) && ($p_vWhat.length > 0)) {
		$sURL = $sScript+'?sAction='+$p_sAction+'&id='+$p_vWhat+$sGetName;	
	}
	// alert($sURL);
	// window.location = $sURL;
	if ($sURL.length > 0) {
		http.open('post', $sURL);
		http.onreadystatechange = handleResponse;
		http.send(null);
		
		if ($p_sReturn.length > 0) {
	    	return true;
	    }
	} else {
		return false;
	}
}

function createRequestObject() {
    var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;

}

function handleResponse() {
	var $sReturn = "";
	if (http.readyState == 1) {
    	document.body.style.cursor = "wait";
    }
    
	// alert(http.getAllResponseHeaders());
	
    if (http.readyState == 4){
    	if (http.status == 200) {
	        var response = http.responseText;
	        var update = new Array();
			
			if (response) {
				if (response.indexOf('|') != -1) {
					// insert respone into given document element as innerHTML
					update = response.split('|');
		            // alert(update[1]);
		            if (document.getElementById(update[0])) {
		            	document.getElementById(update[0]).innerHTML = update[1];
		            } else {
		            	alert(document.getElementById(update[0])+" does not exist");
		            }
		            if ((update[2]) && (update[2].length > 0)) {
		            	Sortable.create('lijst'+update[2], {tree:true,scroll:window});
	            	}
		            if ((update[3]) && (update[3].length > 0)) {
		            	document.getElementById(update[3]).style.background = 'green';
	            	}
		        } else if ($m_objFormField) {
			        // insert respone into a formfield as value
			        $m_objFormField.value = response;
			        
		        } else if ($m_objAjaxAction) {
		        	// return response to the requisting function
		        	if ($m_objAjaxAction = "return") {
		        		$p_sReturn = response;
		        	}
		        }
	        } else {
	        	alert("No response!");
	        }
    	} else {
    		alert("HTTP error: "+http.status);
    	}
        document.body.style.cursor = "default";
    }
}
