﻿var funcName="";
var parameters="";
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\nIt's about time to upgrade don't you think?");
	}
	return false;
}

//Our XmlHttpRequest object to get the auto suggest
var treeAjax = getXmlHttpRequestObject();

//sample: var test=request(your url)
//Starts the AJAX request.
function request(url,functionName,parameter)
{
	funcName=functionName;
	parameters=(parameter)?","+parameter:"";
	if (treeAjax.readyState == 4 || treeAjax.readyState == 0) {
	
		treeAjax.open("GET", url, true);
		treeAjax.onreadystatechange = handleTree;
		treeAjax.send(null);
		return true;
	}
	return false;
}
//Called when the AJAX response is returned.
function handleTree() {
	
	if (treeAjax.readyState == 4) {
		//alert(funcName+"('"+treeAjax.responseText+"'"+parameters+");");
		eval(funcName+"('"+treeAjax.responseText+"'"+parameters+");");
		return true;
	}
	return false;
}


// ------- ezafeh nemodan block pas az zadan button + 
var urldefault=0;
function addBlock(Id)
{
	urldefault=urldefault+1;
	number = Math.random();
	number = Math.round(number*100000000);
	var ni = document.getElementById(Id);
	var newdiv = document.createElement('div');
	var divIdName = 'newDiv'+number;
	newdiv.setAttribute('id',divIdName);
	switch(Id){
		case 'NurlBlock':
			htmlValue='<div class="headerPos"><div class="addIcon"><a href="#" onclick="removeElement(\''+Id+'\',\''+number+'\')"><img src="images/tree/delete.gif" alt="حذف" /></a></div><span>آدرس پایگاه: </span><input type="text" name="NUrl[]" id="Url'+number+'" value="" tabindex="4" maxlength="100" onblur="checkurl(this.id,address,typepage,sitelang,siteid)" title="url" /><input type="radio" name="UrlDefault" id="UrlDefault" title="آدرس پیش فرض" value="n'+urldefault+'" /></div>';
		break;
		case 'NemailBlock':
			htmlValue='<div class="headerPos"><div class="addIcon"><a href="#" onclick="removeElement(\''+Id+'\',\''+number+'\')"><img src="images/tree/delete.gif" alt="حذف" /></a></div><span>پست الكترونيك: </span><input type="text"  id="NEmail'+number+'" name="NEmail[]" value="" tabindex="5" onblur="CheckEmail(this.id);"  maxlength="100" title="mail" /></div>';
		break;

		case 'NlangBlock':
			htmlValue='<div class="rPos"><span>زبان سایت: </span>\<select name="NLanguage[]" id="Language" tabindex="24">\
	<option value="">-----</option><option value="252">آذري</option><option value="27">\
	اردو</option><option value="270">ارمني</option><option value="262">\
	ازبكي</option><option value="2">آلماني</option><option value="28">\
	آلبانيائي</option><option value="3">اسپانيايي</option><option value="14">\
	اسكانديناوي</option><option value="8">اندونزيايي</option><option value="26">\
	انگليسي</option><option value="4">ايتاليايي</option><option value="255">\
	برمئي</option><option value="18">بلغاري</option><option value="16">\
	بنگالي</option><option value="17">بوسنيايي</option><option value="21">\
	پرتغالي</option><option value="258">پشتو</option><option value="23">\
	تاجيكي</option><option value="253">تاميلي</option><option value="1">\
	تركي</option><option value="12">چيني</option><option value="259">\
	دانماركي</option><option value="263">دري</option><option value="15">\
	روسي</option><option value="22">روماني</option><option value="13">\
	ژاپني</option><option value="7">سوئدي</option><option value="11">\
	سواحيلي</option><option value="9">طايي</option><option value="24">\
	عربي</option><option value="269">عبري</option><option value="25">\
	فارسي</option><option value="5">فرانسوي</option><option value="256">\
	فنلاندي</option><option value="261">فولاني</option><option value="267">\
	فيليپيني</option><option value="272">قزاقي</option><option value="19">\
	كردي</option><option value="268">كره اي</option><option value="10">\
	گجراتي</option><option value="271">گرجي</option><option value="265">\
	لهستاني</option><option value="20">مالايي</option><option value="6">\
	نروژي</option><option value="257">هائوسي</option><option value="260">\
	هلندي</option><option value="254">هندي</option></select>\
	</div>\
	<div class="pos">\
	<div class="addIcon"><a href="#" onclick="removeElement(\''+Id+'\',\''+number+'\')"><img src="images/tree/delete.gif" alt="حذف" /></a></div>\
	<span>لينك: </span><input type="text"  id="LanguageAddress" name="NLanguageAddress[]" value="" tabindex="25" maxlength="250" onblur="fullUrl(this.id);" title="fullurl" /></div>';
		break;
	}
	newdiv.innerHTML = htmlValue;
	ni.appendChild(newdiv);
}

// ------- hazf nemodan block pas az zadan button - 

