
/* ---------------------------------------------------------------  
1)	f:sql;s:49;m:49;r
donde	
	f: formato sql no acepta " ni '
	s: size
	m: maxlength
	r: requerido

2) 	sql;49;r 
Donde
	Toma formato sql
	el size y el maxlength lo setea en 49
	y es requerido
 ---------------------------------------------------------------  */

//--------------------------------------------------------------- SetearFormato

function SetearFormato(obj)
{
var sFormat		= obj.getAttribute("format")

	if (sFormat == "DD/MM/AAAA" || sFormat == "MM/DD/YYYY")
		SetearFecha(obj)
	/*elseif(window.event.srcElement.tagName == "TEXAREA")
		SetearTextArea(obj)*/
	else
		SetearText(obj)

}

//--------------------------------------------------------------- SetearFecha

function SetearFecha(dtbDate)
{
var sFormat = dtbDate.getAttribute("format")
	dtbDate.outerHTML = dtbDate.outerHTML.substr(0, dtbDate.outerHTML.length - 1) + " onblur=ValidaFecha('" + sFormat + "') onkeypress=ValidaKeyFecha() maxlength=10 size=10>"
}

//--------------------------------------------------------------- SetearText

/*function SetearTextArea()
{

}*/

//--------------------------------------------------------------- 

function SetearText(obj)
{
var sFormat		= obj.getAttribute("format")
var bRequired	= false
var sStr = ""
var iPosAnte = 0
	iPos = sFormat.search(":")
	/*if(iPos > 0)
	{*/
		while (iPos > 0)
		{
			sQue = sFormat.substr(0,iPos)
			if (sFormat.search(";")>0)
				sValor = sFormat.substr(iPos + 1, sFormat.search(";")-2)
			else
				sValor = sFormat.substr(2, sFormat.length)
			switch (sQue)
			{
				case "f":	if (sValor=="sql")
								sStr = sStr + " onkeypress=ValidaKeySql() ";
							else
								sStr = sStr + " onkeypress=ValidaKeyInt() "; 
							break;
				case "s": sStr = sStr + " size=" + sValor; break;
				case "m": sStr = sStr + " maxlength=" + sValor; break;
				case "r": sStr = sStr + " onblur=ValidarRequerido() "; break;
			}
			if (sFormat.search(";")>0)
				sFormat = sFormat.substr(sFormat.search(";")+1, sFormat.length)
			else
				sFormat = sFormat.substr(1, sFormat.length)
			iPos = sFormat.search(":")
			if (iPos <= 0)
				iPos = sFormat.search(";")
			if (iPos <= 0 && sFormat.length > 0)
			{
				iPos =1
				iPosAnte-= iPos
			}
			if(iPosAnte == -2)
				break;
		}
	/*}
	else
	{
		iPos = sFormat.search(":")
		if(iPos > 0)
		{
			while (iPos > 0)
			{
				sQue = sFormat.substr(0,iPos)
				sValor = sFormat.substr(iPos + 1, sFormat.search(";")-2)
				switch (sQue)
				{
					case "f": sStr = sStr + " onkeypress=ValidaKeySql() "; break;
					case "s": sStr = sStr + " size=" + sValor; break;
					case "m": sStr = sStr + " maxlength=" + sValor; break;
					case "r": sStr = sStr + " onblur=ValidarRequerido() "; break;
				}
				if (sFormat.search(";")>0)
					sFormat = sFormat.substr(sFormat.search(";")+1, sFormat.length)
				else
					sFormat = sFormat.substr(1, sFormat.length)
				iPos = sFormat.search(":")
				if (iPos <= 0)
				{
					iPos = sFormat.search(";")
					
				}
				if (iPos <= 0 && sFormat.length > 0)
				{
					iPos =1
					iPosAnte=-iPos
				}
				if(iPosAnte == -2)
					break;
			}
		}*/
	/*if(window.event.srcElement.tagName == "TEXAREA")
		obj.outerHTML = obj.outerHTML.substr(0, 10) + " " + sStr + " " + obj.outerHTML.substr(10 + sStr.length , obj.outerHTML.length)
	else*/
		obj.outerHTML = obj.outerHTML.substr(0, obj.outerHTML.length - 1) + " " + sStr + ">"
}

