includeDir = 'includes';


function sendRequestGet(id,link) {

	waitLoading(id);
	var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("get", link, true);
    oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				layOut(id, oXmlHttp.responseText);
			} else {
				layOut(id, "An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		 }            
     };
     oXmlHttp.send(null);

}

//POST request
function sendRequestPost(blockId,formName) {
	
	if (!blockId) blockId = 'sitetun';
	if (!formName) formName = 0;
	
    var oForm = document.forms[formName];
    var sBody = getRequestForm(oForm);
    
    if (!document.getElementById('tm_tm')) {

    	tmbody = '';
    	
    } else {
    	
    	var tmbody = document.getElementById('tm_tm').contentWindow.document.body.innerHTML;
    	    
	    if (tmbody != 'Page_content') {
	    	
	    	tmbody = encodeURIComponent(tmbody);
	    	
	    } else {
	    	
	    	tmbody = '';
    	
    	}
    	
    }
    //alert(tmbody);
    waitLoading(blockId)
    sBody = sBody+'&content='+tmbody;
       // alert(tmbody);
    var oXmlHttp = zXmlHttp.createRequest();
		
    oXmlHttp.open("post", oForm.action, true);
    oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
           	if (oXmlHttp.status == 200) {
               	layOut(blockId, oXmlHttp.responseText);
        	} else {
        		layOut(blockId, "An error occurred: " + oXmlHttp.statusText);
			}
		}
     };
//	 alert(sBody);
	 oXmlHttp.send(sBody);
}
        
function getRequestForm(oForm) {
    var aParams = new Array();
            
	for (var i=0 ; i < oForm.elements.length; i++) {
        var sParam = encodeURIComponent(oForm.elements[i].name);
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
    	aParams.push(sParam);
    } 
            
	aParams.join("&");
	
	return aParams.join("&");
			
}

function layOut(id, sText) {
	
	if (id == 'tm_tm') {
		
		res = document.getElementById('tm_tm').contentWindow.document.body;
		
	} else {
		
		if (id == 'configurator') {
			
			id = 'sitetun';
			
		}
		
		res = document.getElementById(id);
		
	}
	
	res.innerHTML = sText;
   
}



//sitetun функции
function loadUnit(unit, action, target, id) {
	
	if (!id || id  == 'no' || id  == 'nc' || id  == 'fa' || id  == 'hi' || id  == 'ak') {
			
		if (id  == 'no' || id  == 'nc' || id  == 'fa' || id  == 'hi' || id  == 'ak') {
			idlink = '&id='+id;
		} else {
			idlink = '';
		}
		
		id = unit;
		
	} else {
		
		idlink = '&id='+id;
		id = 'tm_tm';
		
	}
	
	link = includeDir+'/'+unit+'.php?action='+action+'&target='+target+idlink;
	
	if ((action == 'load' || action == 'loadcontent') && (target != 'upload_file' && target != 'vacancy' && target != 'upload_price_synergia' && target != 'upload_price_astoreks' && target != 'add_price_type' && target != 'upload_stock' && target != 'newssend' && target != 'metallstock')) {
	
		document.getElementById('tmArea').style.display = '';
		tmLoad();
		
	} else {
		
		//tmLoad();
		document.getElementById('tmArea').innerHTML = '';
		document.getElementById('tmArea').style.display = 'none';
		
	}
	
	sendRequestGet(id,link);
	
}

function deleteItem(target, id) {
	
	if (confirm("Уверены, что хотите удалить страницу?")) {
	
		link = includeDir+'/sitetun.php?action=delete_item&target='+target+'&id='+id;
	
		sendRequestGet('sitetun',link);
	
	}
	
}

function edit(target, id) {
	
	link = includeDir+'/sitetun.php?action=load_'+target+'&target='+target+'&id='+id;
	
	sendRequestGet('sitetun',link);
	
}


function sitetunSave() {
	
	id = 'sitetun';	
	
	sitetunPage_Vars();
	
		
	
}

function tmLoad() {
	
	document.getElementById('tmArea').innerHTML = '<textarea id="_tm" style="height: 170px; width: 100%;"></textarea>';
	generate_tm('_tm', 'Page_content','internal');
	
}

function waitLoading(id) {
    
    document.getElementById(id).innerHTML = 'идет загрузка...';
    
}



//функции загрузки файла-------------------------------------------
function uploadComplete(data)	{
	
	document.getElementById('uploadResult').innerHTML = data;
	document.getElementById('upfile_form_div').innerHTML = '';
		
}
