function somente_numero(campo){
    var digits="0123456789"
    var campo_temp
    for (var i=0;i<campo.value.length;i++){
      campo_temp=campo.value.substring(i,i+1)    
      if (digits.indexOf(campo_temp)==-1){
            campo.value = campo.value.substring(0,i);
            break;
       }
    }
}

function show() {
	for (lnArg = 0; lnArg < arguments.length; lnArg ++) {
		lvArg = arguments[lnArg];
		if (typeof(lvArg) == "object") {
			if (lvArg.constructor.toString().indexOf("Array") > -1) {
				for (lnItem = 0; lnItem < lvArg.length; lnItem ++)
					document.getElementById(lvArg[lnItem]).style.display = "block";
			} else {
				if (lvArg.style) {
					lvArg.style.display = "block";
				}
			}
		} else {
			if (document.getElementById(lvArg))
				document.getElementById(lvArg).style.display = "block";
		}
	}
}

function hide() {
	for (lnArg = 0; lnArg < arguments.length; lnArg ++) {
		lvArg = arguments[lnArg];
		if (typeof(lvArg) == "object") {
			if (lvArg.constructor.toString().indexOf("Array") > -1) {
				for (lnItem = 0; lnItem < lvArg.length; lnItem ++)
					document.getElementById(lvArg[lnItem]).style.display = "none";
			} else {
				if (lvArg.style) {
					lvArg.style.display = "none";
				}
			}
		} else {
			if (document.getElementById(lvArg))
				document.getElementById(lvArg).style.display = "none";
		}
	}
}


function ShowFlashInLightbox() {
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="400" height="300">\n');
    document.write('<param name="movie" value="photo_gallery/viewer.swf" />\n');
    document.write('<param name="quality" value="high">\n');
    document.write('<embed src="photo_gallery/viewer.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="400" height="300"></embed>\n');    
    document.write('</object>\n');
}

function ismaxlength(obj, teste, mlength) {
	//var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
		obj.value=obj.value.substring(0,mlength)
	if(arguments[1]){
		arguments[1].innerHTML = mlength - obj.value.length;
	}
}

// Javascript library for form validation
// Author: Alan França (gorash@gmail.com)

var blnIsNetscape=eval(navigator.appName=="Netscape");
if (blnIsNetscape) document.captureEvents(Event.KEYPRESS);

arrSettings=new Array();
arrRules=new Array();

function set(objField,strRules,strName) {  
	arrSettings[arrSettings.length]=new Array(objField,strRules,strName);
}

function verifyForm(objForm) { //Function that verifies datatype consistence and other rules, defined by the set() function, of a form. Returns true if the form is ok to be submitted, false otherwise
	loadSettings(); //Just executes the set() statements, which are defined on the page
	for (intField=0;intField<arrSettings.length;intField++) { //Go through all form items
			arrRules=arrSettings[intField][1].split(";"); //The rules for a field are in an array containing fieldobject,rules_separated_by_commas. Split the rules.
			for (intRule=0;intRule<arrRules.length;intRule++) { //Go through the rules...
			
				strRule=trim(arrRules[intRule]);
				
				if (strRule.indexOf(":")>-1) { //A kind of rule that has parameters, get its name and value
					strRuleName=strRule.split(":")[0];
					
					strRuleValue=strRule.split(":")[1];
					
				}
				else {
					strRuleName=strRule;
					
					strRuleValue="";
				}

				switch (strRuleName) { //Switch the type of rule
					case "required":
						if (getFieldValue(arrSettings[intField][0]).replace(" ","")=="") {
							alert("O campo " + arrSettings[intField][2] + " é de preenchimento obrigatório");
							//alert("O campo " + getFieldName(arrSettings[intField][0]) + " é de preenchimento obrigatório");
							arrSettings[intField][0].focus();
							return(false);
						}
						break;
					case "email":
						if (!verifyMail(trim(getFieldValue(arrSettings[intField][0])),'E-mail inválido')) {arrSettings[intField][0].focus(); return(false);}
						break;
					//In this case, "Name" parameter will be used to pass the acceptable file types
					case "file":
						if (!checkFileType(arrSettings[intField][0],strRuleValue)) return(false);
						break;
					case "length":
						if (!verifyLength(strRuleValue,strRuleValue,arrSettings[intField][0],arrSettings[intField][2])) return(false);
						break;
					case "lengthRange":
						intMinLength=strRuleValue.split("~")[0];
						intMaxLength=strRuleValue.split("~")[1];
						if (!verifyLength(intMinLength,intMaxLength,arrSettings[intField][0],arrSettings[intField][2])) return(false);
						break;
					case "lengthMin":
						if (!verifyLength(strRuleValue,"",arrSettings[intField][0],arrSettings[intField][2])) return(false);
						break;
					case "lengthMax":
						if (!verifyLength("",strRuleValue,arrSettings[intField][0],arrSettings[intField][2])) return(false);
						break;
					default:
						alert("Warning: rule " + strRuleName + " of unknown type");
          case "compare":
           if (getFieldValue(eval(strRuleValue))!=getFieldValue(arrSettings[intField][0]))
            {
              alert("Por favor, o campo E-mail e confirmação do E-mail devem conter os mesmos dados.");
              arrSettings[intField][0].focus();
              return(false);
            }
						break;
					//default:
					//alert("Warning: rule " + strRuleName + " of unknown type");
				}
			}
			//Done with the rules, now check datatypes
			if (!verifyDataType(arrSettings[intField][0],arrSettings[intField][2])) return(false);
	} //Fields loop
	return(true);
}

