var calculaPrecioPonEnIndice = -1;

function calculaPrecio(indice,forma) {
	if (typeof(indice) == "undefined") {
		indice = -1;
	}
	if (typeof(forma) == "undefined") {
		forma = document.forma_addCart;
	}
//	alert("usando el indice: "+indice);
	calculaPrecioPonEnIndice = indice;
	variables = "?";
//	variables = new Object();
	elms = forma.getElementsByTagName("input");
	for (i = 0; i < elms.length; i++)
	{
		if (elms[i].type == "radio" && !elms[i].checked) {
			continue;
		}
		if (variables != "?") {
			variables += "&";
		}
		variables += elms[i].name+"="+elms[i].value;
	/*	if (elms[i].name == "Width" || elms[i].name == "Height") {
			elms[i].value = "1";
		}
		eval("variables."+elms[i].name+" = "+elms[i].value);
		*/
	}
	elms = forma.getElementsByTagName("select");
	for (i = 0; i < elms.length; i++)
	{
		if (variables != "?") {
			variables += "&";
		}
		variables += elms[i].name+"="+elms[i].value;
//		variables[elms[i].name] = elms[i].value;
		//eval("variables."+elms[i].name+" = "+elms[i].value);
	}
	//alert(variables);
	sendRequest("../getProductPrice.php"+variables,responsePrecio);
}

function responsePrecio(req) {
	//alert(req.responseText);
	if (calculaPrecioPonEnIndice == -1) {
		document.getElementById("elPrecio").innerHTML = req.responseText;
	}else {
		//alert("Poniendo en el indice: "+"elPrecio_"+calculaPrecioPonEnIndice);
		document.getElementById("elPrecio_"+calculaPrecioPonEnIndice).innerHTML = req.responseText;
	}
}

function ajustaCaracteristicas() {
	sendRequest("genAvailableOptions.php?id_seccion="+document.getElementById("id_seccion").value,responseCaracteristicas);
}

function responseCaracteristicas(req) {
//	document.getElementById("lesOpciones").innerHTML = req.responseText;
	//alert(req.responseText);
	ids = req.responseText.split("|");
	j = 0;
	while (elm = document.getElementById("divCarac"+j)) {
		elm.style.display = "none";
		j++;
	}
	i = 0;
	while (ids[i]) {
		//alert("calando id: "+ids[i]);
		j = 0;
		while (elm = document.getElementById("caracId"+j)) {
			if (elm.value == ids[i]) {
				document.getElementById("divCarac"+j).style.display = "block";
				break;
			}
			j++;
		}
		i++;
	}
}

function checaCambioPadre()
{
  var idPadre = document.getElementById("idPadre");
  if (idPadre.value != "0") {
//	  document.getElementById("afectaEntrada").style.display = "block";
//	  document.getElementById("afectaEntradaComboBox").style.display = "block";
	  //document.getElementById("idEntrada")[0].selected = true;
	  document.getElementById("tipo")[0].selected = true;
//	  document.getElementById("divNombreCampo").style.display = "none";
//	  document.getElementById("divNombreCampoInput").style.display = "none";
	  document.getElementById("divCosto").style.display = "block";
	  document.getElementById("divCostoInput").style.display = "block";
//	  document.getElementById("fieldName").value = "none";
  }else {
//	  document.getElementById("afectaEntrada").style.display = "none";
//	  document.getElementById("afectaEntradaComboBox").style.display = "none";
//	  document.getElementById("idEntrada")[0].selected = true;
//	  document.getElementById("divNombreCampo").style.display = "block";
//	  document.getElementById("divNombreCampoInput").style.display = "block";
	  document.getElementById("divCosto").style.display = "none";
	  document.getElementById("divCostoInput").style.display = "none";
	  document.getElementById("costo").value = "0";
  }
}

function checaPedido() {
	if (document.forma_pedido.Nombre.value == "") {
		alert("El Nombre es Requerido.");
		document.forma_pedido.Nombre.focus();
		return false;
		
	}else if (document.forma_pedido.Direccion.value == "") {
		alert("La Dirección es Requerida.");
		document.forma_pedido.Direccion.focus();
		return false;
		
	}else if (document.forma_pedido.Telefono.value == "") {
		alert("El Telefono es Requerido.");
		document.forma_pedido.Telefono.focus();
		return false;
		
	}else if (document.forma_pedido.Email.value == "") {
		alert("El E-mail es Requerido.");
		document.forma_pedido.Email.focus();
		return false;
		
	}else if (!document.forma_pedido.Email.value.match(/^[a-zA-Z0-9._-]{3,}@[a-zA-Z0-9.-]{3,}\.[a-zA-Z]{2,4}$/g)) {
		alert("El Email no es válido.");
		document.forma_pedido.Email.focus();
		return false;
		
	}else{
		return document.forma_pedido.submit();
	}
}

