var http_request = false;

function makeGETRequest(url, handler) 
{
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} 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('Cannot create an XMLHTTP instance');
		return false;
	}
	http_request.onreadystatechange = handler;
	http_request.open('GET', url, true);
	http_request.send(null);
}

function makePOSTRequest(url, query, handler)
{
    var status = false;
    var contentType = "application/x-www-form-urlencoded; charset=UTF-8";

    // Native XMLHttpRequest object. Mozilla, Safari, ...
    if (window.XMLHttpRequest) { 
        http_request = new XMLHttpRequest();
        http_request.onreadystatechange = handler;
        http_request.open("post", url, true);
        http_request.setRequestHeader("Content-Type", contentType);
		http_request.setRequestHeader("Content-length", query.length);                    
        http_request.send(query);
        status = true;

    // ActiveX XMLHttpRequest object. IE
    } else if (window.ActiveXObject) {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
        if (http_request) {
            http_request.onreadystatechange = handler;
            http_request.open("post", url, true);
            http_request.setRequestHeader("Content-Type", contentType);
			http_request.setRequestHeader("Content-length", query.length);            
            http_request.send(query);
            status = true;
        }
    }

    return status;
}

//eef
//sfc

function SetColor(color,bordercolor) {
	document.getElementById('selected'+input_color_id).innerHTML='#'+color;
	document.getElementById('selected'+input_color_id).style.background='#'+color;
	document.getElementById('selected'+input_color_id).style.border='2px solid #'+bordercolor;
	document.getElementById(input_color_id).value=color;
	CloseColorDialog();
}

function Refresh() 
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {	
			if (http_request.responseText == '') eval("location='./'");	
			else eval("location='"+http_request.responseText+"'");	
		} else alert('There was a problem with the request.');
	}
}


		function ClearEditor() {
			if (tinyMCE) {
				if (tinyMCE.getInstanceById('text')) {
				    tinyMCE.execCommand('mceFocus', false, 'text');                    
				    tinyMCE.execCommand('mceRemoveControl', false, 'text');
				}
				tinyMCE.triggerSave();
				document.getElementById('edit_element_form').innerHTML = '';
			}
		}
		
		function EditElementForm(elementId, form_url, title, width, height) 
		{
			ClearEditor();
			$('#'+elementId).dialog('option', 'title', title);
			$('#'+elementId).dialog('option', 'width', width);
			$('#'+elementId).dialog('option', 'height', height);
			$('#'+elementId).dialog('open');	
			makeGETRequest(form_url, showFormContents);
		}
		
		function showFormContents() 
		{
			if (http_request.readyState == 4) {
				if (http_request.status == 200) {			
					document.getElementById('edit_element_form').innerHTML = http_request.responseText;
					if (document.getElementById('text')) {
							setTimeout("tinyMCE.execCommand('mceAddControl', false, 'text')",200);
					} 					
				} else alert('There was a problem with the request.');
			}
		}		
		
		function OpenColorDialog(icid) {
			input_color_id = icid;
			$('#colordialog').dialog('option', 'position', [mouseX,mouseY]);
			$('#colordialog').dialog('option', 'width', 270);
			$('#colordialog').dialog('option', 'height', 400);
			$('#colordialog').dialog('moveToTop');
			$('#colordialog').dialog('open');
		}
		
		function CloseMyDialog() {
			$('#edit_element_form').dialog('close');
			return false;
		};		
		
		function CloseColorDialog() {
			$('#colordialog').dialog('close');
			return false;
		};

//--------------------------------------------------------------------------------------

function closeFormContentsAndSetValues() 
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {	
			setFieldsValues();		
			CloseMyDialog();
		} else alert('There was a problem with the request.');
	}
}

function closeFormContents() 
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {	
			setFields();
			CloseMyDialog();			
		} else alert('There was a problem with the request.');
	}
}

function refreshFormContents() 
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {			
			document.getElementById('edit_element_form').innerHTML = http_request.responseText;
		} else alert('There was a problem with the request.');
	}
}

function GetForm(form_url) 
{
	makeGETRequest(form_url, showFormContents);
}


function SubmitForm(actionrequest, form, handler)
{
	var elements = form.elements;
    var pairs = new Array();
    for (var i = 0; i < elements.length; i++) {
        //if ((name = elements[i].name) && (value = elements[i].value))
        if ((name = elements[i].name && elements[i].id) /*&& (value = document.getElementById(elements[i].id).value)*/) {
        	if (elements[i].type == 'checkbox' && elements[i].checked) {
        		//alert( document.getElementById(elements[i].id).name+' = '+elements[i].value);
        	    pairs.push(name + "=" + elements[i].value); 
        	}
        	else if (elements[i].type != 'checkbox')
        	if(value = document.getElementById(elements[i].id).value)
            	pairs.push(name + "=" + encodeURIComponent(value));
            else
            	pairs.push(name + "= ");
        }
    }
    var q = pairs.join("&");
	//alert(q);
    makePOSTRequest(actionrequest, q, handler); // save data
}

