// JavaScript Document

var isSafari = navigator.userAgent.indexOf("Safari") != -1;
	var isGecko = navigator.userAgent.indexOf("Gecko") != -1;
	var isOpera = navigator.userAgent.indexOf("Opera") != -1;
	var isIE = navigator.appName == "Microsoft Internet Explorer";
	if(isSafari)
		isGecko = false;
		
var keyAction = "";
var GET;
var COOKIES;
	var emptyColor="#ffaaaa";
	var errorColor="#ffaaaa";
function reloadSite() {
		location.reload(true);
	}

function getCookie(variable) {
	if(COOKIES == null)
		setCookies();
	return COOKIES[variable]
}

function trim(STRING){
STRING = lTrim(STRING);
return rTrim(STRING);
}

function rTrim(STRING){
while(STRING.charAt((STRING.length -1))==" "){
STRING = STRING.substring(0,STRING.length-1);
}
return STRING;
}


function lTrim(STRING){
while(STRING.charAt(0)==" "){
STRING = STRING.replace(STRING.charAt(0),"");
}
return STRING;
}

function toNumber(date) {
	dateArr = date.split("-");
	var number = 0;
	number = dateArr[0] * 365;
	number += dateArr[1] * 31;
	number += dateArr[2]*1;
	return number;
}
var month3Array = new Array("ene","feb","mar","abr","may","jun","jul","ago","sep","nov","oct","dic");
var monthArray = new Array("","Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre");

function dateFormat(date) {
	dateArr = date.split("-");
	return ""+dateArr[2]+"/"+month3Array[dateArr[1]-1]+"/"+dateArr[0];
}

	function fullDateFormat(date) {
		dateArr = date.split("-");
		return ""+dateArr[2]+" de "+monthArray[dateArr[1]*1]+" de "+dateArr[0];
	}

function setBackColor(object, color) {
			//object.bgColor = color;
			object.style.backgroundColor = color;
	}
	
function keyCheck(evt) {
	if(!evt)
		evt = window.event;
	var key=evt.keyCode;
	
	if(key == 13)
		eval(keyAction);
}
function getObj(objId) {
	
	return typeof objId == "string" ? document.getElementById(objId):objId;	
}
function validateForm(form) {
	
	/*
		r: required
		i: integer
		n: natural ( > 0)
		f: float
		uf: unsigned float ( > 0)
		d: date
		t: time
		e: email
	*/
	form = getObj(form);
	if(form == null)
		return false;
	var i = 0;
	var expected;
	var field;
	var fieldVal;
	var validField;
	var color;
	var isValid = true;
	while(form.elements[i] != null) {
		field = form.elements[i];

		if(field.type != "checkbox" && field.type != "radio" && field.type != "button" && field.type != "hidden" && field.getAttribute("temp")!="aaa") {
			
					fieldVal = field.value.trim();
			expected = form.elements[i].getAttribute("expected");
			if(expected != null && !expected.isEmpty()) {
				expected = expected.split("-");
				if(expected[1] == "r" && field.value.isEmpty()) {
					setBackColor(field,	emptyColor);
					isValid = false;
				} else {
					if(field.value.isEmpty()) {
						validField = true;
					} else {
						switch(expected[0]) {
							case "i":
								validField = fieldVal.isInt();
								break;
							case "n":
								validField = fieldVal.isInt() && fieldVal.toInt() > 0;
								break;
							case "c":						
								validField = fieldVal.isInt() && fieldVal.toInt() >= 0 && fieldVal.toInt() <= 100;
								break;
							case "f":
								validField = fieldVal.isFloat();
								break;
							case "uf":
								validField = fieldVal.isFloat() && fieldVal.toFloat() >= 0;
								break;
							case "d":
								validField = fieldVal.isDate();
								break;
							case "t":
								validField = fieldVal.isTime();
								break;
							case "e":
								validField = fieldVal.isEmail();
								break;
							case "mt":
								validField = fieldVal.validateTecEmail();
								break;
							case "nt":
								validField = fieldVal.validateTecNomina();
								break;
							default:
								validField = true;
								break;
						}
					}
					
					isValid = isValid && validField;
					color = validField ? "":errorColor;
					setBackColor(field,color);
				}
			}
		}
			i++;
	}
	return isValid;
}

