


function isLowerCase (c) { return ((c >= "a") && (c <= "z")); }
function isUpperCase (c) { return ((c >= "A") && (c <= "Z")); }

function max(a, b) { if (a > b) return a; return b; }

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


function getObj(id)
{
	
	if (document.getElementById)
		return document.getElementById(id);
	
	
	if (document.all)
		return document.all[id];
}

function selectedVal(s)
{
	return s.options[s.selectedIndex].value;
}


function setOption(obj, val)
{
	if (obj && val >= 0)
	{
		for (var i = 0; i < obj.options.length; i++)
		{
			if (obj.options[i].value == val)
			{
				obj.selectedIndex = i;
				break;	
			}
		}
	}
}

function getXMLHttpRequestObject()
{	 
	if (window.XMLHttpRequest)
		return new XMLHttpRequest()
	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
}

function show(id)
{
	getObj(id).style.display = 'inline';	
}

function hide(id)
{
	getObj(id).style.display = 'none';	
}


function checkEmail(email)
{
	var reg = /^[A-Za-z0-9_\-\.]+\@[A-Za-z0-9]+[A-Za-z0-9\-\.]{0,}[A-Za-z0-9]+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
}



function submitEnter(e, fptr)
{
	var key = getKeyCode(e);
	
	if (key == 13)
	{
		if (fptr != null)
			fptr();
		return false;
	}
	return true;
}


function limitInput(e, fptr, allDigits, allLetters, chars)
{
	var key = getKeyCode(e);

	if (key == null)
		return true;
		
	if (key == 13)
	{
		if (fptr != null)
			fptr(); 
		return false;
	}
	
	
	if (key == 0 || key == 8 || key == 9 || key == 27)
		return true;
	
	var keychar = String.fromCharCode(key);	

	
	if (allDigits && keychar >= '0' && keychar <= '9')
		return true;
	
	
	if (allLetters && (keychar >= 'a' && keychar <= 'z' || keychar >= 'A' && keychar <= 'Z'))
		return true;
	
			
	if (chars != null && chars.indexOf(keychar) > -1)
		return true;
		
	return false;
}

function numbersOnly(e, chars, fptr)
{
	return limitInput(e, fptr, true, false, chars);
}

function numbersAndLettersOnly(e, chars, fptr)
{
	return limitInput(e, fptr, true, true, chars);
}


function emailInput(e, fptr)
{
	return limitInput(e, fptr, true, true, "@.-_");
}


function formatField(myfield)
{
	var val = extractNumbers(myfield.value);
	myfield.value = formatNumbers(val);
}


function formatNumbers(val)
{
	val += ""; 
	var millionSeparator = ',';
	var thousandSeparator = ',';
	var len = val.length;
	
	if (len > 6)
		return val.substring(0, len-6) + 
			millionSeparator + val.substring(len-6, len-3) + 
			thousandSeparator +  val.substring(len-3, len);
		
	if (len > 3)
		return val.substring(0, len-3) + thousandSeparator +  
			val.substring(len-3, len);
	
	return val;
}


function extractNumbers(s)
{
	var ret = "";
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if ( ("0123456789").indexOf(c) > -1)
		{
			ret += c;
		}	
	}
	return ret;
}


function getKeyCode(e)
{
	if (window.event)
		return e.keyCode;
	 return e.which;
}


function getDateTime(millis)
{
	var date = new Date();
	date.setTime(millis);
	
	var year = date.getYear();
   var day  = date.getDate();
   var month = date.getMonth();
   var hours = date.getHours();
   var minutes = date.getMinutes();
   
   if (minutes <= 9) minutes = "0" + minutes;
   if (hours <= 9) hours = "0" + hours;
   
   return day + " " + months[month] + " " + year + " " + hours + ":" + minutes;
}


function getDate(millis)
{
	var date = new Date();
	date.setTime(millis);
	
   var day = date.getDate();
   var month = date.getMonth();
   
   return day + " " + months[month].substring(0,3);
}


function getCookie(nameOfCookie)
{
	if (document.cookie.length > 0) 
	{ 
		var begin = document.cookie.indexOf(nameOfCookie+"="); 
		if (begin != -1)
		{ 
			begin += nameOfCookie.length + 1; 
			var end = document.cookie.indexOf(";", begin);
			if (end == -1)
				end = document.cookie.length;
			return unescape(document.cookie.substring(begin, end)); 
		} 
	}
	return null; 
}


function setCookie(name, value, expireSeconds) 
{
	var expireDate = new Date ();
	expireDate.setTime(expireDate.getTime() + expireSeconds * 1000);
	document.cookie = name + "=" + escape(value) + "; path=/" +
	(expireSeconds ? "; expires=" + expireDate.toGMTString() : "");
}


function delCookie(name) 
{
	if (getCookie(name))
		document.cookie = name + "=; path=/; expires=Thu, 01-Jan-01 00:00:01 GMT";
}


function preloadImage(path)
{
	var img = new Image();
	img.src = path;
	return img;
}


function checkCapitals(text, minCaps, percent)
{
	var upper = 0;
	var lower = 0;
	var len = text.length;
	
	for (var i=0; i<len; i++) {
		var c = text.charAt(i);
		if (isUpperCase(c))
			upper++;
		else if (isLowerCase(c))
			lower++;
	}
	if (lower > 0 && upper == 0 && isLowerCase(text.charAt(0)))
		return 2;
	
	
	if (upper > minCaps && (upper * percent) > lower)
		return 1;
		
	return 0;
}


function fixCapitals(s, space)
{
	var ret = "";
	var lastDot = true;
	for (var i=0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if (lastDot && !(/[-, "(]/).test(c))
		{
			ret += c.toUpperCase();
			lastDot = false;	
		}
		else
			ret += c.toLowerCase();
			
		if ((space && c == ' ') || (/[-*.\n!?]/).test(c))
			lastDot = true;	
	}
	return ret;	
}


function fixName(s)
{
	s = fixCapitals(s, true);
	if (s.indexOf(" De ") != -1) s = s.replace(/ De /g, " de ");
	if (s.indexOf(" Del ") != -1) s = s.replace(/ Del /g, " del ");
	if (s.indexOf(" Los ") != -1) s = s.replace(/ Los /g, " los ");
	if (s.indexOf(" Las ") != -1) s = s.replace(/ Las /g, " las ");
	if (s.indexOf(" El ") != -1) s = s.replace(/ El /g, " el ");
	if (s.indexOf(" La ") != -1) s = s.replace(/ La /g, " la ");
	if (s.indexOf(" Y ") != -1) s = s.replace(/ Y /g, " y ");
	if (s.indexOf(" A ") != -1) s = s.replace(/ A /g, " a ");
	if (s.indexOf(" O ") != -1) s = s.replace(/ O /g, " o ");
	if (s.indexOf("Sa de Cv") != -1) s = s.replace("Sa de Cv", "SA de CV");
	return s;
}

function clearText(field, defval)
{
	if (field.value == defval)
		field.value = "";
}

function setText(field, defval)
{
	if (field.value == "")
		field.value = defval;
}

function showImage(url, name, title, width, height)
{
	newwin = window.open('', name, 'width='+width+',height='+height+',scrollbars=0,menubars=0,toolbars=0,location=0,directories=0,status=0');
	newwin.document.open();
	newwin.document.write('<html><head><title>'+title+'</title></head>');
	newwin.document.write('<body style="margin:0;padding:0"><img src='+url+' width='+width+' height='+height+'></body></html>');
	newwin.document.close();
}
