//
//	© 2009 InWise Internet Company do Brasil S.A. - Todos os direitos reservados
//	www.inwise.com.br
//

aPageRollovers[ aPageRollovers.length ] = "/sources/images/buttons/next-over.gif";
aPageRollovers[ aPageRollovers.length ] = "/sources/images/buttons/next-down.gif";
aPageRollovers[ aPageRollovers.length ] = "/sources/images/buttons/previous-over.gif";
aPageRollovers[ aPageRollovers.length ] = "/sources/images/buttons/previous-down.gif";
aPageRollovers[ aPageRollovers.length ] = "/sources/images/buttons/cancel-over.gif";
aPageRollovers[ aPageRollovers.length ] = "/sources/images/buttons/cancel-down.gif";

var FORM_SENDMAIL = 1;
var FORM_ORDERPRODUCT = 2;
var FORM_ORDERCUSTOMIZATION = 3;
var FORM_DOWNLOADTRIAL = 4;

var aPageFramesForm = { Download: FORM_DOWNLOADTRIAL, DownloadSoftphoneFrame: FORM_DOWNLOADTRIAL, DownloadPenphoneFrame: FORM_DOWNLOADTRIAL, OrderProduct: FORM_ORDERPRODUCT, OrderSoftphoneFrame: FORM_ORDERPRODUCT, OrderSoftphoneIFrame: FORM_ORDERPRODUCT, OrderPenphoneFrame: FORM_ORDERPRODUCT, OrderPenphoneIFrame: FORM_ORDERPRODUCT, OrderPrivateLabelCustomizationFrame: FORM_ORDERCUSTOMIZATION, OrderCustomizationFrame: FORM_ORDERCUSTOMIZATION, SendMail: FORM_SENDMAIL }
var iUpdateFormInterval = null;

function OnShowForm( sFrameName, sVisibility, bParentNavigate ) {

	// Reseta dados do formulário
	if ( sVisibility == "hidden" ) {

		var iForm = aPageFramesForm[ sFrameName ];
		ResetFormFields( iForm, sFrameName );

		if ( iForm == FORM_ORDERPRODUCT ) {

			if ( iWizardIndex > 0 ) {

				if ( bParentNavigate != false )
					parent.WizardNavigateFrame( window.name, -1 );

			}
		}
	}

	return true;

}

