/* ------------------
   JAVASCRIPT PARA SOCIALNETWORKS
   ------------------
*/

SOCNET=
{
	debug : true,
	
	// CAMBIOS DE PANEL --------------------------------------------------------
	turnTo : function(destino)
	{
		switch(destino)
		{
			case 'panel'	: 	this.showPanel(true); this.showError(false); this.showFinish(false); this.showSending(false); 
								obImg=document.getElementById("socnet_form_verifImg");
								obImg.src=obImg.src+"1";
								obInp=document.getElementById("socnet_form_verificacion");
								obInp.value="";
							  	break;
			case 'error'	: 	this.showPanel(true); this.showError(true); this.showFinish(false); this.showSending(false); break;
			case 'finish'	: 	this.showPanel(false); this.showError(false); this.showFinish(true); this.showSending(false); break;
			case 'sending'	: 	this.showPanel(true); this.showError(false); this.showFinish(false); this.showSending(true); break;
			default			: 	this.showPanel(false); this.showError(false); this.showFinish(false); this.showSending(false); break; // 'nada'
		}
	},
	showInicial : function()
	{
		document.getElementById("socnet_form_de").value="";
		document.getElementById("socnet_form_para").value="";
		document.getElementById("socnet_form_email").value="";
		document.getElementById("socnet_form_mensaje").value="";
		this.turnTo('panel');
	},
	showPanel : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("socnetPanel");
		if(estado) 
		{
			ob.style.display="block";
		}
		else ob.style.display="none";
	},
	showError : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("socnetError");
		if(estado) ob.style.display="block";
		else ob.style.display="none";
	},
	showFinish : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("socnetFinish");
		if(estado) ob.style.display="block";
		else ob.style.display="none";
	},
	showSending : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("socnetSending");
		if(estado) ob.style.display="block";
		else ob.style.display="none";
	},

	// CHEQUEO Y ENVIO DE FORM -------------------------------------------------	
	checkForm : function()
	{
		ok=true;
		causa="";
		
		// TODO: aca van los chequeos
		obDe=document.getElementById("socnet_form_de");
		obPara=document.getElementById("socnet_form_para");
		obEmail=document.getElementById("socnet_form_email");
		obMensaje=document.getElementById("socnet_form_mensaje");
		if(obMensaje.value.lenght>1000) obMensaje.value=obMensaje.value.substr(0,1000);
		
		errDe=document.getElementById("errorDe");
		errPara=document.getElementById("errorPara");
		errEmail=document.getElementById("errorEmail");
		errGen=document.getElementById("socnetErrorMessage");
		errGen.style.display="none";
		errDe.style.visibility="hidden";
		
		if(obDe.value.length<2)
		{ 
			ok=false; 
			errDe.style.display="block";
			errDe.style.visibility="visible";
		}
		else 
		{
			errDe.style.display="none";
			errDe.style.visibility="hidden";
		}
		
		if(obPara.value.length<2) 
		{ 
			ok=false; 
			errPara.style.display="block";
			errPara.style.visibility="visible";
		}
		else 
		{
			errPara.style.display="none";
			errPara.style.visibility="hidden";
		}
		
		if(obEmail.value.length<2 || !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obEmail.value)) 
		{ 
			ok=false; 
			errEmail.style.display="block";
			errEmail.style.visibility="visible";
		}
		else 
		{
			errEmail.style.display="none";
			errEmail.style.visibility="hidden";
		}
		
		if(ok)	this.sendForm();
		else	this.turnTo('error'); // prendo el error
	},
	sendForm : function()
	{
		// agrego el url
		ur=document.getElementById('inputUrl');
		socnetur=document.getElementById('socnet_url');
		if(ur!=null && socnetur!=null) socnetur.value=ur.value;
		
		this.turnTo('sending');
		this.objForm=document
		//FWK.debug=true; // for debugging 
		FWK.postApiCallback("SOCNET.resultForm","socialnetworks:mixplay","saveForm","socnetForm","")
		// TODO: aca va el envio por ajax!!
	},
	resultForm : function(req)
	{
		if(req.responseText=="OK") //todo vino ok!
		{
			this.turnTo('finish');
		}
		else
		{
			document.getElementById("errorDe").style.display="none";
			document.getElementById("errorDe").style.visibility="hidden";
			document.getElementById("errorPara").style.display="none";
			document.getElementById("errorPara").style.visibility="hidden";
			document.getElementById("errorEmail").style.display="none";
			document.getElementById("errorEmail").style.visibility="hidden";
			document.getElementById("socnetErrorMessage").style.display="block";
			document.getElementById("socnetErrorMessage").style.visibility="visible";
			document.getElementById('socnetErrorMessage').innerHTML=req.responseText; // seteo el mensaje
			this.turnTo('error'); // prendo el error
		}
	}
}