function verifyDataType(objField,strLabel) {
	strDataType=getFieldType(objField);
	strFieldValue=getFieldValue(objField);
	switch (strDataType) {
		case "bte":
			if (isNaN(strFieldValue)) {
				alert("O campo " + strLabel + " deve conter um valor numérico");
				objField.focus();
				return(false);
			}
			break;
		case "int":
			if (isNaN(strFieldValue)) {
				alert("O campo " + strLabel + " deve conter um valor numérico");
				objField.focus();
				return(false);
			}
			break;
		case "lng":
			if (isNaN(strFieldValue)) {
				alert("O campo " + strLabel + " deve conter um valor numérico");
				objField.focus();
				return(false);
			}
			break;
		case "dbl":
			if (isNaN(strFieldValue.replace(",","."))) {
				alert("O campo " + strLabel + " deve conter um valor numérico");
				objField.focus();
				return(false);
			}
			break;
		case "cur":
			strDblValue=strFieldValue.replace(".","");
			strDblValue=strDblValue.replace(",",".");
			if (isNaN(strDblValue)) {
				alert("O campo " + strLabel + " deve conter um valor numérico (formato aceito: 88,88)");
				objField.focus();
				return(false);
			}
			break;
		case "dat":
			if (!verifyDate(objField)) return(false);
			break;
		default:
	}
	return(true);
}

function verifyLength(intMinLength,intMaxLength,objField,strLabel) { //Check the size of a field's value
	strFieldValue=getFieldValue(objField);
	intLength=strFieldValue.length;
	if (intMinLength=="" || intMinLength==null) { //Just max
		if (intLength>intMaxLength) {
			alert("O campo " + strLabel + " pode conter no máximo " + intMaxLength + " caracteres");
			objField.focus();
			return(false);
		}
	}
	else if (intMaxLength=="" || intMaxLength==null) { //Just min
		if (intLength<intMinLength) {
			alert("O campo " + strLabel + " deve conter no mínimo " + intMinLength + " caracteres");
			objField.focus();
			return(false);
		}
	}
	else if (intMinLength==intMaxLength) { //Fixed value
		if (intLength!=intMinLength) {
			alert("O campo " + strLabel + " deve conter " + intMinLength + " caracteres");
			objField.focus();
			return(false);
		}
	}
	else { //Range
		if (strValue.length<intMinLength || strValue.length>intMaxLength) {
			alert("O campo " + strLabel + " deve conter no mínimo " + intMinLength + " e no máximo " + intMaxLength + " caracteres");
			objField.focus();
			return(false);
		}
	}	
	return(true);
}

function getFieldValue(objField) { //Just cause combos don't have a value in Netscape
	//alert(objField)
	if (objField.type=="select-one") return (objField.options[objField.selectedIndex].value);
	else return(objField.value);
}

function getFieldType(objField) { //Just return the three first characters of the field's name
	return(objField.name.substr(0,3));
}

function getFieldName(objField) { //Just return the three first characters of the field's name
	return(objField.name);
}