function DoSubmitForm( hButton, hForm, iType ) {

	var bReturn = false;
	if ( hButton != null )
		UpdateButtonEnabled( hButton, false );

	switch ( iType ) {

		// Formulário de download?
		case FORM_DOWNLOADTRIAL:

			var aErrorMessage = [];

			// Valida o nome
			var sName = hForm.Name.value;
			if (( sName == "" ) || ( sName.length < 3 ))
				aErrorMessage.push( "\"Nome\" com pelo menos 3 caracteres" );

			var sFrom = hForm.EMail.value; 
			if (( sFrom == "" ) || ( !isValidEMail( sFrom )))
				aErrorMessage.push( "\"E-mail\" com um e-mail válido" );

			// Valida e-mail de confirmação
			if (( hForm.EMailConfirm.value == "" ) || ( hForm.EMailConfirm.value != sFrom ))
				aErrorMessage.push( "\"Confirmar e-mail\" com o mesmo valor do campo  \"E-mail\"" );

			if ( aErrorMessage.length > 0 ) {

				var sMessage = "Formulário com dados incompletos!\nPor favor, preencha " + ((aErrorMessage.length == 1)? "o campo:\n" : "os campos:\n" );
				for ( var i = 0; i < aErrorMessage.length; i++ )
					sMessage += "\n      " + aErrorMessage[i] + ";";

				alert( sMessage + "\n\nE tente novamente." );

			} else	
				bReturn = true;

			break;

		// Formulário de envio de e-mail?
		case FORM_SENDMAIL:

			// Valida o nome
			var sName = hForm.Name.value;
			if ( sName.length < 3 ) {

				alert( "Nome inválido.\n\nPor favor, preencha o campo \"Nome\" com um nome válido e tente novamente.\n" );
				hForm.Name.focus();
				break;

			}

			var sFrom = hForm.From.value; 
			if ( !isValidEMail( sFrom )) {

				alert( "E-mail inválido.\n\nPor favor, preencha o campo \"E-mail\" com um e-mail válido e tente novamente.\n" );
				hForm.From.focus();
				break;

			}

			var sBody = hForm.Message.value;
			if ( sBody.length < 3 ) {

				alert( "Mensagem inválida.\n\nPor favor, preencha o campo \"Mensagem\" com uma mensagem válida e tente novamente.\n" );
				hForm.Body.focus();
				break;

			}

			hForm.Subject.value = "InWise Homepage - Mensagem do usuário: " + sName;

			var sBody = "IDENTIFICAÇÃO \r\n\r\n";
			sBody += "\t Nome: " + sName + "\r\n" +
					 "\t E-mail: " + sFrom + "\r\n" +
					 "\t Nome da empresa: " + hForm.Company.value + "\r\n";

			hForm.Body.value = sBody + "\r\n" + hForm.Message.value;
			hForm.submit();
			bReturn = true;
			break;

		// Formulário de compra?
		case FORM_ORDERPRODUCT:

			// Pessoa jurídica?
			if ( hForm.CustomerType_Coorporation.checked ) {

				// Valida o nome do responsável
				var sName = hForm.ResponsableFullName.value;
				if ( sName.length < 3 ) {

					alert( "Nome do responsável inválido.\n\nPor favor, preencha o campo \"Nome do responsável\" e tente novamente.\n" );
					hForm.ResponsableFullName.focus();
					break;

				}

				// Valida CPF
				if ( !isValidCNPJ( hForm.CNPJ.value )) {

					alert( "CNPJ inválido.\n\nPor favor, informe um CNPJ válido no campo \"CNPJ\" e tente novamente.\n" );
					hForm.CNPJ.focus();
					break;

				}

			// Pessoa física?
			} else {

				// Valida o nome
				var sName = hForm.FullName.value;
				if ( sName.length < 3 ) {

					alert( "Nome inválido.\n\nPor favor, preencha o campo \"Nome completo\" e tente novamente.\n" );
					hForm.FullName.focus();
					break;

				}

				// Valida CPF
				if ( !isValidCPF( hForm.CPF.value )) {

					alert( "CPF inválido.\n\nPor favor, informe um CPF válido no campo \"CPF\" e tente novamente.\n" );
					hForm.CPF.focus();
					break;

				}
			}

			// Valida o e-mail
			var sFrom = hForm.From.value; 
			if ( !isValidEMail( sFrom )) {

				alert( "E-mail inválido.\n\nPor favor, preencha o campo \"E-mail\" com um e-mail válido e tente novamente.\n" );
				hForm.From.focus();
				break;

			}

			// Valida e-mail de confirmação
			if ( hForm.EmailConfirm.value != sFrom ) {

				alert( "E-mail de confirmação incorreto.\n\nPor favor, preencha o campo \"Confirmação do e-mail\" com o mesmo valor do campo \"E-mail\" e tente novamente.\n" );
				hForm.EmailConfirm.focus();
				break;

			}

			// Valida o nome da empresa
			var sCompanyName = hForm.CompanyName.value;
			if ( sCompanyName.length < 3 ) {

				alert( "Nome da empresa inválido.\n\nPor favor, preencha o campo \"Empresa\" e tente novamente.\n" );
				hForm.CompanyName.focus();
				break;

			}

			// Valida seleção de produto
			if ( !hForm.ProductSoftphone.checked && !hForm.ProductPenphone.checked ) {

				alert( "Produto não selecionado.\n\nPor favor, selecione um produto e tente novamente.\n" );
				hForm.ProductSoftphone.focus();
				break;

			}

			// Valida quantidade de produto
			if ( hForm.ProductSoftphone.checked && ( hForm.SoftphoneCount.value == "" )) {

				alert( "Quantidade de Softphone não informada.\n\nPor favor, informe a quantidade de Softphone e tente novamente.\n" );
				hForm.ProductSoftphone.focus();
				break;

			}

			// Valida quantidade de produto
			if ( hForm.ProductPenphone.checked && ( hForm.PenphoneCount.value == "" )) {

				alert( "Quantidade de Penphone não informada.\n\nPor favor, informe a quantidade de Penphone e tente novamente.\n" );
				hForm.ProductPenphone.focus();
				break;

			}

			hForm.Subject.value = "InWise Homepage - Solicitação de compra";

			var sBody = "IDENTIFICAÇÃO \r\n\r\n";

			if ( hForm.CustomerType_Coorporation.checked ) {

				sBody += "\t Nome da empresa: " + sCompanyName + "\r\n" +
						 "\t CNPJ: " + hForm.CNPJ.value + "\r\n" +
						 "\t Nome do responsável: " + sName + "\r\n";

			} else {

				sBody += "\t Nome: " + sName + "\r\n" +
						 "\t E-mail: " + sFrom + "\r\n" +
						 "\t CPF: " + hForm.CPF.value + "\r\n" +
						 "\t Nome da empresa: " + sCompanyName + "\r\n";

			}

			sBody += "\t Telefone fixo: " + hForm.PhoneDDD.value + " " + hForm.PhoneNumber.value + "\r\n" +
				"\t Telefone celular: " + hForm.MobileDDD.value + " " + hForm.MonbileNumber.value + "\r\n" +
				"\t Área de atividade: " + GetSelectValue( hForm.ActivityArea ) + "\r\n" +
				"\t Gostaria de receber notícias por e-mail: " + GetCheckboxFriendlyValue( hForm.SendNews, "Sim", "Não" ) + "\r\n" +
				"\r\n" +
				"COMPRA \r\n" +
				"\r\n" +

				GetCheckboxFriendlyValue( hForm.ProductSoftphone, "\t InWise Softphone 2.0: " + hForm.SoftphoneCount.value + "\r\n", "" ) +
				GetCheckboxFriendlyValue( hForm.ProductPenphone, "\t InWise Penphone 2.0: " + hForm.PenphoneCount.value + "\r\n", "" ) +

				"\r\n" +
				"\t Valor total: " + CalculatePrice( hForm.ProductSoftphone.checked? parseInt( hForm.SoftphoneCount.value ) : 0, hForm.ProductPenphone.checked? parseInt( hForm.PenphoneCount.value ) : 0 ) + "\r\n" +
				"\r\n" +
				"CONSIDERAÇÕES ADICIONAIS\r\n" + 
				"\r\n" +
				"\t " + hForm.AdditionalConsiderations.value + "\r\n";

			
			hForm.Body.value = sBody;

			bReturn = true;
			break;

		// Formulário de solicitação de customização?
		case FORM_ORDERCUSTOMIZATION:

			var sCompanyName = hForm.CompanyName.value;
			if ( sCompanyName.length < 3 ) {

				alert( "Nome da empresa inválido.\n\nPor favor, preencha o campo \"Empresa\" e tente novamente.\n" );
				hForm.CompanyName.focus();
				break;

			}

			var sName = hForm.ResponsibleName.value;
			if ( sName.length < 3 ) {

				alert( "Nome inválido.\n\nPor favor, preencha o campo \"Nome do responsável\" e tente novamente.\n" );
				hForm.ResponsibleName.focus();
				break;

			}

			// Valida o e-mail
			var sFrom = hForm.From.value; 
			if ( !isValidEMail( sFrom )) {

				alert( "E-mail inválido.\n\nPor favor, preencha o campo \"E-mail do responsável\" com um e-mail válido e tente novamente.\n" );
				hForm.From.focus();
				break;

			}

			// Valida e-mail de confirmação
			if ( hForm.EmailConfirm.value != sFrom ) {

				alert( "E-mail de confirmação incorreto.\n\nPor favor, preencha o campo \"Confirmar e-mail\" com o mesmo valor do campo \"E-mail do responsável\" e tente novamente.\n" );
				hForm.EmailConfirm.focus();
				break;

			}

			// Valida seleção de produto
			if ( !hForm.ProductSoftphone.checked && !hForm.ProductPenphone.checked ) {

				alert( "Produto não selecionado.\n\nPor favor, selecione um produto e tente novamente.\n" );
				hForm.ProductSoftphone.focus();
				break;

			}

			// Valida quantidade de produto
			if ( hForm.ProductSoftphone.checked && ( hForm.SoftphoneCount.value == "" )) {

				alert( "Quantidade de Softphone não informada.\n\nPor favor, informe a quantidade de Softphone e tente novamente.\n" );
				hForm.ProductSoftphone.focus();
				break;

			}

			// Valida quantidade de produto
			if ( hForm.ProductPenphone.checked && ( hForm.PenphoneCount.value == "" )) {

				alert( "Quantidade de Penphone não informada.\n\nPor favor, informe a quantidade de Penphone e tente novamente.\n" );
				hForm.ProductPenphone.focus();
				break;

			}

			// Valida tipo de customização
			if ( !hForm.CustomizationType_PrivateLabel.checked && !hForm.CustomizationType_Shared.checked && !hForm.CustomizationType_CoBranded.checked ) {

				alert( "Tipo de customização não selecionada.\n\nPor favor, selecione um tipo de customização e tente novamente.\n" );
				hForm.CustomizationType_PrivateLabel.focus();
				break;

			}

			if ( isNaN( parseInt( hForm.SoftphoneCount.value )))
				hForm.SoftphoneCount.value = "0";

			if ( isNaN( parseInt( hForm.PenphoneCount.value )))
				hForm.SoftphoneCount.value = "0";

			hForm.Subject.value = "InWise Homepage - Solicitação de proposta";

			hForm.Body.value = "IDENTIFICAÇÃO \r\n" +

				"\r\n" +
				"\t Empresa: " + sCompanyName + "\r\n" +
				"\t Responsável: " + sName + "\r\n" +
				"\t E-mail: " + sFrom + "\r\n" +
				"\t Telefone fixo: " + hForm.DDD.value + " " + hForm.PhoneNumber.value + "\r\n" +
				"\t Telefone celular: " + hForm.MobileDDD.value + " " + hForm.MonbileNumber.value + "\r\n" +
				"\t Número de funcionários: " + GetSelectValue( hForm.Employees ) + "\r\n" +
				"\t Área de atividade: " + GetSelectValue( hForm.ActivityArea ) + "\r\n" +
				"\t Como conheceu a InWise: " + GetSelectValue( hForm.HowMet ) + " " + hForm.HowMetComplement.value + "\r\n" +
				"\t Gostaria de receber notícias por e-mail: " + GetCheckboxFriendlyValue( hForm.SendNews, "Sim", "Não" ) + "\r\n" +
				"\r\n" +
				"CUSTOMIZAÇÃO \r\n" +
				"\r\n" +
				"\t Tipo: " + GetRadioValue( hForm.CustomizationType, "" ) + "\r\n" +
				"\r\n" +

				GetCheckboxFriendlyValue( hForm.ProductSoftphone, "\t InWise Softphone 2.0: " + hForm.SoftphoneCount.value + "\r\n", "" ) +
				GetCheckboxFriendlyValue( hForm.ProductPenphone, "\t InWise Penphone 2.0: " + hForm.PenphoneCount.value + "\r\n", "" ) +

				"\r\n" +
				"CONSIDERAÇÕES ADICIONAIS\r\n" + 
				"\r\n" +
				"\t " + hForm.AdditionalConsiderations.value + "\r\n";

			bReturn = true;

			// Depuração
			//alert( hForm.Body.value );
			//bReturn = false;
			break;
	
	}

	if ( bReturn ) {

		var sFrom = document.location.href;
		var iStart = sFrom.indexOf( "?" );
		if ( iStart == -1 )
			sFrom = sFrom.substr( sFrom.indexOf( "//" ) + 2 );
		else
			sFrom = sFrom.substring( sFrom.indexOf( "//" ) + 2, iStart );

		hForm.SuccessRedirect.value += "?from=" + sFrom;
		hForm.ErrorRedirect.value += "?from=" + sFrom;

		hForm.submit();

		if ( iType == FORM_DOWNLOADTRIAL ) {

			if ( hButton != null )
				UpdateButtonEnabled( hButton, true );

		}

	} else {

		if ( hButton != null )
			UpdateButtonEnabled( hButton, true );

	}

	return bReturn;

}

