/*
------------------------------------------------------------------------------------------
© Digital Industry - 2008
Autor:		Rafael Cronemberger de Assis
Cliente: 	Esser
------------------------------------------------------------------------------------------
*/

// Global Variables

var nmOpacity 			= 100;
var stNs4 				= (navigator.appName == 'Netscape')?1:0;
var stNn 				= (navigator.appName.indexOf('Netscape')!=-1);

function ShowModal(stPage){
	var args 		= new Object;
	args.window 	= window;
	showModalDialog(stPage, args);
}

function DynamicSimpleValidate(objForm){
	var objElements 	= objForm.elements
	var nmElements 		= objForm.elements.length
	var objField;

	for(var nmInc = 0; nmInc < nmElements; nmInc++){
		try{
			var objField	= objElements[nmInc]
			var boValidate	= new Boolean(objField.getAttribute('boValidate'))
			var stMsgError	= new String(objField.getAttribute('stMsgError'))
			var stFieldType	= new String(objField.getAttribute('stFieldType'))
			var stValue		= new String(objField.value)
		}
		
		catch(stError){
			//alert(stError)
		}

		/* Validação Simples */
		if(stFieldType == 'null' || stFieldType == ''){
			if(boValidate == true && stValue == ''){
				alert(stMsgError);
				objField.focus();
				return false;
			}
			
		/* Date */
		}else if(stFieldType == 'date'){
			if(CheckDate(stValue) == false){
				alert(stMsgError);
				objField.focus();
				return false;
			}
			
		/* E-mail */
		}else if(stFieldType == 'email'){
			if(validateRegexEmail(stValue) == false){
				alert(stMsgError);
				objField.focus();
				return false;
			}

		/* Numbers */
		}else if(stFieldType == 'number'){
			if(isNaN(stValue)){
				alert(stMsgError);
				objField.focus();
				return false;
			}
		}
	}
}

function Redirect(stDestino){
	document.location.href=(stDestino)
}

function openCenterPopup(stUrl, stWindowName, stParameters, nmWidth, nmHeight){
	stWindowParameters 	= ', width='+nmWidth+', height='+nmHeight+'';
	stParameters		= stParameters += stWindowParameters;
	objPopUp 			= window.open(stUrl, stWindowName, stParameters)

	try{
		CenterObject(objPopUp, nmWidth, nmHeight)
	}catch(ex){}
	
	objPopUp.focus();
}

function CenterObject(objElement, nmWidth, nmHeight){
	try{
		/* Popups */
		var nmX	= (screen.width - nmWidth) / 2;
		var	nmY = (screen.height - nmHeight) / 2;

		objElement.moveTo(nmX, nmY)
	}
	catch(ex){
		/* Elements */
		var nmLeft	= (screen.width - nmWidth) / 2;
		var	nmTop 	= ((screen.height - nmHeight) / 2) + (parseInt(document.documentElement.scrollTop) / 2);
		//var	nmTop 	= (	parseInt(document.body.scrollTop)	+	parseInt(document.body.offsetHeight / 2) );
		objElement.style.left 	= nmLeft;
		objElement.style.top 	= nmTop;
	}
}

function MascaraMoeda(objTextBox, stSeparadorMilesimo, stSeparadorDecimal, e){
	var nmSep 		= 0;
	var stKey 		= '';
	var 			i = j = 0;
	var nmLen 		= nmLen2 = 0;
	var stCheck 	= '0123456789';
	var stAuxiliar 	= stAuxiliar2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if(whichCode == 13) return true;
	stKey = String.fromCharCode(whichCode);

	if(stCheck.indexOf(stKey) == -1) return false;
	nmLen = objTextBox.value.length;
	
	for(i = 0; i < nmLen; i++)
		if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != stSeparadorDecimal)) break;

	stAuxiliar = '';

	for(; i < nmLen; i++)
		if (stCheck.indexOf(objTextBox.value.charAt(i))!=-1) stAuxiliar += objTextBox.value.charAt(i);
	
	stAuxiliar 	+= stKey;
	nmLen 		= stAuxiliar.length;

	if (nmLen == 0) objTextBox.value = '';
	if (nmLen == 1) objTextBox.value = '0'+ stSeparadorDecimal + '0' + stAuxiliar;
	if (nmLen == 2) objTextBox.value = '0'+ stSeparadorDecimal + stAuxiliar;
	if (nmLen > 2){
	stAuxiliar2 = '';

	for (j = 0, i = nmLen - 3; i >= 0; i--){
		if (j == 3){
			stAuxiliar2 += stSeparadorMilesimo;
			j = 0;
		}
		stAuxiliar2 += stAuxiliar.charAt(i);
		j++;
	}

	objTextBox.value	=	'';

	nmLen2 = stAuxiliar2.length;

	for (i = nmLen2 - 1; i >= 0; i--)
		objTextBox.value += stAuxiliar2.charAt(i);
		objTextBox.value += stSeparadorDecimal + stAuxiliar.substr(nmLen - 2, nmLen);
	}
	return false;
}

function ResetDropDown(objDropDown){
	objDropDown.options.length = 0;
}

function Timer(nmSeconds, stAction){
	window.setTimeout(stAction, nmSeconds);	
}

function ShowHideElement(objElement, fgDisplay){
	if(fgDisplay == 1)
		objElement.style.display = ''
	else
		objElement.style.display = 'none'
}

function BlockUnblockElement(objElement, fgBlock){
	if(fgBlock == 1)
		objElement.disabled = false
	else
		objElement.disabled = true
}