function setGet() {
	var queryArr = new Array();
	var query = location.search.substr(1).split("&");
	var i = 0;
	var value;
	while(query[i] != null) {
		value = query[i].split("=");
		queryArr[trim(value[0])] = value[1];
		i++;
	}
	GET = queryArr;
	return GET;
}

function setCookies() {
	var cookiesArr = new Array();
	var cookies = document.cookie.split(";");
	var i = 0;
	var value;
	while(cookies[i] != null) {
		value = cookies[i].split("=");
		cookiesArr[trim(value[0])] = value[1];
		i++;
	}
	COOKIES = cookiesArr;
	return COOKIES;
}


String.prototype.trim=function()
{	
	return this.replace(/^[\s\t\r\n]+|[\s\t\r\n]+$/g,'');	
}

String.prototype.isEmpty=function()
{	
	return this.trim().length==0?true:false;
}
	
/////////////////////////////////

/////////agregar escape//////
String.prototype.escape=function()
{
	return escape(this);
}
/////////////////////////////////

String.prototype.toInt=function()
{
	return Math.floor(this*1);
}

String.prototype.toFloat=function()
{
	return this*1.0;
}



String.prototype.isInt=function()
{
	if(this.isEmpty())
	{
		return false;
	}
	var regex=/^-?[\d]+$/
	return regex.test(this);
}

String.prototype.isFloat=function()
{
	if(this.isEmpty())
	{
		return false;
	}
//	regex=/^(-?\d)[\d]*([\d]|([\.][\d]))[\d]*)$/
	regex = /^(-)?\d*(\d+\.?|\.?\d+)\d*$/
	return regex.test(this)
}

String.prototype.isDate=function()
{	
	regexOne=/^[\d]{1,2}-[\d]{1,2}-[\d]{4}$/;
	regexTwo = /^[\d]{4}-[\d]{1,2}-[\d]{1,2}$/;
	var format;
	if(!regexOne.test(this) && !regexTwo.test(this))
	{
		return false;
	}
	format = regexOne.test(this) ? 1:2;
	var date=this.split('-');	
	var day,month,year;
	day = format == 1 ? date[0]:date[2];
	day = day.toInt();
	month=date[1].toInt();
	year= format == 1 ? date[2]:date[0];
	year = year.toInt();
	if(day> 31 || month>12 || day == 0 || month == 0)
	{
		return false;
	}
	var monthArr =new Array(1,3,5,7,8,10,12);
	if(!monthArr.inArray(month))
	{
		if(day>30)
		{
			return false;
		}
	}
	if(!isLeap(year) && month==2 && day>28 )
	{
		return false;
	}
	if(isLeap(year) && month ==2 && day>29)
	{
		return false;
	}
	return true;	
}

String.prototype.isTime=function()
{
	regex=/^[\d]{1,2}:[\d]{1,2}(:[\d]{1,2})?$/
	if(!regex.test(this))
	{
		return false;
	}
	var hour=this.split(':');
	if(hour[0].toInt()>24 || hour[1].toInt()>59)
	{
		return false;
	}
	if(hour[2])
	{
		if(hour[2].toInt()>59)
		{
			return false;
		}
	}
	return true;
}


////////////////////////////////////////////////////

String.prototype.isEmail = function () 
{ 
	var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	var matches = rx.exec(this);
	return (matches != null && this == matches[0]); 
}

function isLeap(year) {
	return year%4 == 0 && (year%100 != 0 || year%400 == 0);
}

Array.prototype.inArray=function(value,regex)
{
	if(value==null)
	{
		return false;
	}		
	for(var i=0;i<this.length;i++)
	{
		if(regex)
		{			
			if(value.test(this[i]))
			{
				return true;
			}
			continue;
		}
		if(value==this[i])
		{
			return true;
		}
	}
	return false;	
}

function show(obj) {
	getObj(obj).style.display = "block";
	getObj(obj).style.visible = "visible";
	getObj(obj).style.visibility = "visible";
}

function hide(obj) {
	getObj(obj).style.display = "none";
	//getObj(obj).style.visible = "hidden";
	getObj(obj).style.visibility = "hidden";
}

function clearObj(objName) {
		var obj = getObj(objName);
		if(obj != null)
			obj.innerHTML = "";
	}