function ResetFormFields( iType, sFrameName ) {

	switch ( iType ) {

		// Formulário de contato?
		case FORM_SENDMAIL:

			var hForm = document.getElementById( "SendMail" );
			hForm.Name.value = "";
			hForm.Company.value = "";
			hForm.From.value = "";
			hForm.Body.value = "";
			hForm.Subject.value = "";
			UpdateButtonEnabled( document.getElementById( "FormImgSend" ), true );
			break;

		// Formulário de compra?
		case FORM_ORDERPRODUCT:

			var hForm = document.getElementById( "OrderProductForm" );
			hForm.FullName.value = "";
			hForm.ResponsableFullName.value = "";
			hForm.From.value = ""; 
			hForm.EmailConfirm.value = "";
			hForm.CPF.value = "";
			hForm.CNPJ.value = "";
			hForm.CompanyName.value = "";
			hForm.Subject.value = "";
			hForm.Body.value = "";
			hForm.ProductSoftphone.checked = ( sFrameName == "OrderSoftphoneFrame" );
			document.getElementById( "SoftphoneCountRow" ).style.display = (hForm.ProductSoftphone.checked)? "inline" : "none";
			hForm.ProductPenphone.checked = !hForm.ProductSoftphone.checked;
			document.getElementById( "PenphoneCountRow" ).style.display = (hForm.ProductPenphone.checked)? "inline" : "none";
			hForm.SendNews.checked = false;
			hForm.PhoneDDD.value = "";
			hForm.PhoneNumber.value = "";
			hForm.MobileDDD.value = "";
			hForm.MonbileNumber.value = "";
			hForm.PenphoneCount.value = "";
			hForm.SoftphoneCount.value = "";
			hForm.AdditionalConsiderations.value = "";
			hForm.ActivityArea.selectedIndex = 0;
			UpdateButtonEnabled( document.getElementById( "SendFormImg" ), true );
			UpdatePrice();
			SwitchCustomerType( 'individual' );

			break;

		// Formulário de solicitação de customização?
		case FORM_ORDERCUSTOMIZATION:

			var hForm = document.getElementById( "OrderCustomizationForm" );
			hForm.CompanyName.value = "";
			hForm.ResponsibleName.value = "";
			hForm.From.value = "";
			hForm.EmailConfirm.value = "";
			hForm.Subject.value = "";
			hForm.Body.value = "";
			hForm.PhoneNumber.value = "";
			hForm.MonbileNumber.value = "";
			hForm.PenphoneCount.value = "";
			hForm.SoftphoneCount.value = "";
			hForm.AdditionalConsiderations.value = "";
			hForm.Employees.selectedIndex = 0;
			hForm.ActivityArea.selectedIndex = 0;
			hForm.ProductSoftphone.checked = false;
			hForm.ProductPenphone.checked = false;
			hForm.SendNews.checked = false;
			hForm.CustomizationType_PrivateLabel.checked = false;
			hForm.CustomizationType_CoBranded.checked = false;
			hForm.CustomizationType_Shared.checked = false;
			UpdateButtonEnabled( document.getElementById( "SendFormImg" ), true );

			break;

		// Formulário de download
		case FORM_DOWNLOADTRIAL:

			var hForm = document.getElementById( "DownloadForm" );
			hForm.Name.value = "";
			hForm.EMail.value = "";
			hForm.EMailConfirm.value = "";
			hForm.ReceiveNews.checked = false;
			UpdateButtonEnabled( document.getElementById( "DoDownloadImg" ), true );
			break;

	}

	return true;

}


