/* NEW VALIDATION FUNCTIONS */
function validateContent(Value){
	return(Value!=null&&Value!="");
	};

/* INSTRUCOES PARA O SMARTSTORE
Substituir as definicoes das funcoes validateString()
nos arquivos dialogue.js e pchasecustinfo.js
por este arquivo, lembrando de somente utilizar a 
validacao de string para CPF's e CNPJ's.
*/

//by bamboo, szilard & igbassani and ulysses


//Remove da string <str> todos os caracteres <char>
function ClearStr(str, char)
{
  while((cx=str.indexOf(char))!=-1)
  {		
    str = str.substring(0,cx)+str.substring(cx+1);
  }
  return(str);
}


//Remove os caracteres de auxílio como "-", ".", ",", "/", etc.
//Se a string resultante não for composta apenas por números retorna 0
function ParseNumb(c)
{
  c=ClearStr(c,'-');
  c=ClearStr(c,'/');
  c=ClearStr(c,',');
  c=ClearStr(c,'.');
  c=ClearStr(c,'(');
  c=ClearStr(c,')');
  c=ClearStr(c,' ');
  if((parseFloat(c) / c != 1))
  {
    if(parseFloat(c) * c == 0)
    {
      return(c);
    }
    else
    {
      return(0);
    }
  }
  else
  {
    return(c);
  }
}


//Verifica o número <CNUMB> se é CPF ou CNPJ atualizando a variavel CTYPE e repassando os blocos
//para a função TestDigit. Se algum dos blocos for inválido, a função dá um aviso.
function validateString(CNUMB)
{
  var CTYPE = "CNPJ/CPF";

  CNUMB=ClearStr(CNUMB,' ');
  if(CNUMB==null||CNUMB=="")
  {
    return(true);
  }

  CNUMB=ParseNumb(CNUMB);


  if(CNUMB.length == 14)
  {
    CTYPE="CNPJ";
  }
  else if(CNUMB.length == 11)
  {
    CTYPE="CPF";
  }
  else
  {
//    alert(CTYPE+" inválido!");
    return(false);
  }


  if(CNUMB == 0)
  {
//    alert(CTYPE+" inválido!");
    return(false);
  }
  else
  {
    g=CNUMB.length-2;
    if(TestDigit(CNUMB,CTYPE,g))
    {
      g=CNUMB.length-1;
      if(TestDigit(CNUMB,CTYPE,g))
      {	
//        alert(CTYPE+" válido!");
        return(true);
      }
      else
      {
//        alert(CTYPE+" inválido!");
        return(false);
      }
    }
    else
    {
//      alert(CTYPE+" inválido!");
      return(false);
    }
  }
}


function TestDigit(CNUMB,CTIPO,g)
{
  var dig=0;
  var ind=2;
  for(f=g;f>0;f--)
  {
    dig+=parseInt(CNUMB.charAt(f-1))*ind;
    if (CTIPO=='CNPJ')
    { if(ind>8) {ind=2} else {ind++} }
    else
    { ind++ }
  }
  dig%=11;
  if(dig<2)
  {
    dig=0;
  }
  else
  {
    dig=11-dig;
  }
  if(dig!=parseInt(CNUMB.charAt(g)))
  {
    return(false);
  }
  else
  {
    return(true);
  }
}
	
function validateNumber(Value){
var regExpTerm = /[0-9]+(.[0-9]+)*/;
	return(validateRegExp(regExpTerm, Value))
	};
	
function validateEmail(Value){
var posAt, posDt;
		posAt = Value.indexOf("@");
		posDt = Value.lastIndexOf(".");
		if(!(posAt>0&&posAt<posDt&&posDt<Value.length-1)) return false
		else return true;
	};

function validateRegExp(Expression, Value){
var regExpValue = Expression.exec(Value);
	if(regExpValue!=null){
		if(regExpValue.toString().indexOf(",")!=-1) return(regExpValue[0]==Value)
		else return(regExpValue==Value);
		}
	else return(false);
	};

function compareAgainst(Value, OperatorMode, CompareTo){
	if(OperatorMode==0) return(Value==CompareTo)
	else if(OperatorMode==1) return(Value!=CompareTo)
	else if(OperatorMode==2) return(Value<CompareTo)
	else if(OperatorMode==3) return(Value<=CompareTo)
	else if(OperatorMode==4) return(Value>CompareTo)
	else if(OperatorMode==5) return(Value>=CompareTo)
	return(false);
	};