function removeElement(blockId,Id) {

	switch (blockId){
		case "NemailBlock":
			if(document.getElementById('HEmail'+Id)){
				divId="DivEmail"+Id;
				document.getElementById('HEmail'+Id).name="DEmail[]";
			}
			else divId="newDiv"+Id;
		break;
		case "NurlBlock":
			if(document.getElementById('HUrl'+Id) && document.getElementById('HUrl'+Id).name=='HUrl[]'){
				if(document.getElementById('UrlDefault'+Id).checked!=true){
					document.getElementById('HUrl'+Id).name="DUrl[]";
					document.getElementById('Url'+Id).name="";
					document.getElementById("Url"+Id).style.background='#FFCEB7';
					document.getElementById('UrlDefault'+Id).disabled='disable';
				}
				return false;
			}
			else if(document.getElementById('HUrl'+Id) && document.getElementById('HUrl'+Id).name == 'DUrl[]'){
				document.getElementById('HUrl'+Id).name="HUrl[]";
				document.getElementById('Url'+Id).name="Url[]";
				document.getElementById("Url"+Id).style.background='#ffffff';
				document.getElementById('UrlDefault'+Id).disabled='';
				return false;
			}
			else divId="newDiv"+Id;
		break;
		case "NlangBlock":
			if(document.getElementById('HLang'+Id)){
				divId="Lang"+Id;
				document.getElementById('HLang'+Id).name="DLang[]";
			}
			else divId="newDiv"+Id;
		break;
	}
	var d = document.getElementById(blockId);
	var olddiv = document.getElementById(divId);
	d.removeChild(olddiv);
	return true;
}

// namaish image bozorg dar hengam raftan bar roy img

function showBigImg(e,divId,imgSrc)
{
	if(imgSrc){
		document.getElementById('bigImage').src='';
		document.getElementById('bigImage').src=imgSrc;
		document.getElementById(divId).className='show';
		y = (e.clientY-120)+document.documentElement.scrollTop;
		document.getElementById(divId).style.top=(y+'px');	
	}
	return false;
}


function hiddBigImg(divId)
{
	document.getElementById(divId).className='hidd';
}
function removeCategory(Id){
	var olddiv = window.opener.document.getElementById('Cat'+Id);
	window.opener.document.getElementById('NCatBlock').removeChild(olddiv);

}
function addCategory(OBJ)
{
	var Id = OBJ.value;
	var Lable = OBJ.alt;
	doc=document.getElementById('a_'+Id);
	if(doc.className=="selectLi"){
		doc.className="leafs";
		removeCategory(Id);
		return false;
	}
	doc.className="selectLi";
	var divIdName = 'Cat'+Id;
	if(window.opener.document.getElementById(divIdName)){
		return false;
	}
	var inputList = document.getElementsByTagName('input');
	for (i=0; i<inputList.length; i++){
		if (inputList[i].id == Id){
			current = i;
			break;
		}
	}
	setOthers(current);
	var ni = window.opener.document.getElementById('NCatBlock');
	var newdiv = window.opener.document.createElement('div');
	newdiv.setAttribute('id',divIdName);
	htmlValue='<div class="pos">\
	<span>لینک: </span><input type="text" id="Catlink'+Id+'" name="CatLink[]"  tabindex="7" maxlength="250" onblur="fullUrl(this.id);" title="fullurl" /></div>\
	<div class="headerPos" id="subject">\
	<span>عنوان: </span><input type="hidden" name="CatId[]" id="CatId_'+Id+'" value="'+Id+'" alt="'+current+'" /><input type="text" id="Subject" name="Subject" value="'+Lable+'" tabindex="6" readonly="readonly" /></div>';
	newdiv.innerHTML = htmlValue;
	ni.appendChild(newdiv);
	return true;
}

var cHeight=20;
function windowForm(status){
	if(cHeight==20 || status==1){
	cHeight=200;
	document.getElementById("arrow").src="images/horizontal.png";
	}
	else{
		cHeight=20;
		document.getElementById("arrow").src="images/vertical.png";
	}
	document.getElementById("frameContent").style.height=cHeight+"px";
	document.getElementById("mainContent").style.bottom=cHeight+5+"px";
}

function windowOpen()
{
var cats="";
	if(document.getElementsByName('CatId[]'))
	{
		var arr = document.getElementsByName('CatId[]');
		var temp = Array();
		var count = arr.length;
		for (i=0; i<count; i++)
		{
			var min = 10000000000;
			index = -1;
			for (j=0; j<count; j++)
			{
				if (parseInt(arr[j].alt) < min && !exist(j,temp))
				{
					min = parseInt(arr[j].alt);
					index = j;
				}
			}
			if (index != -1)
				temp[i] = index;
		}
		
		for (i=0; i<count; i++)
		{
			cats+=arr[temp[i]].value;
			cats+=(i<(count-1))?",":"";
		}
	}
	window.open('?req=edittree&lang='+this.lang+'&catarray='+cats,'category','top=150px,left=150px,width=320px,height=280px,scrollbars=yes')
}

function exist(value,handle)
{
	if (handle.length){
		var len = handle.length;
		for (k=0; k<len; k++)
			if (handle[k] == value)
				return true;
	}
	return false;
}


function setFrame(Height){
	document.getElementById('framecontentBottom').style.height =Height+'px';
	document.getElementById('maincontent').style.bottom =Height+'px';
	window.document.body.style.paddingBottom =Height+'px';
}


function setOthers(currIndex)
{
	var inputList = document.getElementsByTagName('input');
	for (i=0; i<inputList.length; i++)
	{
		if (inputList[i].checked && i != currIndex && window.opener.document.getElementsByName('CatId[]').length)
			if (i >= currIndex)
			{
				inputId = inputList[i].id;
				window.opener.document.getElementById('CatId_'+inputId).alt = i;
			}
	}
}

function checkSelected(idSelect)
{
	if (window.opener.document.getElementsByName('CatId[]').length)
	{
		if (window.opener.document.getElementById('CatId_'+idSelect))
			return true;
		return false;
	}
	return false;
}


function showMenu()
{
	if (document.getElementById('languages').className == 'hidd')
		document.getElementById('languages').className = 'show';
}

function hideMenu()
{
	if (document.getElementById('languages').className == 'show')
		document.getElementById('languages').className = 'hidd';
}