var ListaPartes = new Array();

/***************** AJAX ******************/
var asynchronous = new Asynchronous();
asynchronous.complete = function(responseText, id) {
  document.getElementById(id).innerHTML = responseText;
}

function loadContent(url, id)
{
	var responseItem = document.getElementById(id)
    responseItem.innerHTML = '<img src="http://www.exposigns.com.mx/imagenes/loading.gif" align="absmiddle"/> Cargando, por favor espere...';
    asynchronous.call(url, id)
}

function AjaxGetMicas(valor,categoriaID,anchoID)
{
	loadContent('ajaxGetMicas.php?valor=' + valor + '&ID=' + categoriaID + '&ancho=' + anchoID, 'divMicas');
}

function CalculaTotal(categoriaID)
{
	var arrComponentes = Array();
	var j = 0;
	obj = document.getElementById('body');
	for (i = 0; ele = obj.getElementsByTagName('input')[i]; i++)
	{
		if (ele.value != '') { arrComponentes[j] = [ele.name,ele.value]; j++; }
	}
	for (i = 0; ele = obj.getElementsByTagName('select')[i]; i++)
	{
		if (ele.value != '') { arrComponentes[j] = [ele.name,ele.value]; j++; }
	}
	loadContent('ajaxGetTotal.php?categoriaID=' + categoriaID + '&componentes=' + arrComponentes, 'precioTotal');
}

function borraOption(selectId)
{
	document.getElementById(selectId).options.length = 0;
}

function addOption(selectId, txt, val)
{
    var objOption = new Option(txt, val);
	document.getElementById(selectId).options.add(objOption);
}

function BuscaComponentes(valor,ref,parteID)
{
	var url = 'ajaxGetComponentes.php?search=' + valor + '&ref=' + ref;
	if (parteID != '') { url += '&parteID=' + parteID; }
	loadContent(url ,'divListaComponentes' + ref);
}

function AddComponente(id,ref,parteID)
{
	var url = 'ajaxGetComponentesParte.php?ID=' + id + '&ref=' + ref;
	if (parteID != '') { url += '&parteID=' + parteID; }
	loadContent(url,'divComponentesParte' + ref);
}

function QuitaComponente(key,ref,parteID)
{
	var url = 'ajaxGetComponentesParte.php?KEY=' + key + '&ref=' + ref;
	if (parteID != '') { url += '&parteID=' + parteID; }
	loadContent(url,'divComponentesParte' + ref);
}

/***************** AJAX ******************/

function Show(divID)
{
	if (window.document.getElementById('distNacionales') != null) { window.document.getElementById('distNacionales').style.display = 'none'; }
	if (window.document.getElementById('distExtranjeros') != null) { window.document.getElementById('distExtranjeros').style.display = 'none'; }
	window.document.getElementById(divID).style.display = 'block';
}

function Distribuidores(estado,pais)
{
	if (pais == 'Mexico') { Ubicacion = estado; tipo = 'Nac'; }
	else { Ubicacion = pais; tipo = 'Ext'; }
	var url = 'http://www.exposigns.com.mx/distribuidores.php?loc=' + Ubicacion + '&tipo=' + tipo;
	showPopWin(url, 800, 400, null);
}

function Borrar(mod,id)
{
	Sexy.confirm("<h3>Confirmacion del Sistema</h3><p>¿Estas seguro de querer Eliminar este registro?</p>", { onComplete: 
		function(returnvalue)
		{
			if(returnvalue)
            {
				window.location = "managedata.php?mod=" + mod + "&act=Borrar&ID=" + id;
			}
        }
	});
}

function Convierte(id)
{
	Sexy.confirm("<h3>Confirmacion del Sistema</h3><p>¿Estas seguro de querer Convertir este contacto en Distribuidor?</p>", { onComplete: 
		function(returnvalue)
		{
			if(returnvalue)
            {
				window.location = "managedata.php?mod=Solicitud&act=Convierte&ID=" + id;
			}
        }
	});
}