// Testes
/*
var b;
b = isValidEMail( "a@a.a" ); // FAIL: menor que 6 caracteres
// b = isValidEMail( "a@abcd.com" ); // FAIL: usuário menor que 2 caracteres - Testar melhor
b = isValidEMail( "abcd@a.com" ); // FAIL: domínio menor que 2 caracteres
b = isValidEMail( "@abcd.com" ); // FAIL: sem user
b = isValidEMail( "abcdef@" ); // FAIL: sem domínio
b = isValidEMail( ".abcd@abcd.com." ); // FAIL: Ponto no início do usuário
b = isValidEMail( "abcd.@abcd.com." ); // FAIL: Ponto no final do usuário
b = isValidEMail( "abcd@.abcd.com." ); // FAIL: Ponto no início do domínio
b = isValidEMail( "abcd@abcd.com." ); // FAIL: Ponto no final do domínio
b = isValidEMail( "abcd@@a.com.br" ); // FAIL: 2 arrobas
b = isValidEMail( "abcd@abcd.com.br" ); // OK
b = isValidEMail( "abcd.efgh@abcd.com.br" ); // OK
b = isValidEMail( "a.b@ab.com.br" ); // OK
*/
function isValidEMail( sEMail ) {

	// O e-mail tem mais de 6 caracteres?
	if ( sEMail.length < 6 )
		return false;
	
	// Tem arroba?
	var iIndex = sEMail.indexOf( "@" );
	if ( iIndex == -1 )
		return false;
	
	var sUser = sEMail.substr( 0, iIndex );
	if ( sUser == "" )
		return false;
	
	// *** Testar melhor qual o menor tamanho...
	//if ( sUser.length < 3 )
	//	return false;

	// Usuário inicia com ponto?
	if ( sUser.charAt(0) == '.' )
		return false;

	// Usuário finaliza com ponto?
	if ( sUser.charAt( sUser.length - 1 ) == '.' )
		return false;
	
	// Pega o domínio...
	var sDomain = sEMail.substr( iIndex + 1 );
	if ( sDomain == "" )
		return false;

	// Domínio inicia com ponto?
	if ( sDomain.charAt(0) == '.' )
		return false;
	
	// Domínio finaliza com ponto?
	if ( sDomain.charAt( sDomain.length - 1 ) == '.' )
		return false;

	// Tem mais de 1 arroba?
	if ( sDomain.indexOf( "@" ) != -1 )
		return false;
		
	// Tem ponto depois do arroba?
	var iIndex2 = sDomain.indexOf( "." );
	if ( iIndex2 == -1 )
		return false;

	// Domínio menor que 2 caracteres?
	if ( iIndex2 <= 1 )
		return false;
	
	return true;

}