function compareRange(Value, MinValue, MaxValue){
var nValue = "";
var nResult = true;
	if(isNaN(Value)) nValue = Value.length
	else nValue = parseFloat(Value);
	if(MinValue>=0) nResult = ( nValue >= MinValue );
	if(MaxValue>0) nResult = ( nResult && ( nValue <= MaxValue ) );
	return nResult;
	};

function setupDialogueMessage(){
	var seperator = "";
	var tmpCaption = "";
	var tmpMessage = "";
	var elem = null;

	for(var i=0;i<41;i++) seperator += "=";
	seperator += "\n"
	
	tmpMessage += seperator;

	for(var i=0; i<document.Contact.elements.length; i++){
		elem = document.Contact.elements[i];
		if(elem.type!="hidden"){
			tmpCaption = decryptTextData(getElementByID(xmlOrder,elem.name).caption);
			
			if(elem.type=="text"||elem.type=="password"){
				tmpMessage += tmpCaption + " ";
				tmpMessage += elem.value + "\n";
				}
			else if(elem.type=="textarea"){
				tmpMessage += seperator + tmpCaption + "\n";
				tmpMessage += elem.value + "\n" + seperator;
				}
			else if(elem.type=="select-one"){
				tmpMessage += tmpCaption + "\n";
				tmpMessage += " * " + elem.options[elem.selectedIndex].value + "\n" + seperator;
				}
			else if(elem.type=="select-multiple"){
				tmpMessage += tmpCaption + "\n";
				for(var j=0; j<elem.options.length; j++){
					if(elem.options[j].selected==true){
						tmpMessage += " * " + elem.options[j].value + "\n";
						};
					};
				tmpMessage += seperator;
				}
			else if(elem.type=="radio"){
				if(elem.checked==true){
					tmpMessage += tmpCaption + "\n";
					tmpMessage += " * " + elem.value + "\n";
					};
				}
			else if(elem.type=="checkbox"){
				tmpMessage += tmpCaption + "\n";
				tmpMessage += " * " + ((elem.checked) ? "Sim" : "Não") + "\n";
				}
			else{
				tmpMessage += "Missing specification : " + elem.type + ", Contact www.smartstore.com\n";
				};
			};
		};
	document.Contact.SenderName.value = document.Contact.FirstName.value + " " + document.Contact.LastName.value;
	document.Contact.From.value = document.Contact.Email.value;
	document.Contact.Message.value = tmpMessage;
	document.Contact.MerchantID.value = merchantID;
	document.Contact.Build.value = replace(build,".","");
	document.Contact.SiteDirectory.value = unescape(location.href.substring(0,location.href.indexOf("dialogue.htm")));
	document.Contact.DataString.value =	xmlOrder.putSource();
	return(true);
	};
	
	
function checkForm(){
	document.Contact.DialogueSubject.blur();if(document.Contact.DialogueSubject.selectedIndex!=-1){if(!(validateContent(document.Contact.DialogueSubject[document.Contact.DialogueSubject.selectedIndex].text))){alert("Dados incompletos. Por favor selecione um 'Assunto'!");document.Contact.DialogueSubject.focus();return(false);};} else{ alert("Dados incompletos. Por favor selecione um 'Assunto'!");return(false); };if(!(validateContent(document.Contact.LastName.value))){alert("Dados incompletos. Por favor forneça o 'Nome da Revenda'!");document.Contact.LastName.focus();return(false);};if(!(validateContent(document.Contact.FirstName.value))){alert("Dados incompletos. Por favor forneça o 'Seu Nome'!");document.Contact.FirstName.focus();return(false);};if(!(validateContent(document.Contact.Email.value))){alert("Dados incompletos. Por favor forneça seu 'Email'!");document.Contact.Email.focus();return(false);};if(!(validateEmail(document.Contact.Email.value))){alert("Dados incompletos. Por favor forneça um 'Email' válido!");document.Contact.Email.focus();return(false);};
		if(!(validateString(document.Contact.CNPJ.value))){alert("CNPJ inválido!");document.Contact.CNPJ.focus();return(false);};
		if(!(validateContent(document.Contact.Comment.value))){alert("Dados incompletos. Por favor digite sua mensagem!");document.Contact.Comment.focus();return(false);};
		return(setupDialogueMessage());
	};


