
// initial variable for Ajax object /////
var AjaxStarted = false;
/////////////////////////////////////////

var ie4 = false; if(document.all) { ie4 = true; }
function getobject(id) { 
  if (ie4) { 
    return document.all[id]; 
  }else { 
    return document.getElementById(id); 
  }
}


function deleteItem(c, f, r, msg) {
	if (confirm(msg, '')) {
		document.getElementById(r).value = c;
		document.getElementById(f).submit();
	}
}

function confirmRedirectURL(url, msg) {
	if(confirm(msg, ''))
		document.location.href = url;
}

function redir(url){
	window.location = url;	
}

function showhide_obj(arr) {
	var arrsplit = arr.split(",");
	for (i=0; i < arrsplit.length; i++) {
		var item_tr = getobject(arrsplit[i]);
		if (item_tr) { 
			if (item_tr.style.display == "none") {
				item_tr.style.display = "";
			} else {
				item_tr.style.display = "none";
			}
		}
	}
}

function show_hide_obj(arr,b) {
	var arrsplit = arr.split(",");
	for (i=0; i < arrsplit.length; i++) {
		var item_tr = getobject(arrsplit[i]);
		if (item_tr) { 
			if (b == 1) {
				item_tr.style.display = "";
			} else {
				item_tr.style.display = "none";
			}
		}
	}
}

function lis(a,b){
	tagss=document.getElementsByTagName("li");
	for(f=0;f<tagss.length;f++){
		gaa=tagss[f].getAttribute("id");
		if(gaa){
			if(gaa==a)
				if(b=="selected"){ 
					tagss[f].style.backgroundColor="white";
					tagss[f].style.borderBottom="1px solid white";
				}else{ 
					tagss[f].style.backgroundColor="#f4f4f4";
					tagss[f].style.borderBottom="1px solid #90C320";
				}
		}
	}
}

function goBack(coords,arr){
	var editorssplit = arr.split(",");
	for (esi=0; esi < editorssplit.length; esi++) {
		var ed = getobject(editorssplit[esi]);
		if(ed){
			ed.style.marginLeft=coords;
			ed.style.marginTop=coords;
		}
	}
}

function bbbb(pref,master,st){
	tags=document.getElementsByTagName("div");
	for(vvv=0;vvv<tags.length;vvv++){
		ga=tags[vvv].getAttribute("id");
		if(ga)
			if(st=="")
				if(ga.indexOf(pref+""+master)!=-1){
					tags[vvv].style.display="";}
			else
				if(ga.indexOf(pref+""+master)==-1&&ga.indexOf(pref)!=-1){
					tags[vvv].style.display="none";	}
	}
}

function switchPest(){
	tags=document.getElementsByTagName("div");
	i=-1;
	for(hei=0;hei<100;hei++){
		if(getobject("htmleditor_"+hei)){
			getobject("htmleditor_"+hei).style.marginLeft="-1000px";
			getobject("htmleditor_"+hei).style.marginTop="-9999px";
		}
	}
	for(t=0;t<tags.length;t++){
		ga=tags[t].getAttribute("id");
		if(ga){
			if(ga.indexOf("p_")==0){
				i++;
				if(ga==switchPest.arguments[0]){
					getobject(ga).style.display="";
					lis("tab"+i,"selected");
					bbbb("_savebar_",switchPest.arguments[0],"");
					bbbb("_pictures_",switchPest.arguments[0],"");
					if (switchPest.arguments.length > 1)
						goBack("0px",switchPest.arguments[1]);
				}else{
					getobject(ga).style.display="none";
					lis("tab"+i,"");
					bbbb("_savebar_",switchPest.arguments[0],"none");
					bbbb("_pictures_",switchPest.arguments[0],"none");
				}
			}
		}
	}
}