function isValidCPF( sCPF ) {

	if ( sCPF.indexOf( "." ) != -1 )
		sCPF = RemoveChars( sCPF, "." );

	if ( sCPF.indexOf( "-" ) != -1 )
		sCPF = RemoveChars( sCPF, "-" );

	// Invalida CPFs óbvios (?)
	if ( sCPF.length != 11 || sCPF == "00000000000" || sCPF == "11111111111" || sCPF == "22222222222" || sCPF == "33333333333" || sCPF == "44444444444" || sCPF == "55555555555" || sCPF == "66666666666" || sCPF == "77777777777" || sCPF == "88888888888" || sCPF == "99999999999" )
		return false;

	var iAdd = 0;
	for ( var i = 0; i < 9; i++ )
		iAdd += parseInt( sCPF.charAt(i)) * (10 - i);

	var iRev = 11 - ( iAdd % 11 );

	if (iRev == 10 || iRev == 11)
		iRev = 0;

	if (iRev != parseInt( sCPF.charAt(9)))
		return false;

	iAdd = 0;
	for (i = 0; i < 10; i ++)
		iAdd += parseInt( sCPF.charAt(i)) * (11 - i);

	iRev = 11 - ( iAdd % 11);
	if (iRev == 10 || iRev == 11)
		iRev = 0;

	if (iRev != parseInt( sCPF.charAt(10)))
		return false;

	return true;

}

