/* Validar campos del formulario de alta */

/* Obtenemos información del tipo de sitio */
function ___SiteBuilder_GetObjInfo(objType, objId) {
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.addPostParameter("___MSB_OBJECT_TYPE", objType);
	oAjax.addPostParameter("___MSB_OBJECT_ID", objId);
	oAjax.throwEvent2("sitebuilder_get_info", true, "XML_DOCUMENT.wbe");
	return oAjax.responseText;
}

/* Comprobación de que es un dominio correcto */
function ___DirectoryCmd_CheckLogin(objectHTML) {
	var sHtml;
	if (objectHTML.value=='') return;
	sHtml = ___SiteBuilder_GetObjInfo('check_login', objectHTML.value);
	if (sHtml>0) {
		alert('El usuario ya existe, prueba con otro nombre de usuario.');
		objectHTML.select();
		return false;
	}
}

function ___DirectoryCmd_CheckLogin2(objectHTML) {
	var sHtml;
	sHtml = ___SiteBuilder_GetObjInfo('check_login', objectHTML.value);
	if (sHtml>0) {
		alert('El usuario ya existe, prueba con otro nombre de usuario.');
		objectHTML.select();
		return false;
	} else {
		return true;
	}
}

function ___DirectoryCmd_CheckFields(pass1,pass2) {		
	var oFormValidator = new WBEFormValidator();
	oFormValidator.LOGIN_MIN_LONG = 5;
	oFormValidator.PASSWORD_MIN_LONG = 5;
	return (___DirectoryCmd_CheckSpecificFields(pass1,pass2)) && 
				(oFormValidator.validateForm(document.forms[0]));
}

/* Validar campos de formulario */
function ___DirectoryCmd_CheckContactForm() {
	var oFormValidator = new WBEFormValidator();
	return  (oFormValidator.validateForm(document.forms[0]));
}


function ___DirectoryCmd_CheckSpecificFields(password1,password2) {
	//Check doble password
	var pass1 = document.getElementById(password1);;
	var pass2 = document.getElementById(password2);
	if ((pass1!=null) && (pass2!=null)) {
		if ((pass1.value.length==0) && (pass2.value.length==0)) return true;
		if (pass2.value!=pass1.value) {
			alert(PASS_EQUAL);
			return false;
		}	
	}
	return true;
}

function ___DirectoryCmd_Search(sUrl, iPosID) 
{	
	var oForm = document.forms[0];
	var oAjax = new WBE_AjaxClass();
	var oFormValidator = new WBEFormValidator();
	oAjax.clear();

	if (___DirectoryCmd_IsTextEmpty(oForm) && ___DirectoryCmd_IsSectorEmpty(oForm) && 
			___DirectoryCmd_IsProvinceEmpty(oForm) && ___DirectoryCmd_IsRegionEmpty(oForm) && 
			___DirectoryCmd_IsAddressEmpty(oForm) && ___DirectoryCmd_IsPostalCodeEmpty(oForm) &&
			___DirectoryCmd_IsCityEmpty(oForm) && ___DirectoryCmd_IsEnterpriseEmpty(oForm)) {
		alert('Debes especificar algún criterio de búsqueda.');
		return;
	}	

	if (!oFormValidator.validateForm(oForm, iPosID)) return;
	
	if (oForm._dir_s_string) 
		{oAjax.addPostParameter("search_string", oForm._dir_s_string.value);}
	if (oForm._dir_sel_sector) 
		{oAjax.addPostParameter("sector_id", oForm._dir_sel_sector[oForm._dir_sel_sector.selectedIndex].value);}
	if (oForm._dir_s_product) 	
		oAjax.addPostParameter("product_string", oForm._dir_s_product.value);
	if (oForm._dir_sel_province) 
		{oAjax.addPostParameter("province_id", oForm._dir_sel_province[oForm._dir_sel_province.selectedIndex].value);}
	if (oForm._dir_sel_region) 
		{oAjax.addPostParameter("region_id", oForm._dir_sel_region[oForm._dir_sel_region.selectedIndex].value);}
	if (oForm._dir_sel_city) 
		{oAjax.addPostParameter("city_id", oForm._dir_sel_city[oForm._dir_sel_city.selectedIndex].value);}
	if (oForm._dir_sel_city_code) 
		{oAjax.addPostParameter("city_code", oForm._dir_sel_city_code[oForm._dir_sel_city_code.selectedIndex].value);}
	if (oForm._dir_s_city_name) 
		{oAjax.addPostParameter("city_name", oForm._dir_s_city_name.value);}
	if (oForm._sel_type) 
		{oAjax.addPostParameter("user_type", oForm._sel_type.value);}
	if (oForm._dir_s_enterprise) 
		{oAjax.addPostParameter("enterprise_string", oForm._dir_s_enterprise.value);}
	if (oForm._dir_s_address)
		{oAjax.addPostParameter("address_string", oForm._dir_s_address.value);}
	if (oForm._dir_s_cp)
		{oAjax.addPostParameter("postal_code_string", oForm._dir_s_cp.value);}

	oAjax.throwEvent2("directory_search_enterprise");
	window.location.href = sUrl;	
}

