// load the appropriate xmlHttpRequest for IE or Mozilla
// this sniffer code can be found at
// http://jibbering.com/2002/4/httprequest.html
var xmlHttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
  try {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlHttp=false
  }
}
@else
xmlHttp=false
@end @*/
if (!xmlHttp) {
try {
  xmlHttp = new XMLHttpRequest();
}
catch (e) {
  xmlHttp=false
}
}
// end jibbering.com code
// the php script to process the form.  var must be global!
var URLto = 'AplicacaoEmail/Requests/SMS/Confirmar.php';
// function to build POST requests
function buildPOST(theFormName) {
    theForm = document.forms[theFormName];
    var qs = ''
    for (e=0;e<theForm.elements.length;e++) {
        if (theForm.elements[e].name!='') {
            var name = theForm.elements[e].name;
            qs+=(qs=='')?'':'&'
            qs+= name+'='+escape(theForm.elements[e].value);
        }
    }
    qs+="\n";
    return qs
    
}
// function to communicate with remote script
function Enviar_SMS(theFormName) {
    var xmlMessage = buildPOST(theFormName);
      
      nrOrigem = document.getElementById("origem").value;
      nrDestino = document.getElementById("destino").value;
      nrIndOrigem = document.getElementById("indicativode").value;
      nrIndDestino = document.getElementById("indicativopara").value;
    
    
    Desactivar_Edicao_SMS()
      
    xmlHttp.open("POST", URLto, false)
       
    // for ie compatability
    xmlHttp.setRequestHeader('Content-Type','text/html')
   
    xmlHttp.send(xmlMessage)
}
function Mostrar_Resposta() {
    var optionDiv = document.getElementById("responseContainer");
    optionDiv.innerHTML = xmlHttp.responseText;
}

function Limpar_Resposta() {
    var optionDiv = document.getElementById("responseContainer");
    optionDiv.innerHTML = "  ";
    }

//mostra opcoes e resultado da validacao
function Mostrar_Opcoes() {
    Limpar_Resposta()
    var optionDiv = document.getElementById("responseContainer");
    //optionDiv.innerHTML = "<hr/><div align='center' class='boxads'><img src='AplicacaoEmail/Media/EditarSMS.gif' border='0' width='20' height='20' vspace='0' align='top'><a onClick='Javascript: Modo_Editar_SMS();'>Edita</a>&nbsp;<img src='AplicacaoEmail/Media/CancelarSMS.gif' border='0' width='20' height='20' vspace=0 align='top'><a onClick='Cancelar_SMS();'>Apaga</a>&nbsp;" + xmlHttp.responseText + "</div>";
    optionDiv.innerHTML = xmlHttp.responseText;
}

function Contar_Caracteres(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else 
    countfield.value = maxlimit - field.value.length;
    }
    
function Modo_Editar_SMS () {
      document.SMSForm.indicativode.disabled=false;
      document.SMSForm.origem.disabled=false;
      document.SMSForm.indicativopara.disabled=false;
      document.SMSForm.destino.disabled=false;
      document.SMSForm.mensagem.disabled=false;
      //document.SMSForm.remLen.disabled=false;
      document.SMSForm.validator.disabled=false;
      //document.SMSForm.botao.disabled=false;
      Limpar_Resposta();
      
}

function Cancelar_SMS() {

PedidoSimplesComCabecalho('rapidinhaSMS', 'AplicacaoEmail/Includes/SMSrapidinha.php', 'SMS Rapidinha');

}

function Desactivar_Edicao_SMS() {

      document.SMSForm.indicativode.disabled=true;
      document.SMSForm.origem.disabled=true;
      document.SMSForm.indicativopara.disabled=true;
      document.SMSForm.destino.disabled=true;
      document.SMSForm.mensagem.disabled=true;
      //document.SMSForm.remLen.disabled=false;
      document.SMSForm.validator.disabled=true;
      //document.SMSForm.botao.disabled=true;
      
}

	function Enviar_Mensagem_Request( div, path, destino ) {          
        //se tiver suporte ajax     
        if( ( ajax = Verifica_Browser( ) ) != null ) {      
            idOpcao  = document.getElementById( div );          
            ajax.open("POST", path , true );
            ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            ajax.onreadystatechange = function() {
                //enquanto estiver processando...emite a msg de carregando
                if(ajax.readyState == 1) {
                    idOpcao.innerHTML = "<h2>SMS Rapidinha</h2><h3>A Enviar SMS</h3><DIV class='boxads'>Por favor, aguarde ...<BR /><img align='middle' src='AplicacaoEmail/Media/EnviandoSMS.gif' border='0'><BR /><BR />Estamos a processar o envio da SMS para <B>" + destino + "</B><BR /><BR /><img align='middle' src='AplicacaoEmail/Media/AEnviarSMS.gif' border='0'><BR /><BR /><H3>Esta opera&ccedil;&atilde;o demora 5/10 segundos</H3></DIV>";
                }
                if(ajax.readyState == 4 ) {
                    if( ajax.responseText ) {                       
                        idOpcao.innerHTML = ajax.responseText;
                    } else {
                        idOpcao.innerHTML = "Gateway SMS indisponivel de momento!";
                    }                   
                }
             }
             ajax.send(1);
          }       
       }
