/*
Devolve um httpobject - usado nos métodos ajax
*/
function GetXmlHttpObject()
{
var xmlHttp1=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp1=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp1;
}//fim de GetXmlHttpObject




/*
Muda o estilo para indicar que o campo precisa de ser preenchido
*/
function ChangeStyle(p_object,p_wrong)
{
	if(p_wrong==true)
	{
		p_object.className ="caixas_border_obrigatorio";	
	}
	else
	{
		p_object.className ="caixas_border";	
	}
}//fim de ChangeStyle


//percorre todos os elementos do form e ter a certeza de que os styles são os normais
function ResetFormStyles()
{
	
//percorrer todos os elementos do form e ter a certeza de que os styles são os normais
for (var i=0; i < document.forms[0].elements.length; i++) 
{
  var element = document.forms[0].elements[i];
 if (typeof element == "object") 
 {
  	if(element.name.substring(0,3)=="txt")
	{
	 ChangeStyle(element,false);
	 }
  }
}//fim do For


}// fim do ResetFormStyles





/*
function para verificar a validade de um dado e-mail
*/
function CheckValidEmail(emailStr)
{

var emailPat=/^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {

	return false
}
var user=matchArray[1]
var domain=matchArray[2]


if (user.match(userPat)==null) {

    return false
}

var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {

	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {

		return false
	    }
    }
    return true
}

var domainArray=domain.match(domainPat)
if (domainArray==null) {

    return false
}


var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {

   return false
}


if (len<2) {

   return false
}

return true

}//fim de CheckValidEmail



/*
onKeyDown="return AllowOnlyNumbers();"
**/
function AllowOnlyNumbers()
{

var i_code = event.keyCode;
	
	/*
	numpad = 96-105
	numeros=48-57
	del=46 
	home=36 
	end=35 
	shift=16
	*/
	
	if (i_code==8 || i_code==13 || i_code==9 || i_code==37 || 
		i_code==39 || i_code==46 || i_code==35 || i_code==36 || i_code==16)
	{
		return true;
	}
	
	if (i_code<48 )
	{
		return false;
	}
	
	if (i_code>57 && i_code<96)
	{
		return false;
	}
	
	if (i_code>105)
	{
		return false;	
	}

	//correu tudo bem
	
	return true;
}//fim de AllowOnlyNumbers




/*****************************
FORMS VALIDATION
******************************/
//	document.getElementById('valida_sms_grupos').getElementsByTagName('textarea').item(0).innerHTML+="#IncluirNome#";
//Valida o formulário no pedido de orçamento - productsquote.php
function CheckQuotesForm()
{
	//document.forms[0].submit();
/*
campos obrigatórios
txtOrder (textarea)
txtContact (input)
txtEmail ou txtFax (input)
*/	

//1º - reset dos estilos para o estado normal
ResetFormStyles();


var i_tmp;
var i_result;
i_result	=true; //valor por defeito

i_tmp		=document.getElementById("msgWarning");
i_tmp.style.visibility="hidden";

	
i_tmp		=document.getElementById("txtOrder");
if(i_tmp.value=="")
{	
	
	ChangeStyle(i_tmp,true);
	i_result= false;
}

i_tmp		=document.getElementById("txtContact");
if(i_tmp.value=="")
{
	ChangeStyle(i_tmp,true);
	i_result= false;
}

i_tmp		=document.getElementById("txtEmail");
if(i_tmp.value=="")
{
	//se o e-mail não foi introduzido tem que ter o fax
	var i_tmp2;
	i_tmp2		=document.getElementById("txtFax");
	if(i_tmp2.value=="")
	{
		ChangeStyle(i_tmp,true);
		ChangeStyle(i_tmp2,true);
		i_result= false;
	}	
	
}else
	{
		if(CheckValidEmail(i_tmp.value)==false)
		{
			ChangeStyle(i_tmp,true);
			i_result= false;
		}
	}


if (i_result==true)
{
document.forms[0].submit();	
} else
	{
	i_tmp		=document.getElementById("msgWarning");
	i_tmp.style.visibility="visible";
	}
}




function CheckInfoForm()
{
	//document.forms[0].submit();
/*
campos obrigatórios
txtMsg (textarea)
txtContact (input)
*/	

//1º - reset dos estilos para o estado normal
ResetFormStyles();


var i_tmp;
var i_result;
i_result	=true; //valor por defeito

i_tmp		=document.getElementById("msgWarning");
i_tmp.style.visibility="hidden";

	
i_tmp		=document.getElementById("txtMsg");
if(i_tmp.value=="")
{	
	
	ChangeStyle(i_tmp,true);
	i_result= false;
}

i_tmp		=document.getElementById("txtContact");
if(i_tmp.value=="")
{
	ChangeStyle(i_tmp,true);
	i_result= false;
}



if (i_result==true)
{
document.forms[0].submit();	
} else
	{
	i_tmp		=document.getElementById("msgWarning");
	i_tmp.style.visibility="visible";
	}
}







/*
Usada para ir contando os caracteres num campo de texto e actualizando um campo de contagem dos mesmos
Chamado:onKeyUp="UpdateTextCounter(this.form.mensagem_grp,this.form.remLen,160);
<input readonly type="text" name="remLen" class="caixinha" size="4" maxlength="4"
value="160"> 
**/
function UpdateTextCounter(p_txtfield,p_countfield,p_textlimit)
{
	if(p_txtfield.value.length>p_textlimit)
	{
		p_txtfield.value = p_txtfield.value.substring(0, p_textlimit);
	}else
		{
	 		p_countfield.value = p_textlimit - p_txtfield.value.length;
		}
	
}//fim de UpdateTextCounter


/**
verifica se os radiobuttons estão preenchidos no inquérito de satisfação (inquiry.php)
*/
function ValidateInquiry()
{

//limpar qualquer mensagem de erro que possa estar visivel
var i_tmp =document.getElementById("msgWarning");
i_tmp.style.visibility="hidden";


//1º - reset dos estilos para o estado normal
ResetFormStyles();
var i_arrayradio	=["r1","r2","r3","r4","r5","r6","r7","r8","r9","r10"];
var i_tmpfield;
var i_tmpresult		=false; //valor por defeito


i_tmptxt		=document.getElementById("txtContact");
if(i_tmptxt.value=="")
{
	ChangeStyle(i_tmptxt,true);
	i_tmp.style.visibility="visible";
	return false;
}

for(var i_counter1=0;i_counter1<i_arrayradio.length; i_counter1++)
{
	i_tmpfield	=eval("document.forms[0]." + i_arrayradio[i_counter1]);
	
	for(var i_counter2=0;i_counter2<i_tmpfield.length;i_counter2++)
	{
		if(i_tmpfield[i_counter2].checked==true)
		{
			i_tmpresult=true;
		}
	}
	//basta que um dos radios não esteja preenchido para o resultado ser falso
	if(i_tmpresult==false)
	{
		i_tmpfield[0].focus();
		i_tmp.style.visibility="visible";
		return false;	
	}else
		{
			i_tmpresult		=false;
		}	
}



document.forms[0].submit();
	
}//fim de ValidateInquiry





/*****************************
FIM DO FORMS VALIDATION
******************************/