function isValidCNPJ( sCNPJ ) {

	// Opera? permite qualquer coisa...
	if ( navigator.userAgent.indexOf( "Opera" ) != -1 )
		return true;

	if ( sCNPJ.length < 18 ) 
		return false;

	if (( sCNPJ.charAt(2) != ".") || ( sCNPJ.charAt(6) != "." ) || ( sCNPJ.charAt(10) != "/" ) || ( sCNPJ.charAt(15) != "-" ))
		return false;

	sCNPJ = sCNPJ.replace (".","");
	sCNPJ = sCNPJ.replace (".","");
	sCNPJ = sCNPJ.replace ("-","");
	sCNPJ = sCNPJ.replace ("/","");

	var nonNumbers = /\D/;
	if ( nonNumbers.test( sCNPJ ))
		return false;

	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for ( var i = 0; i < 12; i++ ) {

		a[i] = sCNPJ.charAt(i);
		b += a[i] * c[i+1];

	}

	if (( x = b % 11 ) < 2 )
		a[12] = 0;
	else
		a[12] = 11 - x;

	b = 0;

	for ( var y = 0; y < 13; y++ )
		b += ( a[y] * c[y] ); 

	if (( x = b % 11 ) < 2 )
		a[13] = 0; 
	else
		a[13] = 11 - x;

	if (( sCNPJ.charAt(12) != a[12] ) || ( sCNPJ.charAt(13) != a[13] ))
		return false;

	return true;

}

