function startOptinCallback(){
    return true;    
}

function completeOptinCallback(response){
    var errorStr = '';
    var campi = {"from" : "E-mail" , "name" : "Nome"};
    var rJSON = eval('a = (' + response + ')');
    if (rJSON.error !='' && rJSON.error!=null){
        
        if (rJSON.error == 'no_campo'){
            errorStr = 'Il campo ' + campi[rJSON.campo] + ' &egrave; richiesto';                            
        }
        else if (rJSON.error == 'verify' ){
            errorStr = 'Le due ' + campi[rJSON.campo] + ' non coincidono!';          
        }
        else if (rJSON.error == "in_use"){
            if (rJSON.campo == 'email'){
                errorStr = "L'" + campi[rJSON.campo] + ' inserita si trova gi&agrave; in uso!<br><a href="b-login.php">Accedi con il tuo account</a>';         
            }
            else {
                errorStr = "Il " + campi[rJSON.campo] + ' inserito si trova gi&agrave; in uso. Inserisci un ' + campi[rJSON.campo] + ' differente.';  
            }
        }
        else if (rJSON.error == "not_valid"){
            if (rJSON.campo == 'email'){   
                errorStr = "Il formato dell'" + campi[rJSON.campo] + " inserita non &egrave; corretto.";    
            }
            else {
                errorStr = "Il formato dell" + campi[rJSON.campo] + " inserito non &egrave; corretto. Puoi inserire lettere (non accentate), numeri e caratteri quali: \"-\" \"_\" e \".\" ";        
            }        
        }
        if(prendiElementoDaId("error_msg")){
            prendiElementoDaId("error_msg").innerHTML = errorStr;
        }
    }
    else{
        prendiElementoDaId("from_optin").value= prendiElementoDaId("from").value;
        prendiElementoDaId("name_optin").value= prendiElementoDaId("name").value;                          
        prendiElementoDaId("addlead").submit();
        
    } 
    
}

function startOptinCallback2(){
    return true;    
}

function completeOptinCallback2(response){
    var errorStr = '';
    var campi = {"from" : "E-mail" , "name" : "Nome"};
    var rJSON = eval('a = (' + response + ')');
    if (rJSON.error !='' && rJSON.error!=null){
        //alert (rJSON.error + " " + rJSON.campo);
        if (rJSON.error == 'no_campo'){
            errorStr = 'Il campo ' + campi[rJSON.campo] + ' &egrave; richiesto';                            }
        else if (rJSON.error == 'verify' ){
            errorStr = 'Le due ' + campi[rJSON.campo] + ' non coincidono!';          
        }
        else if (rJSON.error == "in_use"){
            if (rJSON.campo == 'email'){
                errorStr = "L'" + campi[rJSON.campo] + ' inserita si trova gi&agrave; in uso!<br><a href="b-login.php">Accedi con il tuo account</a>';         
            }
            else {
                errorStr = "Il " + campi[rJSON.campo] + ' inserito si trova gi&agrave; in uso. Inserisci un ' + campi[rJSON.campo] + ' differente.';  
            }
        }
        else if (rJSON.error == "not_valid"){
            if (rJSON.campo == 'email'){   
                errorStr = "Il formato dell'" + campi[rJSON.campo] + " inserita non &egrave; corretto.";    
            }
            else {
                errorStr = "Il formato dell" + campi[rJSON.campo] + " inserito non &egrave; corretto. Puoi inserire lettere (non accentate), numeri e caratteri quali: \"-\" \"_\" e \".\" ";        
            }        
        }
        if(prendiElementoDaId("error_msg")){
            prendiElementoDaId("error_msg").innerHTML = errorStr;
        }
    }
    else{
        prendiElementoDaId("from_optin").value= prendiElementoDaId("from2").value;
        prendiElementoDaId("name_optin").value= prendiElementoDaId("name2").value;                          
        prendiElementoDaId("addlead").submit();
        
    } 
    
}

function trackClicks(pack_id){
    
        
        var qs = new Querystring();
        var aff_id = qs.get("aff_id",0);       
        var camp_id = qs.get("camp_id",0);
        var ref = document.referrer;
        
        sendAjaxTrackClicks(pack_id,aff_id,camp_id,ref);
    
    
}

function sendAjaxTrackClicks(pack_id,aff_id,camp_id,ref){
    var url = "http://nuovo-internet-marketing.com/trckng/?pack_id=" + pack_id + "&aff_id=" + aff_id + "&camp_id=" + camp_id + "&ref=" + encodeURIComponent(ref);
    
    
    new Ajax.Request(url, {
      method: 'get',
      
      onSuccess: function(transport) {
        //Do Nothing... 
      }
    });
}

var pack_optin = 0;

function set_trackOptin(pack_id){
    pack_optin = pack_id;
    $('addlead').onsubmit = function(){
        
        var qs = new Querystring();
        var aff_id = qs.get("aff_id",0);       
        var camp_id = qs.get("camp_id",0);
        var ref = document.referrer;
        var name = $('optin_name').value;
        var email = $('optin_email').value;
        var url = "http://nuovo-internet-marketing.com/trckng/optin_track.php?pack_id=" + pack_optin + "&aff_id=" + aff_id + "&camp_id=" + camp_id + "&ref=" + encodeURIComponent(ref) + "&name=" + name + "&from=" + email + "&pagename=custom";
        
        new Ajax.Request(url, {
           method: 'get',
      
            onSuccess: function(transport) {
             //Do Nothing... 
              var rJSON = eval('a = (' + transport.responseText + ')');   
              if (rJSON.error !='' && rJSON.error!=null){
                var campi = {"email" : "E-mail" , "name" : "Nome"};
                if (rJSON.error == 'no_campo'){
            errorStr = 'Il campo ' + campi[rJSON.campo] + ' è richiesto';                            
        }
        else if (rJSON.error == 'verify' ){
            errorStr = 'Le due ' + campi[rJSON.campo] + ' non coincidono!';          
        }
        else if (rJSON.error == "in_use"){
            if (rJSON.campo == 'email'){
                errorStr = "L'" + campi[rJSON.campo] + ' inserita si trova già in uso!<br><a href="b-login.php">Accedi con il tuo account</a>';         
            }
            else {
                errorStr = "Il " + campi[rJSON.campo] + ' inserito si trova già in uso. Inserisci un ' + campi[rJSON.campo] + ' differente.';  
            }
        }
        else if (rJSON.error == "not_valid"){
            if (rJSON.campo == 'email'){   
                errorStr = "Il formato dell'" + campi[rJSON.campo] + " inserita non è corretto.";    
            }
            else {
                errorStr = "Il formato dell" + campi[rJSON.campo] + " inserito non è corretto. Puoi inserire lettere (non accentate), numeri e caratteri quali: \"-\" \"_\" e \".\" ";        
            }        
        }
        if($("error_msg")){
            $("error_msg").innerHTML = errorStr;
        }
        else {
            alert(errorStr);
        }  
              
              }
              else{
                setTimeout("submitForm()", 500);
                $('addlead').onsubmit = function(){ return true;};
                
                
                
                
              } 
             //
           }
        });
        return false;
                                                                            
    }
}

function submitForm(){
    $('addlead').submit();
    
    set_trackOptin(pack_optin);  
    
}