AMIGO=
{
	debug : true,
	
	// CAMBIOS DE PANEL --------------------------------------------------------
	turnTo : function(destino)
	{
		switch(destino)
		{
			case 'panel'	: 	this.showPanel(true); this.showError(false); this.showFinish(false); this.showSending(false); 
								obImg=document.getElementById("amigo_form_verifImg");
								obImg.src=obImg.src+"1";
								obInp=document.getElementById("amigo_form_verificacion");
								obInp.value="";
							  	break;
			case 'error'	: 	this.showPanel(true); this.showError(true); this.showFinish(false); this.showSending(false); break;
			case 'finish'	: 	this.showPanel(false); this.showError(false); this.showFinish(true); this.showSending(false); break;
			case 'sending'	: 	this.showPanel(true); this.showError(false); this.showFinish(false); this.showSending(true); break;
			default			: 	this.showPanel(false); this.showError(false); this.showFinish(false); this.showSending(false); break; // 'nada'
		}
	},
	showInicial : function()
	{
		document.getElementById("amigo_form_de").value="";
		document.getElementById("amigo_form_para").value="";
		document.getElementById("amigo_form_email").value="";
		document.getElementById("amigo_form_mensaje").value="";
		this.turnTo('panel');
	},
	showPanel : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("amigoPanel");
		if(estado) ob.style.display="block";
		else ob.style.display="none";
	},
	showError : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("amigoError");
		if(estado) ob.style.display="block";
		else ob.style.display="none";
	},
	showFinish : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("amigoFinish");
		if(estado) ob.style.display="block";
		else ob.style.display="none";
	},
	showSending : function(estado)
	{
		if(null==estado) estado=false;
		ob=document.getElementById("amigoSending");
		if(estado) ob.style.display="block";
		else ob.style.display="none";
	},

	// CHEQUEO Y ENVIO DE FORM -------------------------------------------------	
	checkForm : function()
	{
		ok=true;
		causa="";
		
		// TODO: aca van los chequeos
		obDe=document.getElementById("amigo_form_de");
		obPara=document.getElementById("amigo_form_para");
		obEmail=document.getElementById("amigo_form_email");
		obMensaje=document.getElementById("amigo_form_mensaje");
		if(obMensaje.value.lenght>1000) obMensaje.value=obMensaje.value.substr(0,1000);
		
		errDe=document.getElementById("errorDe");
		errPara=document.getElementById("errorPara");
		errEmail=document.getElementById("errorEmail");
		errGen=document.getElementById("amigoErrorMessage");
		errGen.style.display="none";
		errDe.style.visibility="hidden";
		
		if(obDe.value.length<2)
		{ 
			ok=false; 
			errDe.style.display="block";
			errDe.style.visibility="visible";
		}
		else 
		{
			errDe.style.display="none";
			errDe.style.visibility="hidden";
		}
		
		if(obPara.value.length<2) 
		{ 
			ok=false; 
			errPara.style.display="block";
			errPara.style.visibility="visible";
		}
		else 
		{
			errPara.style.display="none";
			errPara.style.visibility="hidden";
		}
		
		if(obEmail.value.length<2 || !/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(obEmail.value)) 
		{ 
			ok=false; 
			errEmail.style.display="block";
			errEmail.style.visibility="visible";
		}
		else 
		{
			errEmail.style.display="none";
			errEmail.style.visibility="hidden";
		}
		
		if(ok)	this.sendForm();
		else	this.turnTo('error'); // prendo el error
	},
	sendForm : function()
	{
		// agrego el url
		ur=document.getElementById('inputUrl');
		amigour=document.getElementById('amigo_url');
		if(ur!=null && amigour!=null) amigour.value=ur.value;
		
		// agrego el nodo
		c1=document.getElementById("combo1");
		c2=document.getElementById("combo2");
		c3=document.getElementById("combo3");
		if(c3.value!="") cf=c3;
		else
		{
			if(c2.value!="") cf=c2;
			else cf=c1;
		}
		document.getElementById("amigo_nodo").value=cf.value;
		
		this.turnTo('sending');
		this.objForm=document
		//FWK.debug=true; // for debugging 
		FWK.postApiCallback("AMIGO.resultForm","socialnetworks:mixplay","saveFormAmigo","amigoForm","")
		// TODO: aca va el envio por ajax!!
	},
	resultForm : function(req)
	{
		if(req.responseText=="OK") //todo vino ok!
		{
			this.turnTo('finish');
		}
		else
		{
			document.getElementById("errorDe").style.display="none";
			document.getElementById("errorDe").style.visibility="hidden";
			document.getElementById("errorPara").style.display="none";
			document.getElementById("errorPara").style.visibility="hidden";
			document.getElementById("errorEmail").style.display="none";
			document.getElementById("errorEmail").style.visibility="hidden";
			document.getElementById("amigoErrorMessage").style.display="block";
			document.getElementById("amigoErrorMessage").style.visibility="visible";
			document.getElementById('amigoErrorMessage').innerHTML=req.responseText; // seteo el mensaje
			this.turnTo('error'); // prendo el error
		}
	}
}