function Sube()
{
	window.document.getElementById('cargando').style.display = "block";
}

function RecuperaPass()
{
	document.getElementById('formLogin').style.display = 'none';
	document.getElementById('recuperaPass').style.display = 'block';
}

function CancelaRPass()
{
	document.getElementById('formLogin').style.display = 'block';
	document.getElementById('recuperaPass').style.display = 'none';
}

function Confirma(id)
{
	if (confirm("¿Estas seguro de querer eliminar este Producto de tu Carrito?"))
	{
		window.location = "destroyCarro.php?id=" + id;
	}
}

function AddDato(mod)
{
	showPopWin('ajaxDatos.php?mod=' + mod, 600, 300, null);
}

function EditDato(mod,id)
{
	showPopWin('ajaxDatos.php?mod=' + mod + '&ID=' + id, 600, 300, null);
}

function DeleteDato(mod,id)
{
	if (confirm('Estas seguro de querer Eliminar este Registro?'))
	{
		window.location = "send.php?mod=" + mod + "&acc=Borrar&ID=" + id;
	}
}

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 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_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 SetData(divID,bgColor)
{
	document.getElementById(divID).style.background = bgColor;
}

function TargetOpener(liga)
{
	if (! (window.focus && window.opener))return true;
	window.opener.focus();
	window.opener.location.href=liga.href;
	window.close();
	return false;
}

function Recarga()
{
	window.location.reload();
}

function Chat()
{
	window.open('http://www.expomex.com/chat/choosedepartment.php','_blank','height=550,width=700,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,left=150,top=50');
}

function Detalle(id)
{
	window.open("detallePedido.php?id=" + id,"","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=800,height=600,screenX=50,screenY=50,top=50,left=50");
}

function ShowImg(id)
{
	window.open("http://www.exposigns.com.mx/showImage.php?ID=" + id,"","height=100,width=100");
}

function CargaPartes()
{
	window.location = 'cotizador.php?SID=' + document.getElementById('sistema').value + '&FID=' + document.getElementById('familia').value + '&CID=' + document.getElementById('categoria').value;
}

function ValidaValor(minimo,maximo,intervalo)
{
	var valor = document.getElementById('largo').value;
//	alert ('Valor Ingresado = ' + valor);
	if (valor % intervalo > 0)
	{
		var residuo = valor % intervalo;
	//	alert ('Residuo = ' + residuo);
		var diferencia = intervalo - residuo;
	//	alert ('Diferencia = ' + diferencia);
		if (diferencia < residuo) { valor = parseInt(valor) + diferencia; }
		else { valor = parseInt(valor) - residuo; }
	}
//	alert ('Valor despues de diferencias = ' + valor);
	if (valor < parseInt(minimo)) { valor = parseInt(minimo); }
//	alert ('Valor < Minimo? = '.valor);
	if (valor > parseInt(maximo)) { valor = parseInt(maximo); }
//	alert ('Valor > Maximo? = '.valor);
	document.getElementById('largo').value = valor;
}

