/***********************************************************************
* Website Pyro Ajax Engine
* Author - Al Wolfson
* Changelog:
* 3/24/2008 - added checkObj to loop through all nodes of target obj
* 3/20/2008 - added optional updateField, no longer required to use callback functions
* 4/24/2007 - added support for POST form submission and form field checking
***********************************************************************/

function makeHttpRequest(link, callback_function, return_xml,parameters,updateField){
	var http_request = false;
	
	if (window.XMLHttpRequest){ // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	}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('Unfortunately your browser doesn\'t support this feature.');
		return false;
	}
	http_request.onreadystatechange = function(){
		if (http_request.readyState == 4){
			if (http_request.status == 200){
				if (typeof updateField == "undefined"){
					if (return_xml)
						eval(callback_function + '(http_request.responseXML)');
					else eval(callback_function + '(http_request.responseText)');
				}else{
					if (return_xml)
						document.getElementById(updateField).innerHTML = http_request.responseXML;
					else document.getElementById(updateField).innerHTML = http_request.responseText;
				}
			} else alert('There was a problem with the request.(Code: ' + http_request.status + ')');
		}
	}
	http_request.open('POST', link, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function getValues(obj,link,callback,return_xml,updateField){
	poststr=checkObj(obj);
	makeHttpRequest(link,callback,return_xml,poststr,updateField);
}

function checkObj(obj){
	var poststr='';
	var j=0;
	poststr+=checkSingleObj(obj);
	for (j=0; j<obj.childNodes.length; j++)
		poststr+=checkObj(obj.childNodes[j]);
	return poststr;
}

function checkSingleObj(obj){
	var poststr='';
	if (obj.tagName == "INPUT"){
		if (obj.type == "text")
			poststr += obj.name + "=" + escape(obj.value) + "&";
		else if (obj.type == "checkbox"){
			if (obj.checked)
				poststr += obj.name + "=" + escape(obj.value) + "&";
			else poststr += obj.name + "=&";
		}else if (obj.type == "radio"){
			if (obj.checked)
				poststr += obj.name + "=" + escape(obj.value) + "&";
		}else if (!obj.disabled)
	   		poststr += obj.name + "=" + escape(obj.value) + "&";
	}else if (obj.tagName == "SELECT"){
		var sel = obj;
		poststr += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
	}else if (obj.tagName == "Button")
		poststr += obj.name + "=" + escape(obj.value) + "&";
	else if (obj.type == "textarea")
		poststr += obj.name + "=" + escape(obj.value) + "&";
	else if (obj.disabled)
		obj.disabled=false;
	return poststr;
}

//process individual elements
function adminEdit(text){
   document.getElementById('adminEditField').innerHTML = text;
   var settings = new WYSIWYG.Settings(); 
   settings.ImagesDir = "/admin/images/"; 
   settings.PopupsDir = "/admin/popups/"; 
   settings.CSSFile = "/admin/styles/wysiwyg.css";
   settings.ImagePopupFile = "/admin/addons/imagelibrary/insert_image.php";
   settings.ImagePopupWidth = 600;
   settings.ImagePopupHeight = 245;
   settings.DocPopupFile = "/admin/addons/imagelibrary/insert_file.php";
   settings.DocPopupWidth = 600;
   settings.DocPopupHeight = 245;
   settings.Width = "100%";
   settings.Height = "400px";
   WYSIWYG.setSettings('PageContent', settings); 
   WYSIWYG._generate('PageContent'); 
}
function adminEdit2(text){
   document.getElementById('adminEditField2').innerHTML = text;
   var settings = new WYSIWYG.Settings(); 
   settings.ImagesDir = "/admin/images/"; 
   settings.PopupsDir = "/admin/popups/"; 
   settings.CSSFile = "/admin/styles/wysiwyg.css";
   settings.ImagePopupFile = "/admin/addons/imagelibrary/insert_image.php";
   settings.ImagePopupWidth = 600;
   settings.ImagePopupHeight = 245;
   settings.DocPopupFile = "/admin/addons/imagelibrary/insert_file.php";
   settings.DocPopupWidth = 600;
   settings.DocPopupHeight = 245;
   settings.Width = "100%";
   settings.Height = "400px";
   WYSIWYG.setSettings('Secondary_Content', settings); 
   WYSIWYG._generate('Secondary_Content'); 
}

/***********************************************
* Misc
***********************************************/
//display/hide div
function toggleLayer( whichLayer ){
	var elem, vis;
	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( whichLayer );
	else if( document.all ) // this is the way old msie versions work
		elem = document.all[whichLayer];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[whichLayer];
	vis = elem.style;
	// if the style.display value is blank we try to figure it out here
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}
function popUp(URL){
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=300,left=490,top=362');");
}
function popUpPlayer(URL,width,height){
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width='+width+',height='+height+',left=490,top=362');");
}
function addBookmark(title,url){
	if (window.sidebar)
		window.sidebar.addPanel(title, url,""); 
	else if( document.all )
		window.external.AddFavorite( url, title);
	else if( window.opera && window.print )
		return true;
}
function insertAtCursor(myField, myValue){
	if (document.selection){
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}else if (myField.selectionStart || myField.selectionStart == '0'){
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	}else myField.value += myValue;
}
function insertspecial(myField,tag){
	var space=" ";
	if (tag=='URL'){
		var url = prompt('Enter the URL:','http:\/\/');
		if (! url){ return; }
		var txt = prompt('Enter the text for the link or press enter to use the URL as the text:','Link Text');
		if (!txt || (txt == 'Link Text')){ txt = URL; }
		insertAtCursor(myField,"[URL="+url+"]"+txt+"[/URL]");
	}else{
		var text=prompt("Type the text you want to enter:","");
		if (text != null){
		        var text_to_insert = '['+tag+']'+text+'[/'+tag+']';
		        insertAtCursor(myField, text_to_insert);
		}
	}
	myField.focus();
}
function displayOrder_Dropdown(pageLink){
	window.location = pageLink + '?sortOrder=' + document.displayOrder.sortOrd.options[document.displayOrder.sortOrd.selectedIndex].value;
}
function ValidateLogin(theForm){
	var why = "";
	why += isEmpty(theForm.access_login.value,'Login');
	why += isEmpty(theForm.access_password.value,'Password');
	if (why != ""){
		alert(why);
		return false;
	}
	return true;
}
function ValidateEmail(theForm){
	var why = "";
	why += checkEmail(theForm.ml_Email.value);
	if (why != ""){
		alert(why);
		return false;
	}
	return true;
}