function setFields() 
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {	
			XMLDoc = http_request.responseXML;	
			if (XMLDoc) {
			    var root = XMLDoc.getElementsByTagName('root').item(0);
			   	for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
			       var node = root.childNodes.item(iNode); 
			       for (i = 0; i < node.childNodes.length; i++) {
			          	var sibl = node.childNodes.item(i);	
			          	var elid = document.getElementById(sibl.tagName);
			          	if (sibl.childNodes.item(0)) {
			       	 		if (elid) elid.innerHTML = sibl.childNodes.item(0).data;
			          	} else if (elid) elid.innerHTML = '';
			       }
			    }		
			}	
		} else {
			alert('There was a problem with the request.');
			return false;
		}
	}
	return true;
}

function setFieldsValues() 
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {	
			XMLDoc = http_request.responseXML;	
			if (XMLDoc) {
			    var root = XMLDoc.getElementsByTagName('root').item(0);
			   	for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
			       var node = root.childNodes.item(iNode); 
			       for (i = 0; i < node.childNodes.length; i++) {
			          	var sibl = node.childNodes.item(i);	
			          	var elid = document.getElementById(sibl.tagName);
			          	if (sibl.childNodes.item(0)) {
			          		if (elid) {
			          			if (elid.type == 'radio') elid.checked = 'checked';
			          			else elid.value = sibl.childNodes.item(0).data;
			          		}
			          	} else if (elid) elid.value = '';
			       }
			    }		
			}	
		} else {
			alert('There was a problem with the request.');
			return false;
		}
	}
	return true;
}

function setFieldsAndValues() 
{
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {	
			XMLDoc = http_request.responseXML;	
			if (XMLDoc) {
			    var root = XMLDoc.getElementsByTagName('root').item(0);
			   	for (var iNode = 0; iNode < root.childNodes.length; iNode++) {
			       var node = root.childNodes.item(iNode); 
			       for (i = 0; i < node.childNodes.length; i++) {
			          	var sibl = node.childNodes.item(i);	
			          	var elid = document.getElementById(sibl.tagName);
			          	if (sibl.childNodes.item(0)) {
			          		if (elid) {
			          		//	if (elid.type == 'radio') elid.checked = 'checked';
			          			//else elid.value = sibl.childNodes.item(0).data;
			          			//alert (typeof elid);
			          		//	if (typeof elid == "object") {			          		
			          				//elid.innerHTML = 1;///sibl.childNodes.item(0).data;
			          				SetInnertHtml(sibl.tagName, sibl.childNodes.item(0).data);
			          				//alert('OBJ');
			          		///	}
			          				//alert(sibl.tagName+' = '+sibl.childNodes.item(0).data);
			          		} 
			          	} else if (elid) {
			         		//alert ('tag: '+sibl.tagName);
			          		//elid.value = '';
			          		//if (typeof elid == "object")
			          		SetInnertHtml(sibl.tagName, '');
			          		//if (typeof elid == "object") elid.innerHTML = '';
			          	}
			       }
			    }		
			}	
		} else {
			alert('There was a problem with the request.');
			return false;
		}
	}
	return true;
}

function SetInnertHtml(id, text) {
	oldDiv = document.getElementById(id);
	newDiv = document.createElement(oldDiv.tagName);
	newDiv.id = oldDiv.id;
	newDiv.style.backgroundColor = oldDiv.style.backgroundColor;
	newDiv.bgColor = oldDiv.bgColor;
	newDiv.style.visibility = oldDiv.style.visibility;
	newDiv.className = oldDiv.className;
	newDiv.innerHTML = text;
	oldDiv.parentNode.replaceChild(newDiv, oldDiv);     
}

function html_entity_decode(str) {
  var ta=document.createElement("textarea");
  ta.innerHTML=str.replace(/</g,"&lt;").replace(/>/g,"&gt;");
  return ta.value;
}

function DeleteElement(message, action_url, handler) {
	if (confirm(message)) makeGETRequest(action_url, handler);
	else close();
}

function SaveBoxPosition(pages_id, blocks_id, x, y) {
	makeGETRequest("block.php?pages_id="+pages_id+"&blocks_id="+blocks_id+"&x="+x+"&y="+y, emptyaction);
}

function SaveBoxSize(pages_id, blocks_id, w, h) {
	makeGETRequest("block.php?pages_id="+pages_id+"&blocks_id="+blocks_id+"&w="+w+"&h="+h, emptyaction);
}

function emptyaction() {}
		
function LockBlocks() {
	$(blockids).draggable("destroy");
	$(blockids).resizable("destroy");
	$("#lockblocks").button({icons: {primary: 'ui-icon-locked'}});
	blockslocked = true;
	//$("#lockblocks").button( "option", "label", "Unlock" );
}		

function UnlockBlocks() {
	$(blockids).draggable({scroll: true}); 
	$(blockids).resizable({autoHide: true, handles: 'se, sw'});
	$("#lockblocks").button({icons: {primary: 'ui-icon-unlocked'}});
	blockslocked = false;
	//$("#lockblocks").button( "option", "label", "Lock" );
}

function BlocksLocker() { 
	if (!blockslocked) LockBlocks();
	else UnlockBlocks();
}