function Valida(form,mod)
{
	var mensaje = 'Los campos marcados con * son requeridos';
	var mensajeCapcha = 'No has escrito el campo de validacion';
	var mensajePass = 'Existe una diferencia entre la contraseña y si confirmacion';
	switch (mod)
	{
		case "Registro":
		{
			if (form.email.value == "") { alert(mensaje); form.email.focus(); return false; }
			if (form.password.value == "") { alert(mensaje); form.password.focus(); return false; }
			if (form.nombre.value == "") { alert(mensaje); form.nombre.focus(); return false; }
			if (form.razonSocial.value == "") { alert(mensaje); form.razonSocial.focus(); return false; }
			if (form.rfc.value == "") { alert(mensaje); form.rfc.focus(); return false; }
			if (form.contacto.value == "") { alert(mensaje); form.contacto.focus(); return false; }
			if (form.domicilio.value == "") { alert(mensaje); form.domicilio.focus(); return false; }
			if (form.colonia.value == "") { alert(mensaje); form.colonia.focus(); return false; }
			if (form.ciudad.value == "") { alert(mensaje); form.ciudad.focus(); return false; }
			if (form.estado.value == "") { alert(mensaje); form.estado.focus(); return false; }
			if (form.pais.value == "") { alert(mensaje); form.pais.focus(); return false; }
			if (form.telefono.value == "") { alert(mensaje); form.telefono.focus(); return false; }
			
			if (form.imagetext.value == "") { alert(mensajeCapcha); form.imagetext.focus(); return false; }
			if (form.password.value != form.cPassword.value) { alert(mensajePass); form.password.focus(); return false; }
			break;
		}
		case "EditaRegistro":
		{
			if (form.email.value == "") { alert(mensaje); form.email.focus(); return false; }
			if (form.password.value == "") { alert(mensaje); form.password.focus(); return false; }
			if (form.nombre.value == "") { alert(mensaje); form.nombre.focus(); return false; }
			if (form.razonSocial.value == "") { alert(mensaje); form.razonSocial.focus(); return false; }
			if (form.rfc.value == "") { alert(mensaje); form.rfc.focus(); return false; }
			if (form.contacto.value == "") { alert(mensaje); form.contacto.focus(); return false; }
			if (form.domicilio.value == "") { alert(mensaje); form.domicilio.focus(); return false; }
			if (form.colonia.value == "") { alert(mensaje); form.colonia.focus(); return false; }
			if (form.ciudad.value == "") { alert(mensaje); form.ciudad.focus(); return false; }
			if (form.estado.value == "") { alert(mensaje); form.estado.focus(); return false; }
			if (form.pais.value == "") { alert(mensaje); form.pais.focus(); return false; }
			if (form.telefono.value == "") { alert(mensaje); form.telefono.focus(); return false; }
			if (form.cPass.checked)
			{
				if (form.password.value == "") { alert("You can not send an empty password"); form.password.focus(); return false; }
				if (form.password.value != form.cPassword.value) { alert("There is a difference between the password and its confirmation"); form.password.focus(); return false; }
			}
			break;
		}
		case "Login":
		{
			if (form.email.value == "") { alert("You have not provided your E-mail"); form.email.focus(); return false; }
			if (form.password.value == "") { alert("You have not provided your Password"); form.password.focus(); return false; }
			break;
		}
		case "RecuperaPass":
		{
			if (form.email.value == "") { alert("You have not provided any E-mail"); form.email.focus(); return false; }
			break;
		}
		case "Cupon":
		{
			if (form.tienda.value == "") { alert("The fields marked with * are required"); form.tienda.focus(); return false; }
			if (form.promocion.value == "") { alert("The fields marked with * are required"); form.promocion.focus(); return false; }
			if (form.descripcion.value == "") { alert("The fields marked with * are required"); form.descripcion.focus(); return false; }
			break;
		}
		case "Envio":
		{
			if (!form.aceptoTerminos.checked) { alert("You have not accepted the ADVERTISING AGENCY AGREEMENT"); form.aceptoTerminos.focus(); return false; }
			break;
		}
		case "Archivo":
		{
			if (form.titulo.value == "") { alert(mensaje); formulario.titulo.focus(); return false; }
			else { window.document.getElementById('cargando').style.display = "block"; return true; }
			break;
		}
		case "Contacto":
		{
			if (form.nombreC.value == "") { alert(mensaje); form.nombreC.focus(); return false; }
			if (form.apellido.value == "") { alert(mensaje); form.apellido.focus(); return false; }
			if (form.telefono.value == "") { alert(mensaje); form.telefono.focus(); return false; }
			if (form.email.value == "") { alert(mensaje); form.email.focus(); return false; }
			if (form.comentarios.value == "") { alert(mensaje); form.comentarios.focus(); return false; }
			break;
		}
	}
}