/****************************************************************************************************
 FUNCIONES DE GADGET
****************************************************************************************************/
function gadget_ProcesarPrincipal(){
	//campos ocultos
	var var_idmenu	= document.getElementById("var_idmenu");
	var var_codigo	= document.getElementById("var_codigo");
	var var_skin	= document.getElementById("var_skin");
	
	//combos
	var combo1 = document.getElementById("combo1");
	var combo2 = document.getElementById("combo2");
	var combo3 = document.getElementById("combo3");
	
	
	//value contiene: "ID_ITEM,CODIGO,ID_MENU,SKIN"
	//ejemplo: "623,moda,202,sk_mixplay"
	arrValue	= combo1.value.split(",");
	
	id_item		= arrValue[0];
	codigo		= arrValue[1];
	id_menu		= arrValue[2];
	skin		= arrValue[3];

	//cargar variable global
	var_codigo.value	= codigo;
	var_idmenu.value	= id_menu;
	var_skin.value		= skin;
	
	//alert("hidden codigo: "+var_codigo.value);
	//alert("id_item:"+id_item+"codigo:"+codigo+"id_menu:"+id_menu+"skin:"+skin);
		
	//remover valores de combo2
	gadget_RemoverTodo(combo2);
	combo2.disabled=true;
	
	//remover valores de combo3
	gadget_RemoverTodo(combo3);
	combo3.disabled=true;
	
	
	//ejecutar FWK
	request		= FWK.loadApi("socialnetworks|playlist|playlist|player", "procesarcombos", "&id_item="+id_item+"&id_menu="+id_menu+"&rand="+Math.random());
	arrLineas	= request.split("\n");
	
	for(i=0; i<arrLineas.length; i++){
		arrLineas[i] = gadget_Trim(arrLineas[i]);
		
		if(arrLineas[i]!=""){
			if(combo2.disabled==true){
				combo2.disabled=false;
				gadget_AgregarItem(combo2, "", "Selecciona");
			}
			
			//línea: "623,moda,sk_mixplay,Moda"			
			arrDato = arrLineas[i].split(",");
			gadget_AgregarItem(combo2, arrDato[0]+","+arrDato[1]+","+arrDato[2], arrDato[3]);
		}
	}
		
	return false;
	
}