function getFrameData(form){
	for(i=0;i<form.childNodes.length;i++){
		if(form.childNodes[i].nodeName=="IFRAME"){
			var splitgetname = form.childNodes[i].src.split("&");
			var splitgetname2 = splitgetname[0].split("=");
			window.frames[form.childNodes[i].name].getCleanHTML();
			var getinputvalue = window.frames[form.childNodes[i].name].document.getElementById(splitgetname2[1]).value;
			var val=form[splitgetname2[1]];
			val.value=getinputvalue;
			if(val.value.indexOf("<p><br /></p>")==4&&val.value.length==19)
				val.value="";
		}
	}
}

function addEditPest(id,title,url){
	
	if(!AjaxStarted){
	
		var tags=document.getElementsByTagName("li");
		var sum;
		var is_found=false;
		var a_tabs = new Array();
		for(var t=0;t<tags.length;t++){
			ga=tags[t].getAttribute("id");
			ga_=tags[t].getAttribute("name");
			if(ga){
				if(ga.indexOf("tab")==0){	
					sum = ga.substring(3);	
					if(ga_) a_tabs.push(ga_);
				}
			}
		}
		
		var likey=parseInt(sum)+1;
		
		for(var mm=0;mm<a_tabs.length;mm++){
			if(a_tabs[mm]=='tabid'+id)
				is_found=true;
		}
		
		if(!is_found){
		
			var newlinode=document.createElement("li");
			newlinode.setAttribute('id', 'tab'+likey);
			newlinode.setAttribute('name', 'tabid'+id);
			newli=getobject("tabnav").appendChild(newlinode);
			var newanode=document.createElement("a");
			newanode.setAttribute('href', '#');
			newanode.onclick = function() { switchPest("p_Edit_"+likey,"htmleditor_"+(id+1));return false; }
			if (document.all) newanode.setAttribute('className', 'edit');
			else newanode.setAttribute('class', 'edit');
			newanode.innerHTML=title+" <img src='images/loading_small.gif' />";
			newli.appendChild(newanode);
			var newdivnode=document.createElement("div");
			getobject("maintab").appendChild(newdivnode);
			
			// Creación de elemento AJAX
			if (window.XMLHttpRequest)
				ajax = new XMLHttpRequest();
			else if (window.ActiveXObject)
				ajax = new ActiveXObject("Microsoft.XMLHTTP");		
				
			
			if (!ajax) {
				alert("Your browser does not support AJAX. Please use either Internet Explorer 5.5+ or Mozilla Firefox 1.5+");
				return(false);
			}else AjaxStarted = true;
			
			// Configuación y envío de AJAX
			ajax.open("POST", url, true);
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.send("id="+id+"&likey="+likey);
			// Comprobación de resultado
			ajax.onreadystatechange = function() {
				if (ajax.readyState == 4) {			
					newdivnode.innerHTML=ajax.responseText;
					newanode.innerHTML=title;
					AjaxStarted = false;
				}
			}
			
		}else{
		
			alert("You have already open a tab to edit this item");
		
		}
		
	}
	
}

function putCal(div,value,name){
	if(getobject(div)){
		getobject(div).innerHTML="<p>Discount expiration:</p><script>newCalendar('"+name+"','"+value+"')</script>";
		alert("<p>Discount expiration:</p><script>newCalendar('"+name+"','"+value+"')</script>");
	}
}

////////////////////////////////////////////////////////////////////////////////////////
// muestra ventana de recuperación e contraseña
////////////////////////////////////////////////////////////////////////////////////////
function showRecPassword(){
	show_hide_obj("alphadiv",1);
	shiftOpacity("recpass",500,1);
}

function closeRecPassword(){
	show_hide_obj("alphadiv",0);
	shiftOpacity("recpass",500);
}

////////////////////////////////////////////////////////////////////////////////////////
// cierra ventana de mensaje emergente
////////////////////////////////////////////////////////////////////////////////////////

function closeMsg(red){
	document.getElementById('alphadiv').style.display='none';
	document.getElementById('msg_cont_master').style.display='none';
	document.getElementById('msg_cont').style.display='none';
	document.getElementById('msg_cont2').style.display='none';
	document.getElementById('msg').style.display='none';	
	if(red) location.replace(red);
}