function checaCarto(id)
{
	if (document["forma_addCart"+id].Width && isNaN(document["forma_addCart"+id].Width.value))
	{
		alert("El Largo debe ser un valor numerico.");
		document["forma_addCart"+id].Width.focus();
		return false;
		
	}else if (document["forma_addCart"+id].Width && document["forma_addCart"+id].Width.value == "")
	{
		alert("El Largo es requerido.");
		document["forma_addCart"+id].Width.focus();
		return false;
		
	}else if (document["forma_addCart"+id].Height && isNaN(document["forma_addCart"+id].Height.value))
	{
		alert("El Alto debe ser un valor numerico.");
		document["forma_addCart"+id].Height.focus();
		return false;
		
	}else if (document["forma_addCart"+id].Height && document["forma_addCart"+id].Height.value == "")
	{
		alert("El Alto es requerido.");
		document["forma_addCart"+id].Height.focus();
		return false;
		
	}else
	{
		return document["forma_addCart"+id].submit();
	}
}

function checaCart()
{
	if (document.forma_addCart.Width && isNaN(document.forma_addCart.Width.value)) {
		alert("El Largo debe ser un valor numerico.");
		document.forma_addCart.Width.focus();
		return false;
		
	}else if (document.forma_addCart.Width && document.forma_addCart.Width.value == "") {
		alert("El Largo es requerido.");
		document.forma_addCart.Width.focus();
		return false;
		
	}else if (document.forma_addCart.Height && isNaN(document.forma_addCart.Height.value)) {
		alert("El Alto debe ser un valor numerico.");
		document.forma_addCart.Height.focus();
		return false;
		
	}else if (document.forma_addCart.Height && document.forma_addCart.Height.value == "") {
		alert("El Alto es requerido.");
		document.forma_addCart.Height.focus();
		return false;
		
	}else {
		return document.forma_addCart.submit();
	}
}

function valida_caracteristica()// VALIDAMOS CARACTERISTICA
{
	checaCambioPadre();
	if(document.forma_caracteristica.nombre.value=="")
	{
		alert("Ingrese un nombre.");
		document.forma_caracteristica.nombre.focus();
		return false;
	}else
	if(document.forma_caracteristica.costo.value=="")
	{
		alert("Ingrese un costo.");
		document.forma_caracteristica.costo.focus();
		return false;
	}else
	/*
	if(document.forma_caracteristica.fieldName.value=="")
	{
		alert("Ingrese un nombre para el campo.");
		document.forma_caracteristica.fieldName.focus();
		return false;
	}else
	if(document.forma_caracteristica.fieldName.value.match(/[^A-Za-zñÑ0-9_]/g))
	{
		alert("El nombre del campo contiene caracteres no validos. Caracteres Permitidos: A-Z, a-z, ñ, Ñ, 0-9, _");
		document.forma_caracteristica.fieldName.focus();
		return false;
	}else*/
	if(isNaN(document.forma_caracteristica.costo.value))
	{
		alert("No es un numero valido.");
		document.forma_caracteristica.costo.focus();
		return false;
	}
	else
	{
		loading('boton_div','espere_div');
		// when all input are correct 
		// return true so the form will submit
		return document.forma_caracteristica.submit();
	}
}
function valida_entrada()// VALIDAMOS ENTRADA
{
	if(document.forma_entrada.titulo.value=="")
	{
		alert("Ingrese un titulo.");
		document.forma_entrada.titulo.focus();
		return false;
	}else if(isNaN(document.forma_entrada.precioM2.value))
	{
		alert("Debe ser un numero.");
		document.forma_entrada.precioM2.focus();
		return false;
	}
	else
	{
		i = 0;
		while (elm = document.getElementById("divCarac"+i)) {
			if (elm.style.display == "none") {
				document.getElementById("carac"+i).checked = false;
			}
			i++;
		}
		loading('boton_div','espere_div');
		// when all input are correct 
		// return true so the form will submit
		return document.forma_entrada.submit();
	}
}
function valida_seccion()// VALIDAMOS SECCION
{
	if(document.forma_seccion.nombre.value=="")
	{
		alert("Ingrese un titulo.");
		document.forma_seccion.nombre.focus();
		return false;
	}
	else
	{
		loading('boton_div','espere_div');
		// when all input are correct 
		// return true so the form will submit
		return document.forma_seccion.submit();
	}
}
function valida_loading()// VALIDAMOS ORDEN SECCION
{
		loading('boton_div','espere_div');
		// when all input are correct 
		// return true so the form will submit
		//return document.forma_OrdenSeccion.submit();
}