//--------------------------------------------------------------- ValidaKeyFecha

function ValidaKeyFecha() 
{
	if ((window.event.srcElement.value.length == 2 || window.event.srcElement.value.length == 5) && 47 != window.event.keyCode)
		window.event.srcElement.value = window.event.srcElement.value + "/"
	else if (46 < window.event.keyCode && window.event.keyCode < 58)
		return
	else
		window.event.keyCode = 0
}

//--------------------------------------------------------------- ValidaKeySql

function ValidaKeySql() 
{
	if (window.event.keyCode != 34 && window.event.keyCode != 39)
		return
	else
		window.event.keyCode = 0
}

//--------------------------------------------------------------- ValidaKeyInt

function ValidaKeyInt() 
{
	if (47 < window.event.keyCode && window.event.keyCode < 58)
		return
	else
		window.event.keyCode = 0
}

//--------------------------------------------------------------- ValidaFecha

function ValidaFecha(sFormato)
{
var sExp, sDate, bOk
var iDia, iMes, iAnio
	sExp = /[0-9]{2}\/[0-9]{2}\/[0-9]{4}/
	sDate = window.event.srcElement.value

	if (sDate == "")
		return
	
	if (sFormato == "MM/DD/AAAA" || sFormato == "MM/DD/YYYY")
	{
		iMes  = sDate.substr(0,2)
		iDia  = sDate.substr(3,2)
	}
	else							//Por defecto toma DD/MM/AAAA
	{
		iDia  = sDate.substr(0,2)
		iMes  = sDate.substr(3,2)
	}
	iAnio = sDate.substr(6,4)

	bOk = false
	switch (iMes)
	{
		case "01": if (iDia <= 31) bOk = true; break;
		case "02": if ((iAnio%4 == 0 && iDia <= 29) || (iDia <= 28)) bOk = true; break;	
		case "03": if (iDia <= 31) bOk = true; break;
		case "04": if (iDia <= 30) bOk = true; break;
		case "05": if (iDia <= 31) bOk = true; break;
		case "06": if (iDia <= 30) bOk = true; break;
		case "07": if (iDia <= 31) bOk = true; break;
		case "08": if (iDia <= 31) bOk = true; break;
		case "09": if (iDia <= 30) bOk = true; break;
		case "10": if (iDia <= 31) bOk = true; break;
		case "11": if (iDia <= 30) bOk = true; break;
		case "12": if (iDia <= 31) bOk = true; break;
	}
	
	if (!sExp.test(sDate) || !bOk)
	{
		if (sFormato == "MM/DD/AAAA" || sFormato == "MM/DD/YYYY")
			alert("Fecha Inválida. Formato válido MM/DD/YYYY.")
		else
			alert("Fecha Inválida. Formato válido DD/MM/AAAA.")
		window.event.srcElement.focus()
		return
	}
}
//--------------------------------------------------------------- ValidarRequerido
function ValidarRequerido()
{
	if (window.event.srcElement.value == "")
	{
		alert("Required Field")
		window.event.srcElement.focus()
	}
}

//--------------------------------------------------------------- ValidarEmail

function ValidarEmail(cadena)
{ 
var emailexp = /[a-z_0-9\.]@[a-z_0-9\.]+\.([a-z]{2}|[a-z]{3})$/i

	if (cadena == "" )
		return 1
	if (emailexp.test(cadena))
	   return 1
	else
	   return 0
}
//--------------------------------------------------------------- Abrir ventana Popup
function venPopup(sIma){ 
	var sParam, sPag
	sPag = "verImagenAmpliada.asp?sImagenAmpliada=" + sIma
	sParam = 'menubar=no'	
	sParam = sParam + ',resizable=no'
	sParam = sParam + ',scrollbars=no'
	sParam = sParam + ',directories:no'
	sParam = sParam + ',status=no'
	sParam = sParam + ',titlebar=no'
	sParam = sParam + ',toolbar=no'
	sParam = sParam + ',width=340' 
	sParam = sParam + ',height=300'
	sParam = sParam + ',left=100'
	sParam = sParam + ',top=80'
	window.open(sPag,'NuevaVentana',sParam)
}