function GetSelectValue( hSelect ) {

	return hSelect.options[ hSelect.selectedIndex ].value;

}

function GetCheckboxFriendlyValue( hCheckbox, sTrue, sFalse ) {

	return ( hCheckbox.checked )? sTrue : sFalse;

}

function GetRadioValue( aRadio, sDefault ) {

	for ( var i = 0; i < aRadio.length; i++ ) {

		if ( aRadio[i].checked )
			return aRadio[i].value;

	}

	return sDefault;

}

function SelectRadioBox( sID ) {

	var hRadio = document.getElementById( sID );
	if ( hRadio != null )
		hRadio.checked = true;

	return true;

}

function SelectCheckBox( sID ) {

	var hCheckbox = document.getElementById( sID );
	if ( hCheckbox != null )
		hCheckbox.checked = true;

	return true;

}

function RemoveChars( sString, sChar ) {

	var sOutput = sString;
	while( sOutput.indexOf( sChar ) > -1 )
		sOutput = sOutput.substring( 0, sOutput.indexOf( sChar )) + sOutput.substr( sOutput.indexOf( sChar ) + sChar.length );

	return sOutput;

}

function FormatMask( hEvent, hField, sMask ) {

	if ( !isIE()) {

		var iKeyCode = GetEventKeyCode( hEvent );

		// Alpha-numérico?
		if (!((
			(( iKeyCode > 47 ) && ( iKeyCode < 58 )) ||
			(( iKeyCode > 64 ) && ( iKeyCode < 91 )) ||
			(( iKeyCode > 96 ) && ( iKeyCode < 123 ))
			))) return true;
	
	}

	var iLength = hField.value.length;
	var sOutput = sMask.substring( 0, 1 );
	var sText = sMask.substring( iLength );

	if ( sText.substring( 0, 1 ) != sOutput )
		hField.value += sText.substring( 0, 1 );

	return true;

}

