// TRANSPARENCIA EM ARQUIVOS PNG NO INTERNET EXPLORER
function correctPNG()
   {
   for(var i=0; i<document.images.length; i++)
      {
      var img = document.images[i]
      var imgName = img.src.toUpperCase()
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
         var imgID = (img.id) ? "id='" + img.id + "' " : "";
         var imgClass = (img.className) ? "class='" + img.className + "' " : "";
         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
         var imgStyle = "display:inline-block;" + img.style.cssText;
         if (img.align == "left") imgStyle = "float:left;" + imgStyle;
         if (img.align == "right") imgStyle = "float:right;" + imgStyle;
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
         img.outerHTML = strNewHTML;
         i = i-1;
         }
      }
   }
if (document.all)
	window.attachEvent("onload", correctPNG);

function para(){
if(navigator.appName == 'Microsoft Internet Explorer') {
			if(event.keyCode=="13"){
			event.keyCode=0;
			return false;
		}
	}
else {
	if(navigator.appName == 'Netscape') {
			if(event.wich=="13"){
			event.wich=0;
			return false;
		}
	}
	}
}

function Formata_Telefone(objeto,tammax,teclapres)
{
	var tecla = teclapres.keyCode;
	vr = objeto.value;
	vr = vr.replace( "(", "" );
	vr = vr.replace( ")", "" );
	vr = vr.replace( " ", "" );
	vr = vr.replace( "-", "" );
	tam = vr.length;
	if (tam < tammax && tecla != 8) {
		tam = vr.length + 1 ;
	}
	if (tecla == 8 ) {
		tam = tam - 1 ;
	}
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
		if ( tam <= 4 ) { 
			objeto.value = vr ;
		}
		if ( (tam > 4) && (tam <= 8) ) {
			objeto.value = vr.substr(0,tam-4) + '-' + vr.substr( tam - 4, tam ) ;
		}
		if ( (tam >= 9) && (tam <= 10) ) {
			objeto.value = '(' + vr.substr(0,2) + ') ' + vr.substr(2,tam-6) + '-' + vr.substr(tam-4,tam) ;			
		}
	}		
}

/* Funções adicionadas por Fábio */

/* Adiciona método trim() à classe String */
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};

/* Validar campos */
/**
 * DHTML date validation script. Courtesy of SmartWebby.com
 * (http://www.smartwebby.com/dhtml/)
 */

/**
 * @desc Valida número inteiro
 * @param string
 *            s String para testar
 * @return boolean
 */
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9")))
        	return false;
    }
    // All characters are numbers.
    return true;
}

/**
 * @desc Retira caracteres de uma string
 * @param s
 * @param bag
 * @return string
 */
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

/**
 * 
 */
function isMail(mail) {
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	if(typeof(mail) == "string") {
		if(er.test(mail)) {
			return true;
		}
	} else if(typeof(mail) == "object") {
		if(er.test(mail.value)) { 
			return true; 
		}
    } else {
    	return false;
	}
}

/**
 * @desc Retorna número de dias em fevereiro pro ano passado
 * @param int
 *            year Ano
 * @return int
 */
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

/**
 * 
 * @param n
 * @return
 */
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {
			this[i] = 30;
		}
		if (i==2) {
			this[i] = 29;
		}
   } 
   return this;
}

/**
 * 
 * @param dtStr
 * @return
 */
function isDate(dtStr){
	var dtCh = '/';
	var daysInMonth = DaysArray(12);
	var pos1 = dtStr.indexOf(dtCh);
	var pos2 = dtStr.indexOf(dtCh, pos1+1);
	var strDay = dtStr.substring(0, pos1);
	var strMonth = dtStr.substring(pos1+1, pos2);
	var strYear = dtStr.substring(pos2+1);
	strYr = strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1)
		strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1)
		strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1)
			strYr=strYr.substring(1);
	}
	month = parseInt(strMonth);
	day = parseInt(strDay);
	year = parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		// alert("A data deve estar no formato: 'dd/mm/yyyy'.");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		// alert("Por favor digite um mes válido.");
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		// alert("Por favor digite um dia válido.");
		return false;
	}
	if (strYear.length != 4 || year==0 || year < 1900 || year > 2999){
		// alert("Por favor digite um ano válido.\nO ano tem que ter 4 dígitos
		// entre 1900 e 2999.");
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		// alert("Por favor digite uma data válida.");
		return false;
	}
	return true;
}

/**
 * 
 */
function difData(data1, data2)
{
	var dtCh = '/';
	var pos1 = data1.indexOf(dtCh);
	var pos2 = data1.indexOf(dtCh, pos1+1);
	var strDay = data1.substring(0, pos1);
	var strMonth = data1.substring(pos1+1, pos2);
	var strYear = data1.substring(pos2+1);
	data1 = strMonth + '/' + strDay + '/' + strYear;

	pos1 = data2.indexOf(dtCh);
	pos2 = data2.indexOf(dtCh, pos1+1);
	strDay = data2.substring(0, pos1);
	strMonth = data2.substring(pos1+1, pos2);
	strYear = data2.substring(pos2+1);
	data2 = strMonth + '/' + strDay + '/' + strYear;

	var d1 = Date.parse(data1);
	var d2 = Date.parse(data2);
	return (((d2)-(d1))/(24*60*60*1000)).toFixed(0);
}

function IsValidTime(timeStr) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		//alert("Time is not in a valid format.");
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null; }

	if (hour < 0  || hour > 23) {
		//alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
		return false;
	}
	if (hour <= 12 && ampm == null) {
//		if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
//			alert("You must specify AM or PM.");
//			return false;
//		}
	}
	if  (hour > 12 && ampm != null) {
		//alert("You can't specify AM or PM for military time.");
		return false;
	}
	if (minute<0 || minute > 59) {
		//alert ("Minute must be between 0 and 59.");
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		//alert ("Second must be between 0 and 59.");
		return false;
	}
	return true;
}

function strTimeToInt(timeStr)
{
	var timePat = /^(\d{1,2}):(\d{2})$/;

	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		//alert("Time is not in a valid format.");
		return false;
	}
	hour = parseInt(matchArray[1], 10);
	minute = parseInt(matchArray[2], 10);
	return (hour * 60) + minute;
}

function getYear(dateStr)
{
	var datePat = /^\d{1,2}\/\d{1,2}\/(\d{2,4})$/;

	var matchArray = dateStr.match(datePat);
	if (matchArray == null) {
		//alert("Time is not in a valid format.");
		return false;
	}
	return parseInt(matchArray[1], 10);
}

