// JavaScript Document
var nav4 = window.Event ? true : false;
var dwin = null;

function selPestana(id)
{
	root = 'img/botones/'
	for(i=1;i<=5;i++){
			document.getElementById('p'+i).src = root + 'pestana'+i+'g.jpg';
	}
	document.getElementById('p'+id).src = root + 'pestana'+id+'v.jpg';
	iframe = document.getElementById('mainFrame');
	switch(id){
		case 1:
			iframe.src = "form_vuelos.asp";
			break;
		case 2:
			iframe.src = "form_hoteles.asp";
			break;
		case 3:
			iframe.src = "form_cruceros.asp";
			break;
		case 4:
			iframe.src = "form_vehiculos.asp";
			break;
		case 5:
			iframe.src = "form_paquetes.asp";
			break;
	}
}

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));
}

function validateField()
{
	var mElement = validateField.arguments[0];
	var mOptions = validateField.arguments[1];
	
	if(mOptions.required)
	{
		if(mElement.value=="")			
			return false;
	}
	
	if(mOptions.email)
	{
		if(!IsEmail(mElement.value))
			return false;
	}
	
	if(mOptions.length>0)
	{
		if(mElement.value.length<mOptions.length)
			return false;
	}
	
	if(mOptions.numeric)
	{
        if (!Abs(mElement.value)) 
			return false;
	}
	
	if(mOptions.range)
	{
		if(!((Abs(mElement.value)>=mOptions.range.min)&&(Abs(mElement.value)<=mOptions.range.max)))
			return false;
	}
	
	return true;
}

function IsEmail(Expression)
{
        if (Expression == null)
                return (false);

        var supported = 0;
        if (window.RegExp)
        {
                var tempStr = "a";
                var tempReg = new RegExp(tempStr);
                if (tempReg.test(tempStr)) supported = 1;
        }
        if (!supported)
                return (Expression.indexOf(".") > 2) && (Expression.indexOf("@") > 0);
        var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
        var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
        return (!r1.test(Expression) && r2.test(Expression));
}

function Abs(Number)
{
        Number = Number.toLowerCase();
        RefString = "0123456789.-";

        if (Number.length < 1)
                return (false);

        for (var i = 0; i < Number.length; i++)
        {
                var ch = Number.substr(i, 1)
                var a = RefString.indexOf(ch, 0)
                if (a == -1)
                        return (false);
        }

        if (Number < 0)
                return (Number * -1)

        return Number;
}

function debug(msg)
{
    if ((dwin == null) || (dwin.closed))
    {
        dwin = window.open("","debugconsole","scrollbars=yes,resizable=yes,height=100,width=300");
        dwin.document.open("text/html", "replace");
    }

    dwin.document.writeln('<br/>'+msg);
    dwin.scrollTo(0,10000);
    dwin.focus();

    // uncomment this if you want to see only last message , not
    // all the previous messages

    // dwin.document.close();
}

function handleErrors(msg, url, line)
{
    var errorString = "JavaScript Error Occured\n";
    errorString += "Message: "+msg+"\n";
    errorString += "Url: "+url+"\n";
    errorString += "Line: "+line;

    alert(errorString);

    return true;
}

//window.onerror = handleErrors;

//*********************************************************************
// Propias del Proyecto
//*********************************************************************

function controlStyle()
{
	var mValid = controlStyle.arguments[0];
	var mElement = controlStyle.arguments[1];
	var mErrors = controlStyle.arguments[2];
	
	if(mValid==true)
	{
		mElement.className = "";
	}
	else
	{
		mElement.className = "error";
		mErrors ++;
	}
	
	return mErrors;
}

function comboControlStyle()
{
	var mValid = comboControlStyle.arguments[0];
	var mElement = comboControlStyle.arguments[1];
	var mErrors = comboControlStyle.arguments[2];
	
	if(mValid==true)
	{
		//mElement.setStyle({backgroundColor: ''});
		mElement.className = '';
	}
	else
	{
		//mElement.setStyle({backgroundColor: '#FFECEC'});
		mElement.className = 'cerror';
		mErrors ++;
	}
	
	return mErrors;
}

function validateContactsFields(mErrors)
{
		mErrors = controlStyle(validateField($("nombre"),{required:true,length:2}),$("nombre"),mErrors);
		mErrors = controlStyle(validateField($("apellido"),{required:true,length:2}),$("apellido"),mErrors);
		mErrors = controlStyle(validateField($("telhab"),{required:true,length:7,numeric:true}),$("telhab"),mErrors);
		mErrors = controlStyle(validateField($("correo"),{required:true,length:5,email:true}),$("correo"),mErrors);
		mErrors = controlStyle(validateField($("telcel"),{required:true,length:7,numeric:true}),$("telcel"),mErrors);	
		return mErrors;		
}

function validateTripulants(mErrors)
{
		var nPasajeros = $("npasajeros").value;
		for(i=1;i<=nPasajeros;i++)
		{
			mErrors = controlStyle(validateField($("nombre"+i),{required:true,length:2}),$("nombre"+i),mErrors);
			mErrors = controlStyle(validateField($("apellido"+i),{required:true,length:2}),$("apellido"+i),mErrors);
			mErrors = controlStyle(validateField($("tipop"+i),{required:true}),$("tipop"+i),mErrors);
		}	
		return mErrors;
}

function validateMultiDestiny(mErrors)
{
	var nCiudades = $("nciudades").value;
	for(i=1;i<=nCiudades;i++)
	{
		mErrors = controlStyle(validateField($("desde"+i),{required:true,length:2}),$("desde"+i),mErrors);
		mErrors = controlStyle(validateField($("hasta"+i),{required:true,length:2}),$("hasta"+i),mErrors);
	}
	return mErrors;
}

function validateSimpleDestiny(mErrors)
{
	mErrors = controlStyle(validateField($("desde"),{required:true,length:2}),$("desde"),mErrors);
	mErrors = controlStyle(validateField($("hasta"),{required:true,length:2}),$("hasta"),mErrors);
	return mErrors;
}

function validateBethrooms(mErrors)
{
	var nCabinas = $("ncabinas").value;
	for(i=1;i<=nCabinas;i++)
	{
		if(!validateField($("adultos"+i),{range:{min:1,max:4}}))
			mErrors = comboControlStyle(validateField($("ninos"+i),{range:{min:1,max:4}}),$("ninos"+i),mErrors);	

		if(!validateField($("ninos"+i),{range:{min:1,max:4}}))
			mErrors = comboControlStyle(validateField($("adultos"+i),{range:{min:1,max:4}}),$("adultos"+i),mErrors);
	}	
	return mErrors;
}

//*********************************************************************