//--codigo para quitar boton y poner mensaje de procesando...
function loading(id,idload)
{
	document.getElementById(id).style.display="none";
	document.getElementById(idload).style.display="block";
}

//--limitar textarea
function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}

//-- bbcode
function bbcode_ins(fieldId, tag)
{
	field=document.getElementById(fieldId);
	if(tag=='b' || tag=='quote' || tag=='i' || tag=='u' || tag =='code' || tag=='list')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + ']' + selected + '[/' + tag+']';
		}
 
		//MOZILLA/NETSCAPE/SAFARI support
 
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + ']' + selected + '[/' + tag +']' + field.value.substring(endPos, field.value.length);
		}
	}
	
	if(tag=='left')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + ']' + selected + '[/' + tag + ']';
		}
 
		//MOZILLA/NETSCAPE/SAFARI support
 
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + ']' + selected + '[/' + tag + ']' + field.value.substring(endPos, field.value.length);
		}
	}

	if(tag=='center')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + ']' + selected + '[/' + tag + ']';
		}
 
		//MOZILLA/NETSCAPE/SAFARI support
 
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + ']' + selected + '[/' + tag + ']' + field.value.substring(endPos, field.value.length);
		}
	}

	if(tag=='right')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + ']' + selected + '[/' + tag + ']';
		}
 
		//MOZILLA/NETSCAPE/SAFARI support
 
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + ']' + selected + '[/' + tag + ']' + field.value.substring(endPos, field.value.length);
		}
	}

	if(tag=='*')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + ']' + selected;
		}
 
		//MOZILLA/NETSCAPE/SAFARI support
 
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + ']' + selected + field.value.substring(endPos, field.value.length);
		}
	}
	else if(tag == 'img')
	{
		var path = prompt('Enter image path', 'http://');
		if(!path)
		{
			return;
		}
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + ']' + path + '[/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + ']' + path + '[/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
	else if(tag == 'url')
	{
		var url = prompt('Enter link URL', 'http://');
		var linkText = prompt('Enter link text', '');
		if(!url || !linkText)
		{
			return;
		}
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + '='+url+']' + linkText + '[/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + '='+url+']' + linkText + '[/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
	else if(tag == 'size')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + '=' + document.getElementById("size").value + ']' + selected + '[/' + tag+']';
		}
 
		//MOZILLA/NETSCAPE/SAFARI support
 
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + '=' + document.getElementById("size").value + '] ' + selected + '[/' + tag +']' + field.value.substring(endPos, field.value.length);
		}
	}
	else if(tag == 'color')
	{
		if (document.selection) 
		{
		field.focus();
		var selected = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = '[' + tag + '=' + document.getElementById("color").value + ']' + selected + '[/' + tag+']';
		}
 
		//MOZILLA/NETSCAPE/SAFARI support
 
		else if (field.selectionStart || field.selectionStart == 0) 
		{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var selected = field.value.substring(startPos, endPos);
		field.focus();
		field.value = field.value.substring(0, startPos) + '[' + tag + '=' + document.getElementById("color").value + '] ' + selected + '[/' + tag +']' + field.value.substring(endPos, field.value.length);
		}
	}
	else if(tag == 'youtube')
	{
		var path = prompt('Enter video code', '');
		if(!path)
		{
			return;
		}
		if (document.selection) 
		{
			field.focus();
			sel = document.selection.createRange();
			sel.text = '[' + tag + ']' + path + '[/' + tag+']';
		}
		//MOZILLA/NETSCAPE/SAFARI support
		else if (field.selectionStart || field.selectionStart == 0) 
		{
			var startPos = field.selectionStart;
			var endPos = field.selectionEnd;
			field.focus();
			field.value = field.value.substring(0, startPos)
			+ '[' + tag + ']' + path + '[/' + tag+']'
			+ field.value.substring(endPos, field.value.length);
		} 
	}
}

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}