function gadget_ProcesarCombos(combo){
	
	//campos ocultos
	var var_idmenu	= document.getElementById("var_idmenu");
	var var_codigo	= document.getElementById("var_codigo");
	var var_skin	= document.getElementById("var_skin");
	
	//combos
	var combo1 = document.getElementById("combo1");
	var combo2 = document.getElementById("combo2");
	var combo3 = document.getElementById("combo3");
	
	switch(combo){
		case 2:
			if(!combo2.value) return;

			// El VALUE del COMBO contiene: "ID_ITEM,CODIGO,SKIN"
			// ej:"623,moda,sk_mixplay"
			arrTMP		= combo2.value.split(",");
			id_item		= arrTMP[0];
			codigo		= arrTMP[1];
			sking		= arrTMP[2];
			
			//seteando variables globales
			var_skin.value		= sking;
			var_codigo.value	= codigo;
			
			//alert("hidden codigo: "+var_codigo.value);
			//alert("id_item:"+id_item+"codigo:"+codigo+"id_menu[global]:"+var_idmenu.value+"skin:"+skin);
			
			//vaciar, desabilitar combo3
			gadget_RemoverTodo(combo3);
			combo3.disabled=true;
			
			//ejecutar FWK
			request		= FWK.loadApi("socialnetworks|playlist|playlist|player", "procesarcombos", "&id_item="+id_item+"&id_menu="+var_idmenu.value+"&rand="+Math.random());
			//alert(request);
			arrLineas	= request.split("\n");
			
			for(i=0; i<arrLineas.length; i++){
				arrLineas[i] = gadget_Trim(arrLineas[i]);
				
				if(arrLineas[i]!=""){
					if(combo3.disabled==true){
						combo3.disabled=false;
						gadget_AgregarItem(combo3, "", "Selecciona");
					}
					
					//arrLineas: "623,moda,sk_mixplay,Moda"
					arrDato = arrLineas[i].split(",");
					gadget_AgregarItem(combo3, arrDato[0]+","+arrDato[1]+","+arrDato[2], arrDato[3]);
				}
			}
			
			break;



		case 3:
			if(!combo3.value) return;
			
			// El VALUE del COMBO contiene: "ID_ITEM,CODIGO,SKIN"
			// ej:"623,moda,sk_mixplay"
			arrTMP	= combo3.value.split(",");
			id_item	= arrTMP[0];
			codigo	= arrTMP[1];
			sking	= arrTMP[2];
			
			var_codigo.value = codigo;
			//alert("codigo:"+codigo);			
			break;
	}
	
	return true;
}

function gadget_AgregarItem(combo,value,text) {
	combo.options[combo.length]=new Option(text,value);
}

function gadget_RemoverTodo(combo) {
     if(combo.length <= 0) return;
     var N = combo.length;
     for(var i=0; i<N; i++)	combo.options[0]=null;     
}
function gadget_Trim(cadena){
	return cadena.replace(/^\s+|\s+$/, ''); 
}

function gadget_Enviar(){
		
	//campos ocultos
	var var_idmenu	= document.getElementById("var_idmenu");
	var var_codigo	= document.getElementById("var_codigo");
	var var_skin	= document.getElementById("var_skin");
	
	f	= document.forms["form1"];
	url	= "";
	for(i=0;i<f.elements.length;i++){
		if(f.elements[i].type=="radio" && f.elements[i].checked==1){
			url = f.elements[i].tag;
			red = f.elements[i].params;
			break;
		}
	}
	
	//error, no se seleccionó una red social
	if(url==""){alert("Error no esperado.\nSeleccione una red social.");return;}
	
	//armar url completa
	url += "/nodo/"+var_codigo.value+"/"+red;
	
	//concatenando "template", es opcional
	if(var_skin.value) url += "/template/"+var_skin.value;
	
	
	//abrir nueva ventana
	window.open(url,"_blank","");
	
	//ocultar formulario, mostrar mensaje
	document.getElementById('formGadget').style.visibility	= "hidden";
	document.getElementById('msgGadget').style.display		= "block";
	
}