////////////////////////////////////////////////////////////////////////////////////////
// 
////////////////////////////////////////////////////////////////////////////////////////

function del(url) {
	if (confirm("Realmente desea eliminar este item?")) 
		location.replace(url);
}

////////////////////////////////////////////////////////////////////////////////////////
// script necesario para ejecución cdl menu css, soluciona certos problemas
////////////////////////////////////////////////////////////////////////////////////////

function swap(){this.className="msieFix"}
function swapBack(){this.className="trigger"}
function swapfocus() {this.parentNode.parentNode.parentNode.className="msieFix"}
function swapblur() {this.parentNode.parentNode.parentNode.className="trigger"}
function tmenu(){
	if (document.getElementById){	
	var LI = document.getElementsByTagName("li");
	var zLI= LI.length;
		for(var k=0;k<zLI;k++){
			if(LI[k].id){
//			LI[k].firstChild.href="#";
			LI[k].className="trigger";
			}
			if(LI[k].parentNode.parentNode.className=="trigger"){LI[k].firstChild.onfocus=swapfocus;LI[k].firstChild.onblur = swapblur}
			if(LI[k].className=="trigger"){LI[k].onmouseover=swap;LI[k].onmouseout=swapBack}
		}
	}
}
window.onload=function(){tmenu();}





 function lalala() {
  tags=document.getElementByTagName("IFRAME");
  for(i=0;i<tags.lenght;i++){
	  if(tags[i].indexOf("ieditor")!=-1){
		  
	  }
  }
  var f=window.frames["ieditor"];
  alert(f.document.getElementById("f_text").value);
 }
 
function showLoading(){
	document.getElementById("navlinks_right").style.padding="0px";
	document.getElementById("navlinks_right").style.textAlign="right";

var cont = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="546" height="23"><param name="movie" value="flash/loading.swf" /><param name="quality" value="high" /><embed src="flash/loading.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="546" height="23"></embed></object>';
	
	document.getElementById("navlinks_right").innerHTML=cont;
	
}

function vEnter2(){
	if(document.getElementById("user").value==""){
		alert("Please enter you user name");
	}else if(document.getElementById("pwd").value==""){
		alert("Please enter you password");
	}else{
		document.getElementById("index").submit();
	}
}

function fileProcess(){
	document.getElementById('fileloading').style.display='';
	document.getElementById('div_fileprocess').innerHTML='<iframe id="fileprocess" name="fileprocess" style="width:0px;height:0px;border:0px;" onload="fileProcessReady();"></iframe>';
}

function fileProcessReady(){
	window.parent.document.getElementById('fileloading').style.display='none';
	window.frames["filedisplay"].location.reload(true);
}

function fileRemove(url,pars){
	
	window.parent.document.getElementById('fileloading').style.display='';
	
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	else if (window.ActiveXObject)
		ajax = new ActiveXObject("Microsoft.XMLHTTP");		
		
	
	if (!ajax) {
		alert("Your browser does not support AJAX. Please use either Internet Explorer 5.5+ or Mozilla Firefox 1.5+");
		return(false);
	}
	
	// Configuación y envío de AJAX
	ajax.open("POST", url, true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(pars);
	// Comprobación de resultado
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4) {	
			window.parent.document.getElementById('fileloading').style.display='none';
			if(ajax.responseText==0){
				alert("Error!");
			}else{
				window.parent.frames["filedisplay"].location.reload(true);
			}
		}
	}
	
}

function addSize(id){

	var title=document.getElementById("f_size_title").value;
	var price=document.getElementById("f_size_price").value;
	var stock=document.getElementById("f_size_stock").value;
	
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	else if (window.ActiveXObject)
		ajax = new ActiveXObject("Microsoft.XMLHTTP");		
		
	
	if (!ajax) {
		alert("Your browser does not support AJAX. Please use either Internet Explorer 5.5+ or Mozilla Firefox 1.5+");
		return(false);
	}
	
	// Configuación y envío de AJAX
	ajax.open("POST", "sizes.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("add=1&id="+id+"&title="+title+"&price="+price+"&stock="+stock);
	// Comprobación de resultado
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4){
			if(ajax.responseText==0){
				alert("Error!");
			}else{
				document.getElementById("sizeslist").innerHTML=ajax.responseText;
			}
		}
	}

}