function maskField(objField) { //Avoid invalid chars from being typed on a field (IE only)
	intKeyCode=event.keyCode;
	strTypedChar=String.fromCharCode(intKeyCode);
	if (!verifyField(strTypedChar))	return(false);
	return(true);
}

//objField --> form field to be validated;
//strFileTypes --> acceptable types of file separeted by pipes ('gif|jpg|bmp').
function checkFileType(objField,strFileTypes) {
	var strValue = objField.value;
	var strSuffix = strValue.substr(strValue.lastIndexOf('.')+1);
	var arrFileTypes = strFileTypes.split('|');
	var intField;
	var intResult = 0;
	for (intField=0;intField<arrFileTypes.length;intField++) {
		if(strSuffix!=arrFileTypes[intField]) {
			intResult++;
		}
	}
	if(intResult==arrFileTypes.length) {
		alert("Tipo de arquivo inválido!");
		objField.focus();
		return(false);
	} else {
		return(true);
	}
}

function verifyMail(strEmail,strErrorMsg) {
	if (strEmail=='') return(true);
	if (!strErrorMsg) strErrorMsg="E-mail inválido"; //Use the default error message if nothing is given
	strValidChars="@abcdefghijklmnopqrstuvxywzABCDEFGHIJKLMNOPQRSTUVXYWZ0123456789-_."; //Characters that an email can contain
	arrBoundaryChars=new Array();
	for (intPos=0;intPos<strEmail.length;intPos++) { //Check if all characters are valid
		strThisChar=strEmail.substr(i,1);
		if (strValidChars.indexOf(strThisChar)==-1) {
			alert(strErrorMsg);
			return(false);
		}
		if (strThisChar=="@" || strThisChar==".") { //Feed the BoundaryChars array
			arrBoundaryChars[arrBoundaryChars.length]=parseInt(intPos);
		}		
	}
	for (intPos=0;intPos<arrBoundaryChars.length;intPos++) { //Check if dots and ats are not in conflict
		intThisItem=arrBoundaryChars[i]
		intNextItem=(i==arrBoundaryChars.length) ? 0 : arrBoundaryChars[i+1]
		if (intThisItem==0 || intThisItem==strEmail.length) {
			alert(strErrorMsg);
			return(false);
		}
		if (intThisItem+1==intNextItem) {
			alert(strErrorMsg);
			return(false);
		}
	}
		
	intFirstAtIndex=strEmail.indexOf("@");
	intLastAtIndex=strEmail.lastIndexOf("@");
	intFirstDotIndex=strEmail.indexOf(".");
	intLastDotIndex=strEmail.lastIndexOf(".");
	
	if (intFirstAtIndex!=intLastAtIndex || intFirstAtIndex==-1 || intFirstDotIndex==-1 || intLastDotIndex<intFirstDotIndex) {
		alert(strErrorMsg);
		return(false);
	}
	return(true); //If it got here, it's all ok (I hope)
}

function verifyDate(objField) {
	strDate=getFieldValue(objField);
	if (strDate.indexOf("/")==strDate.lastIndexOf("/")){ // se houver menos que duas barras caia fora
		//alert("data inválida")
		return(false)
	}
	day=strDate.substr(0,strDate.indexOf("/"))
	month=strDate.substr(strDate.indexOf("/")+1,strDate.lastIndexOf("/")-strDate.indexOf("/")-1)
	year=strDate.substr(strDate.lastIndexOf("/")+1,strDate.length-strDate.lastIndexOf("/")-1)
	if (isNaN(day) || isNaN(month) || isNaN(year)) {
		//alert("data inválida. formato aceito: dd/mm/aaaa")
		return(false)
	}
	if (day<1 || day>31 || month<1 || month>12 || year<1800 || year>2200) {
		//alert("data inválida")
		return(false)
	}
	return(true)
}

//To do: re-write the date validation and add more rules

function trim(str) {
	var startIndex,endIndex,returnValue
	for (i=0;i<str.length;i++) {
		atChar=str.substring(i,1)
		if (atChar!=" " && atChar!="\n" && atChar!="\r" && atChar!="\t") {
			startIndex=i
			break
		}
	}
	for (i=str.length-1;i>=0;i--) {
		atChar=str.substring(i,1)
		if (atChar!=" " && atChar!="\n" && atChar!="\r" && atChar!="\t") {
			endIndex=i
			break
		}
	}
	return(str.substring(startIndex,endIndex+1))
}




