/////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////
function trim(inputString) {

   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);

   while (ch == " ") {
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);

   while (ch == " ") {
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }

   while (retValue.indexOf("  ") != -1) {
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length);
   }
   return retValue;
}
////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////// FUNCIONES DE VALIDACION DE FECHAS
//// colocar en el input ==> onblur="valAno(this)" // ==> onblur="valMes(this,formu.vf_dia)" // ==> onblur="valDia(this)"
function esDigito(sChr){
	var sCod = sChr.charCodeAt(0);
	return ((sCod > 47) && (sCod < 58));
}
//-- le paso el mes y el dia
function finMes(MTxt,DTxt){

	var nMes = parseInt(MTxt.value, 10);
	var nDia = parseInt(DTxt.value, 10);
	switch (nMes)
	{
		case 1: if(nDia <= 31) return true; break;
		case 2: if(nDia <= 29) return true; break;
		case 3: if(nDia <= 31) return true; break;
		case 4: if(nDia <= 30) return true; break;
		case 5: if(nDia <= 31) return true; break;
		case 6: if(nDia <= 30) return true; break;
		case 7: if(nDia <= 31) return true; break;
		case 8: if(nDia <= 31) return true; break;
		case 9: if(nDia <= 30) return true; break;
		case 10: if(nDia <= 31) return true; break;
		case 11: if(nDia <= 30) return true; break;
		case 12: if(nDia <= 31) return true; break;
	}
	return false;
}

function valDia(oTxt){
	var bOk = false;
	if (oTxt.value != "")
	{
		var nDia = parseInt(oTxt.value, 10);
		bOk = bOk || ((nDia >= 1) && (nDia <= 31));
		if (!bOk)
		{
			alert("Dia invalido en Fecha");
			oTxt.value = "";
			oTxt.focus();
		}
	}
	return bOk;
}

function valMes(mTxt,dTxt){
	var bOk = false;
	if (mTxt.value != "")
	{
		var nMes = parseInt(mTxt.value, 10);
		bOk = bOk || ((nMes >= 1) && (nMes <= 12));
		if (!bOk)
		{
			alert("Mes invalido en Fecha");
			mTxt.value = "";
			mTxt.focus();
			return false;
		}

		bOk = (finMes(mTxt,dTxt)) && bOk;
		if (!bOk)
		{
			alert("El Dia es invalido para el Mes ingresado en la Fecha");
			dTxt.value = "";
			dTxt.focus();
		}

	}
	return bOk;
}

function valAno(oTxt){
	var bOk = true;
	if (oTxt.value != "")
	{
		var nAno = oTxt.value;
		bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));
		if (bOk)
		{
			for (var i = 0; i < nAno.length; i++)
			{
				bOk = bOk && esDigito(nAno.charAt(i));
			}
		}
		if (!bOk)
		{
			alert("A�o invalido en Fecha");
			oTxt.value = "";
			oTxt.focus();
		}
	}
	return bOk;
}
////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//// Funciones para validar que ingrese solamente numeros en un campo
/// colocar en el input que se quiera validar lo siguiente ==> onKeyPress="return acceptNum(event)"
var nav4 = window.Event ? true : false;