/***********************************************/
// Buscador directorio empresas tiendas de alcoy
function ___DirectoryCmd_Search_Tda(sUrl) 
{		
	var oForm = document.forms[0];
	var oAjax = new WBE_AjaxClass();
	var oFormValidator = new WBEFormValidator();
	oAjax.clear();

	/*
	if (___DirectoryCmd_IsTextEmpty(oForm) && ___DirectoryCmd_IsSectorEmpty(oForm)) {
		alert('Debes especificar algún criterio de búsqueda.');
		return;
	}
	*/	

	//if (!oFormValidator.validateForm(oForm)) return;
	
	if (oForm._dir_s_string) 
		{oAjax.addPostParameter("search_string", oForm._dir_s_string.value);}
	if (oForm._dir_sel_sector) 
		{oAjax.addPostParameter("sector_id", oForm._dir_sel_sector[oForm._dir_sel_sector.selectedIndex].value);}		
	/*
	if (oForm._dir_sel_province) 
		{oAjax.addPostParameter("province_id", oForm._dir_sel_province[oForm._dir_sel_province.selectedIndex].value);}
	if (oForm._dir_sel_region) 
		{oAjax.addPostParameter("region_id", oForm._dir_sel_region[oForm._dir_sel_region.selectedIndex].value);}
	if (oForm._dir_sel_city) 
		{oAjax.addPostParameter("city_id", oForm._dir_sel_city[oForm._dir_sel_city.selectedIndex].value);}
	*/			

	oAjax.throwEvent2("directory_search_enterprise");
	window.location.href = sUrl;	
}
/***********************************************/

function ___DirectoryCmd_IsTextEmpty(oForm) {
	if (oForm._dir_s_string) {return (oForm._dir_s_string.value.length == 0);}
	else {return false;}
}

function ___DirectoryCmd_IsEnterpriseEmpty(oForm) {
	if (oForm._dir_s_enterprise) {return (oForm._dir_s_enterprise.value.length == 0);}
	else {return false;}
}

function ___DirectoryCmd_IsAddressEmpty(oForm) {
	if (oForm._dir_s_address) {return (oForm._dir_s_address.value.length == 0);}
	else {return false;}
}

function ___DirectoryCmd_IsPostalCodeEmpty(oForm) {
	if (oForm._dir_s_cp) {return (oForm._dir_s_cp.value.length == 0);}
	else {return false;}
}

function ___DirectoryCmd_IsSectorEmpty(oForm) {
	if (oForm._dir_sel_sector) {return (oForm._dir_sel_sector.selectedIndex == 0);}
	else {return false;}
}

function ___DirectoryCmd_IsProvinceEmpty(oForm) {
	if (oForm._dir_sel_province) {return (oForm._dir_sel_province.selectedIndex == 0);}
	else {return false;}
}

function ___DirectoryCmd_IsRegionEmpty(oForm) {
	if (oForm._dir_sel_region) {return (oForm._dir_sel_region.selectedIndex == 0);}
	else {return false;}
}

function ___DirectoryCmd_IsCityEmpty(oForm) {
	if (oForm._dir_sel_city) {return (oForm._dir_sel_city.selectedIndex == 0);}
	else {return false;}
}

function ___DirectoryCmd_Login(sUrl1, sUrl2) {
	var oForm = document.forms[0];
	var oAjax = new WBE_AjaxClass();
	var oXmlDoc;
	oAjax.clear();
	if (oForm._dir_login.value=='' || oForm._dir_password.value=='') {
		alert('Debes escribir un usuario y contraseña.');
		return;
	}
	
	oAjax.addPostParameter("_login", escape(oForm._dir_login.value));
	oAjax.addPostParameter("_password", escape(oForm._dir_password.value));
	oXmlDoc = oAjax.throwEventXML("directory_login_enterprise");

	if (!oXmlDoc) {	alert('Error al intentar entrar. Inténtelo de nuevo pasados unos instantes.'); return;	}
	var sStatus = oAjax.getXMLNodeValue(oXmlDoc, 's');
	
	if (sStatus!=0) {
		var sErrorMessage = "Nombre de usuario o contraseña incorrectos.";
		oAjax.clear();
		oAjax.addPostParameter("txt_login", escape(oForm._dir_login.value));
		oAjax.addPostParameter("txt_password", escape(oForm._dir_password.value));
		oXmlDoc = oAjax.throwEventXML("user_login");
		if (!oXmlDoc) {	
			alert('Error al intentar entrar. Inténtelo de nuevo pasados unos instantes.');
			return;	
		} else {
			sStatus = oAjax.getXMLNodeValue(oXmlDoc, 's');
			if (sStatus!=0) {
				//sErrorMessage = oAjax.getXMLNodeValue(oXmlDoc, 'm');
				sErrorMessage = "Nombre de usuario o contraseña incorrectos.";
				alert(sErrorMessage);
				return;	
			} else {
				window.location.href = sUrl2;
			}
		}
	} else {
		window.location.href = sUrl1;	
	}
}

