var xmlHttp;
/*JavaScript*/

function GetXmlHttpObject(){ 
	var objXMLHttp=null;
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

/*Functions*/

function loopSelected(rel)
{
	var relPlantaVals = [];
	$('#relPlanta' + rel + ' :selected').each(function(i, selected) {
		relPlantaVals[i] = $(selected).val();
	});
	return relPlantaVals;
}

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}

function implode (glue, pieces) {
    var i = '', retVal='', tGlue='';
    if (arguments.length === 1) {        pieces = glue;
        glue = '';
    }
    if (typeof(pieces) === 'object') {
        if (pieces instanceof Array) {            return pieces.join(glue);
        }
        else {
            for (i in pieces) {
                retVal += tGlue + pieces[i];                tGlue = glue;
            }
            return retVal;
        }
    }    else {
        return pieces;
    }
}

function removeDups(testArray){
	var tempArray = new Array();

	for(var i=0;i<testArray.length;i++)
	{
		var flag = true;
		for(var j=0;j<tempArray.length;j++)
		{
			if(tempArray[j][0]==testArray[i][0])
			{
				flag = false;
			}
		}//for loop
		if(flag==true)
		tempArray.push(testArray[i]);
	}//for loop
	return tempArray;
}

function removeAllRels(){
	 xmlHttp=GetXmlHttpObject();
	 var url = "ajax/rels.php?rem=all";
	 xmlHttp.onreadystatechange=stateChangedRemoveAll;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedRemoveAll(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		if(bringcode){alert("Todas las relaciones fueron eliminadas.");}
		else {alert("Las relaciones no pudieron ser eliminadas.");}
	}
}

function processRel(rel){
	 xmlHttp=GetXmlHttpObject();
	 var value = document.getElementById('relValue' + rel).value;
	 var id = '';
	 id = loopSelected(rel);
	 var ids = '';
	 id = removeDups(id);
	 ids = implode('-', id);
	 var url = "ajax/rels.php?rel=" + rel + "&id=" + ids + "&value=" + value;
	 xmlHttp.onreadystatechange=stateChangedRel;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedRel(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		var params = bringcode.split('*');
		alert("Registro grabado.");
		$("#relPlanta" + params[1]).val([]);
		document.getElementById('relValue' + params[1]).value = '';
		hidePopUpForm(params[1]);
	}
}

function removeRel(tipo, id){
	 xmlHttp=GetXmlHttpObject();
	 var url = "ajax/rels.php?rid=" + id + "&tipo=" + tipo;
	 xmlHttp.onreadystatechange=stateChangedRelRem;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedRelRem(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		var params = bringcode.split('*');
		document.getElementById('relTable_field_' + params[1]).style.display = 'none';		
		alert(params[0]);
	}
}

function processGenForm(){
	 xmlHttp=GetXmlHttpObject();
	 var value = document.getElementById('generoname').value;
	 var categ = document.getElementById('categ').value;	 
	 var url = "admin/genero/genero.php?gen=" + value + "&categ=" + categ;
	 xmlHttp.onreadystatechange=stateChangedGen;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedGen(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		alert(bringcode);
		hidePopUpForm('genero');
		document.getElementById('generoname').value = '';
	}
}

function processCatForm(){
	 xmlHttp=GetXmlHttpObject();
	 var value = document.getElementById('catname').value;
	 var url = "admin/categoria/categoria.php?cat=" + value;
	 xmlHttp.onreadystatechange=stateChangedCat;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedCat(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		alert(bringcode);
		hidePopUpForm('categoria');
		document.getElementById('catname').value = '';
	}
}

function validateGenForm(){
	if(value = document.getElementById('generoname').value == ''){	
		alert('Escriba un nombre.');
		return false;
	} else {return true;}
}
function validateCatForm(){
	if(value = document.getElementById('catname').value == ''){	
		alert('Escriba un nombre.');
		return false;
	} else {return true;}
}

function removeReg(id){
	 xmlHttp=GetXmlHttpObject();
	 var url = "ajax/regs.php?id=" + id;
	 xmlHttp.onreadystatechange=stateChangedReg;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedReg(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		var params = bringcode.split('*');
		document.getElementById('regTable_field_' + params[1]).style.display = 'none';		
		alert(params[0]);
	}
}

function showDetail(itemid){
	 xmlHttp=GetXmlHttpObject();
	 var url = "templates/default/_detail.php?itemid=" + itemid; 
	 xmlHttp.onreadystatechange=stateChangedDetail;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedDetail(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		$("#backBlack").fadeIn("fast");
		$("#detail").fadeIn("fast");
		document.getElementById('innerDetailPopUp').innerHTML = bringcode;
	}
}

function showPopUpForm(rel){
	$("#backBlack").fadeIn("fast");
	$("#" + rel).fadeIn("fast");
}

function hidePopUpForm(rel){
	$("#backBlack").fadeOut("fast");
	$("#" + rel).fadeOut("fast");
}

function swapGen(id){
	 xmlHttp=GetXmlHttpObject();
	 var url = "librerias/ajax/ajax.php?set=swapgen&idcat=" + id; 
	 xmlHttp.onreadystatechange=stateChangedswapGen;
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function stateChangedswapGen(){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var bringcode = xmlHttp.responseText;
		document.getElementById('genDD').innerHTML = bringcode;
	}
}

function validateNewForm(){
	if(document.getElementById('disco').value == ""){
		alert("Si deja vacio el campo disco se usa la palabra definida.");
	}
	if(document.getElementById('titulo').value == ""){
		alert("Tiene que escribir un titulo.");
		return false;
	}
	
	return true;
}

function checkAll(field, total){
	var chk = true;
	if(document.getElementById(field + 'all').checked == true){chk = true;}
	if(document.getElementById(field + 'all').checked == false){chk = false;}
	for(var i = 1; i <= total; i++){
		document.getElementById(field + 'items_' + i).checked = chk;
	}
}