function acceptNum(evt){
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57));
}
/////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//// funcion que chequea el parametro no sean blancos
//// recibe una cadena
function isBlanco(cadena){
	var s = new String(cadena);
	while (s.indexOf(" ") != -1){
 		s=s.replace(" ","")
	}
	if ( s.length == 0)	return true;
	else return false;
}
//--------------------------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////////////
//// funcion que chequea el email
//// recibe una cadena a evaluar
function isEmail(valor){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
    	return true
	}
	else{
		return false;
	}
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// Abre una nueva ventana 
function openPopUp( ventanaPadre, path_href, w, h, resizable, scrollbars ) {
	
	var name = '';
	if(isNaN(w)) w = screen.width;
	if(isNaN(h)) h = screen.height;
	if(isNaN(resizable)) resizable = 0;
	if(isNaN(scrollbars)) scrollbars = 0;
	
	var winleft = (screen.width - w) / 2;
	var wintop = (screen.height - h) / 2;
	var attrib = 'width='+ w +',height='+ h +',left='+ winleft +',top='+ wintop +
		',resizable=' + resizable + ',scrollbars=' + scrollbars + ',status=0,location=0,directories=0,toolbar=0,menubar=0';

	var ventana = window.open(path_href, name , attrib);
	ventana.opener = ventanaPadre;
	ventana.focus();

	return false;
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// cierra una ventana popup
///// si recibe parametro es para redirigir al padre
function closePopUp() {
	
	if((closePopUp.arguments.length > 0) && !isBlanco(closePopUp.arguments[0])){
		var path = closePopUp.arguments[0];
		window.opener.location.href = path;
	}

	window.opener.focus(); 
	window.close();
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// cambia el tama�o de la ventana
///// recibe parametro en ancho y el alto
function resizeWindowTo(w,h) {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			window.top.outerWidth  = w;
			window.top.outerHeight = h;
		}
		else window.top.resizeTo( w, h );
	}
	
	window.top.moveTo( 0, 0 ); 
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// maximizamos el tama�o de la ventana
function maximizeWindow() {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			if (window.top.screenX>0 || window.top.screenY>0) window.top.moveTo(0,0);
			if (window.top.outerWidth < screen.availWidth)	  window.top.outerWidth = screen.availWidth;
			if (window.top.outerHeight < screen.availHeight)  window.top.outerHeight = screen.availHeight;
		}
		else {
			window.top.moveTo(-4,-4);
			window.top.resizeTo( screen.availWidth+8, screen.availHeight+8 );
		}
	}
}
//--------------------------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////////////
//// muestra una ventana con un div
//// 
var ventanaDivWindow = null;
function openDivWindow(path, w, h, titulo) {
	
	if(isBlanco(path)){
		return false;
	}
	if(isNaN(w) || w <= 0) w = 520;
	if(isNaN(h) || h <= 0) h = 380;
	
	ventanaDivWindow = new Window({className: "alphacube", title: "Sample", width: w,
  						height: h, url: path}); 
	ventanaDivWindow.setTitle( '<B>'+ titulo +'</B>' );
  	ventanaDivWindow.setDestroyOnClose(); 
  	ventanaDivWindow.showCenter( true );
}
//--------------------------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////////////
//// cierra la ventana abierta
////
function closeDivWindow() {
	ventanaDivWindow.close();
  	ventanaDivWindow = null;
  	
  	if((closeDivWindow.arguments.length > 0) && !isBlanco(closeDivWindow.arguments[0])){
		var path = closeDivWindow.arguments[0];
		window.top.location.href = path;
	}
}
//-----------

////////////////////////////////////////////////////////////////
////////////////////////////////
function esconderDiv( div ){
	var op = $(div).getAttribute('opcion');
	op = parseInt( op );
	if(isNaN(op) || op == 0){
		$(div).setAttribute('opcion', '1');
		new Effect.SlideUp( $(div) );
	}
	else{
		//alert(op);
		$(div).setAttribute('opcion', '0');
		new Effect.SlideDown( $(div) );
	}
}	
//-------------

//////////////////////////////////////////////////////////////////////////////////
//// valida que el usuario ingrese un mail
//// 
function validateContacto( f ) {
	var error = "";
	
	if(isBlanco(f.nombre.value))
		error += "Error: Complete su Nombre o Razon Social. \n";

	if(isBlanco(f.pais.value))
		error += "Error: Ingrese su Pais. \n";

	if(!isEmail(f.email.value))
		error += "Error: Complete Correctamente la Direccion de Correo Electronico. \n";
	
	if(isBlanco(f.msg.value))
		error += "Error: Complete el texto de la consulta. \n";
		
	if(error != ""){
		alert(error);
	 	return false;
	}
	else{
		f.submit();
		return true;
	}
	
}
//--------------------------------------------------------------------------------------

//////////////////////////////////////////////////////////////////////////////////
//// valida que el usuario ingrese un mail
//// 
function validateCotizar( f ) {
	var error = "";
	
	if(isBlanco(f.nombre.value))
		error += "Error: Complete su Nombre y Apellido. \n";

	if(!isEmail(f.email.value))
		error += "Error: Complete Correctamente la Direccion de Correo Electronico. \n";
	
	if(isBlanco(f.producto.value))
		error += "Error: Complete el producto que desea consultar. \n";
		
	if(isBlanco(f.msg.value))
		error += "Error: Complete el texto de la consulta. \n";
		
	if(error != ""){
		alert(error);
	 	return false;
	}
	else{
		var str = $('#myForm').serialize();
		AJAXSendCotizar( str );
		//return true;
	}
	
}
//--------------------------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////
///////////// FUNCION PRINCIPAL PARA RECARGAR EL CENTRO DE LA PAGINA
///////////// parametros: params -> los parametros a enviar, string
function AJAXSendCotizar( params ) {

	if(params && !isBlanco(params)){
		$("#divCotizar").html('<div align="center"><img src="img/ajax-loader.gif" border="0" class="img_ajax"></div>');
		$.ajax({
			type: "POST",
			url: "sendCotizar.php",
			data: params,
			success: function(datos){
				$('#divCotizar').replaceWith(datos);
				//alert( "Se guardaron los datos: " + datos);
			},
			error: function(objeto, quepaso, otroobj){
				$('#divCotizar').html(quepaso);
				//alert("Estas viendo esto por que fallé");
				//alert("Pasó lo siguiente: "+quepaso);
			}
		});
	}
	else{
		return false;
	}
}
//--------------------