function OnPasteEditMaskFilter( hEvent, bOnlyNumbers ) {

	if ( isIE()) {

		var sData = clipboardData.getData( "Text" );

		// Não numérico?
		var vChar = parseInt( sData );
		if (( typeof vChar != "number" ) || isNaN( vChar )) {

			hEvent.cancelBubble = true;
			hEvent.returnValue = false;

		}
	}

	return true;

}

function OnKeyPressEditMaskFilter( hEvent, bOnlyNumbers ) {

	var iKeyCode = GetEventKeyCode( hEvent );

	if ( bOnlyNumbers ) {

		// Caractere não numérico?
		var vChar = parseInt( String.fromCharCode( iKeyCode ));
		if (( typeof vChar != "number" ) || isNaN( vChar )) {

			if ( isIE()) {

				hEvent.cancelBubble = true;
				hEvent.returnValue = false;

			} else {

				// Alpha-numérico?
				if ((
					(( iKeyCode > 47 ) && ( iKeyCode < 58 )) ||
					(( iKeyCode > 64 ) && ( iKeyCode < 91 )) ||
					(( iKeyCode > 96 ) && ( iKeyCode < 123 ))
					)) {

					hEvent.stopPropagation();
					hEvent.preventDefault();

				}
			}

			return false;

		}
	}

	return true;

}

function AsyncCancelAdjustEditMinValue( hEdit ) {

	if ( hEdit.ValidationTimeout != null )
		return clearTimeout( hEdit.ValidationTimeout );

	return false;

}

function AsyncAdjustEditMinValue( hEdit, iMinValue, iMaxValue ) {

	hEdit.ValidationTimeout = setTimeout( "AdjustEditMinMaxValue( document.getElementById( '"+ hEdit.id +"' ), "+ iMinValue +", "+ iMaxValue +" );", 750 );
	return hEdit.ValidationTimeout;

}

function AdjustEditMinMaxValue( hEdit, iMinValue, iMaxValue ) {

	var vEditValue = parseInt( hEdit.value );
	if ( !isNaN( vEditValue )) {

		if (( iMinValue != null ) && ( vEditValue < iMinValue ))
			hEdit.value = iMinValue;

		if (( iMaxValue != null ) && ( vEditValue > iMaxValue ))
			hEdit.value = iMaxValue;

	}

	return true;

}

function HandleHowMetChange() {

	if ( GetSelectValue( document.getElementById( "HowMet" )) == "" ) {

		document.getElementById( "HowMetRow" ).style.display = "block";
		document.getElementById( "HowMetComplement" ).focus();

	} else {

		document.getElementById( "HowMetRow" ).style.display = "none";
		document.getElementById( "HowMetComplement" ).value = "";

	}

	return true;

}

function CheckSetFocus( iMaxLength, iActualLength, sNextElement, hEvent ) {

	// Alpha-numérico?
	var iKeyCode = GetEventKeyCode( hEvent );
	if ((
		(( iKeyCode > 47 ) && ( iKeyCode < 58 )) ||
		(( iKeyCode > 64 ) && ( iKeyCode < 91 )) ||
		(( iKeyCode > 96 ) && ( iKeyCode < 123 ))
		)) {
		
		if ( iActualLength == iMaxLength )
			document.getElementById( sNextElement ).focus();

	}

	return true;
	
}

function SwitchCustomerType( sType ) {

	if ( sType.toLowerCase() == 'corporation' ) {

		document.getElementById( 'IdentificationRow_Individual' ).style.display = "none";
		document.getElementById( 'IdentificationRow_Corporation' ).style.display = "block";
		if ( !document.getElementById( 'CustomerType_Coorporation' ).checked )
			document.getElementById( 'CustomerType_Coorporation' ).checked = true;

	} else {

		document.getElementById( 'IdentificationRow_Individual' ).style.display = "block";
		document.getElementById( 'IdentificationRow_Corporation' ).style.display = "none";
		if ( !document.getElementById( 'CustomerType_Individual' ).checked )
			document.getElementById( 'CustomerType_Individual' ).checked = true;

	}

	return true;

}