function ___DirectoryCmd_RememberPassword() {
	var oForm = document.forms[0];
	var oAjax = new WBE_AjaxClass();
	var oLayer;
	var oXmlDoc;

	oAjax.clear();

	// Oculta las capas de alertas
	oLayer = document.getElementById('pwd_nok');
	oLayer.style.display = "none";
	oLayer = document.getElementById('pwd_ok');
	oLayer.style.display = "none";

	if (oForm._dir_email.value=='') {
		alert('Debes escribir un correo electrónico.');
		return;
	}
	oAjax.addPostParameter("_email", oForm._dir_email.value);
	oXmlDoc = oAjax.throwEventXML("directory_remember_password");
	if (!oXmlDoc) {	alert('Se ha producido un error.'); return;	}
	var sStatus = oAjax.getXMLNodeValue(oXmlDoc, 's');
	if (sStatus!=0) {
		var sErrorMessage = oAjax.getXMLNodeValue(oXmlDoc, 'm');
		oLayer = document.getElementById('pwd_nok');
		oLayer.style.display = "";
		//alert(sErrorMessage);
	} else {
		oLayer = document.getElementById('pwd_ok');
		oLayer.style.display = "";
	}
}

function ___DirectoryCmd_PaintEnterpriseTotal() {
	var oForm = document.forms[0];
	var oAjax = new WBE_AjaxClass();
	var oLayer;
	var oXmlDoc;

	oAjax.clear();

	// Oculta las capas de alertas
	oXmlDoc = oAjax.throwEventXML("directory_get_count");
	if (!oXmlDoc) {	alert('Se ha producido un error.'); return;	}
	var iCount = oAjax.getXMLNodeValue(oXmlDoc, 'count');
	document.write(iCount);
}


/*

function ___DirectoryCmd_ThrowEvent(code, eventCode, parameterName) {
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();	
	oAjax.addPostParameter(parameterName, code);
	oAjax.throwEvent2(eventCode);
	return oAjax.responseText;
}
*/

/* Funcion para recargar combo secundario */
function ___DirectoryCmd_ReloadCombo(sel_box) {
var sValue;
	if (sel_box!=null) {
			sValue = sel_box.options[sel_box.selectedIndex].value;		
	} else {
			sValue = "";
	}	
	document.forms[0].action = window.location.pathname + "?prov_selected=" + sValue;
	document.forms[0].submit();
	
	//window.location.href = window.location.pathname + "?prov_selected=" + sValue;
		
}

/* Funcion para cargar el texto que se le pasa en la capa de tipo div que se la pasa */
function ___DirectoryCmd_SearchSelect(layerName,text,visible) {
	var layer = document.getElementById(layerName);
	if (layer!= null) {
		if (visible) {
			layer.innerHTML = text;
		} else {
			layer.innerHTML = '&#160';
		}	
	} 
}

/* -- RollOver y funciones de mapa del Dream*/ 
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function ImgRestore(elem, src) {
	elem.src = src;
}
/* Fin -- RollOver y funciones de mapa del Dream*/ 


/*
function ___DirectoryCmd_ShowLayer(layerName,layerNameInv) {	
	var layer = document.getElementById(layerName);	
	var layerInv = document.getElementById(layerNameInv);	
	if (layer!=null) {
		if (layer.style.display=='none') {
			layer.style.display = 'block';
		} else {
			layer.style.display = 'none';
		}
	}	
	if (layerInv!=null) {
		if (layerInv.style.display=='none') {
			layerInv.style.display = 'block';
		} else {
			layerInv.style.display = 'none';
		}
	}	
}
*/	

/*
* Oculta o muestra una capa en función de su estado anterior.
*/
function ___DirectoryCmd_ShowHideLayer(show_class, hide_class, layer_id) {
	var oLayer = document.getElementById('ss_' + layer_id);
	var oLayer2 = document.getElementById('ss_table_' + layer_id);
	var sClassName;
	if (oLayer) {
		if (oLayer.className==show_class) {
			oLayer.className = hide_class;
			oLayer2.style.display = "none";
		} else {
			oLayer.className = show_class;
			oLayer2.style.display = "";
		}
	} else {
		alert("La capa " + layer_id + " no existe.");
	}
}

function ___ShowMore(layer_id) {
	var oLayer = document.getElementById('sm1_' + layer_id);
	var oLayer2 = document.getElementById('sm2_' + layer_id);
	oLayer.style.display = "none";
	oLayer2.style.display = "block";
}

/*
* Solicita la validación de los datos del usuario.
*/ 
function ___RequestDataValuation() {
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.throwEvent2("directory_request_data_valuation");
	document.location.reload();
}

/*
* Solicita la validación de los datos del usuario.
*/ 
function ___DirectoryChangeViewStatsImg() {
	var oForm = document.forms[0];
	var oImg = document.getElementById('dir_enterprise_stats_chart');
	oImg.src = oForm.stats_fechas[oForm.stats_fechas.selectedIndex].value;
}

/*
* Solicita creación de tienda online
*/ 
function ___RequestShopOnline() {
	var oAjax = new WBE_AjaxClass();
	oAjax.clear();
	oAjax.throwEvent2("dir_request_shop");
	document.location.reload();
}