function validateRegexEmail(stEmail){
	if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(stEmail)){
		return false;
	}
}

function displayFlash(nmWidth, nmHeight, stSwf){
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+nmWidth+'" height="'+nmHeight+'" id="objFlash" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" /><param name="wmode" value="transparent" />');
	document.write('<param name="movie" value="'+stSwf+'" /><param name="quality" value="best" /><param name="bgcolor" value="#ffffff" /><embed src="'+stSwf+'" wmode="transparent" quality="best" width="'+nmWidth+'" height="'+nmHeight+'" name="div" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash"/>');
	document.write('</object>');
}

function FieldOnFocus(objForm, stFieldValue){
	if(eval(objForm).value == stFieldValue){
		eval(objForm).value = '';
	}
}

function FieldOnBlur(objForm, stFieldValue){
	if(eval(objForm).value == ''){
		eval(objForm).value = stFieldValue;
	}
}

function Ltrim(stValue) {
	var re = /\s*((\S+\s*)*)/;
	return stValue.replace(re, '$1');
}

function Rtrim(stValue) {
	var re = /((\s*\S+)*)\s*/;
	return stValue.replace(re, '$1');
}

function Trim(stValue){
	stValue = Ltrim(Rtrim(stValue));
	return stValue.replace(/^\s+|\s+$/g, '');
}

function FadeElement(idElement, stMethod, nmVelocity){
	var objElement	= document.getElementById(idElement)
	if(objElement){
		if(nmOpacity == 100 && stMethod == 'in') nmOpacity = 10; fgContinue	= true; ShowHideElement(objElement, 1);
	
		if(nmOpacity > 0){
			if(stMethod == 'in'){
				fgDisplay = 	1;
				nmOpacity += 	10;
			}else{
				fgDisplay 	= 	0;
				nmOpacity -= 	10
			}
			fgContinue	= true;
		}else{
			fgContinue	= false;
		}
		
		if(nmOpacity == 100 && stMethod == 'in') fgContinue = false;
	
		if(fgContinue == true){
			objElement.style.filter = 'alpha(style=0,opacity=' + nmOpacity + ')';
			objTimer  				= setTimeout ("FadeElement('"+idElement+"', '"+stMethod+"', "+nmVelocity+");", nmVelocity);
		}else{
			ShowHideElement(objElement, fgDisplay)
			nmOpacity 				= 100;
			objElement.style.filter = 'alpha(style=0,opacity=' + nmOpacity + ')';
		}
	}
}

function BlockChars(){
	if(!stNs4){
		if(event.keyCode < 45 || event.keyCode > 57) event.returnValue = false;
	}else{
		if(event.which < 45 || event.which > 57) returnfalse;
	}
}

function BlockSpecialChars(){
	if(!stNs4){
		if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;
	}else{
		if((event.which > 32 && event.which < 48) || (event.which > 57 && event.which < 65) || (event.which > 90 && event.which < 97)) return false;
	}
}

function CheckDate(stDate){
	var stCheck 		= '0123456789';
	var stDateTemp 		= '';
	var stSeparator 	= '/';
	var nmDay;
	var nmMonth;
	var nmYear;
	var nmLoop 			= 0;
	var nmError 		= 0;
	var nmInc;

	for (nmInc = 0; nmInc < stDate.length; nmInc++) {
		if (stCheck.indexOf(stDate.substr(nmInc,1)) >= 0) {
		stDateTemp = stDateTemp + stDate.substr(nmInc,1);
		}
	}

	stDate = stDateTemp;

	if (stDate.length == 6)					stDate = stDate.substr(0,4) + '20' + stDate.substr(4,2);
	nmYear 	= stDate.substr(4,4);
	if (nmYear == 0) 						nmError = 1;
	nmMonth = stDate.substr(2,2);
	if ((nmMonth < 1) || (nmMonth > 12)) 	nmError = 1;
	nmDay 	= stDate.substr(0,2);
	if (nmDay < 1) 							nmError = 1;
	if ((nmYear % 4 == 0) || (nmYear % 100 == 0) || (nmYear % 400 == 0)) 	nmLoop = 1;
	if ((nmMonth == 2) && (nmLoop == 1) && (nmDay > 29)) 					nmError = 1;
	if ((nmMonth == 2) && (nmLoop != 1) && (nmDay > 28)) 					nmError = 1;
	if ((nmDay > 31) && ((nmMonth == '01') || (nmMonth == '03') || (nmMonth == '05') || (nmMonth == '07') || (nmMonth == '08') || (nmMonth == '10') || (nmMonth == '12'))) nmError = 1;
	if ((nmDay > 30) && ((nmMonth == '04') || (nmMonth == '06') || (nmMonth == '09') || (nmMonth == '11'))) nmError = 1;
	if ((nmDay == 0) && (nmMonth == 0) && (nmYear == 00)) nmError = 0; nmDay = ''; nmMonth = ''; nmYear = ''; stSeparator = '';
	if (stDate.length != 8) nmError = 1;

	if (nmError == 0) {
		return true;
	}else{
		return false;
	}
}

function mascaraData(objField){
	var stDate = objField.value;
	if (stDate.length == 2){
		stDate = stDate + '/';
		objField.value = stDate;
		return true;
	}
	if (stDate.length == 5){
		stDate = stDate + '/';
		objField.value = stDate;
	return true;
	}
}

function resetForm(objForm){
	objForm.reset();
}