function delSize(sid,id){
	
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	else if (window.ActiveXObject)
		ajax = new ActiveXObject("Microsoft.XMLHTTP");		
		
	
	if (!ajax) {
		alert("Your browser does not support AJAX. Please use either Internet Explorer 5.5+ or Mozilla Firefox 1.5+");
		return(false);
	}
	
	// Configuación y envío de AJAX
	ajax.open("POST", "sizes.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("del=1&sizeid="+sid+"&id="+id);
	// Comprobación de resultado
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4){	
			if(ajax.responseText==0){
				alert("Error!");
			}else{
				document.getElementById("sizeslist").innerHTML=ajax.responseText;
			}
		}
	}

}

function managePredFollowing(selObj){
	var txtarea=document.getElementById("f_orderfollow_txt");
	if(txtarea){ 
		txtarea.value="";
		txtarea.value = selObj.options[selObj.selectedIndex].value;
	}
}

function setFollowText(form,id){
	
	text = form.f_orderfollow_txt.value;
	
	if(text==""){
		alert("Please enter a description of the order status.");
		return false;
	}
	
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	else if (window.ActiveXObject)
		ajax = new ActiveXObject("Microsoft.XMLHTTP");		
		
	
	if (!ajax) {
		alert("Your browser does not support AJAX. Please use either Internet Explorer 5.5+ or Mozilla Firefox 1.5+");
		return(false);
	}
	
	// Configuación y envío de AJAX
	ajax.open("POST", "editorder.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+id+"&status="+text);
	// Comprobación de resultado
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4){
			if(ajax.responseText==1){ 
				alert("Order following status has been updated");
				document.getElementById("followingSet").style.display="none";
			}else alert("Error");
		}
	}
	
	return false;
	
}

function sendConfirmEmail(form,id){
	
	if (window.XMLHttpRequest)
		ajax = new XMLHttpRequest();
	else if (window.ActiveXObject)
		ajax = new ActiveXObject("Microsoft.XMLHTTP");		
		
	
	if (!ajax) {
		alert("Your browser does not support AJAX. Please use either Internet Explorer 5.5+ or Mozilla Firefox 1.5+");
		return(false);
	}
	
	// Configuación y envío de AJAX
	ajax.open("POST", "editorder.php", true);
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("id="+id+"&email=1");
	// Comprobación de resultado
	ajax.onreadystatechange = function() {
		if (ajax.readyState == 4){
			if(ajax.responseText==1){ 
				alert("Email sent succesfully!");
			}else alert("Error");
		}
	}
	
	return false;
	
}

function showDisplayArea(area){
	
	if(document.getElementById(area).className=="")
		document.getElementById(area).className="on";	
	else document.getElementById(area).className="";
	
}

function showSubs_(div,show,left,top){
	if(showSubs_.arguments.length>4){
		var currpos=findPos(showSubs_.arguments[4]);
		getobject(div).style.marginLeft=(currpos[0]+left)+"px";
		getobject(div).style.marginTop=(currpos[1]+top)+"px";
		getobject(div).style.display="";
		getobject(div+"_shadow").style.marginLeft=(currpos[0]+left+5)+"px";
		getobject(div+"_shadow").style.marginTop=(currpos[1]+top+5)+"px";
		getobject(div+"_shadow").style.display="";
	}
	if(show==1){
 		getobject(div).style.display="";
 		getobject(div+"_shadow").style.display="";
	}else{
		getobject(div).style.display="none";
		getobject(div+"_shadow").style.display="none";
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getInput(obj){
	if(obj.options[obj.selectedIndex].value=="video"){
		document.getElementById("custom_filename").style.display="";
	}else document.getElementById("custom_filename").style.display="none";
}
