var Registration = new Registration();
function Registration(){

    this.onlyLetters = function(el){
        authorSaved = false;
        if(el.value.match(/[^a-zA-Z ]/g)){
            el.value = el.value.replace(/[^a-zA-Z ]/g,"");
            $('errorLabel').innerHTML = "Only letters (a-z and A-Z) are allowed!";
            el.style.borderColor="red";
            return false;
        }   
        return true;
    }
    this.validLastName = function(el){
         authorSaved = false;
        if(el.value.match(/[^a-zA-Z -]/g)){
            el.value = el.value.replace(/[^a-zA-Z -]/g,"");
            $('errorLabel').innerHTML = "Only letters and - are allowed!";
            el.style.borderColor="red";
            return false;
        }   
        return true;
    }
    this.onlyLettersAndNumbers = function(el){
        authorSaved = false;
        if(el.value.match(/[^a-zA-Z0-9 .,\-\+]/g)){
            el.value = el.value.replace(/[^a-zA-Z0-9 .,\- \+]/g,"") ;  
            $('errorLabel').innerHTML = "Only letters, numbers and + - . , are alowed!";
            el.style.borderColor="red";
            return false;
        }
        return true;
    }
    this.validEmail = function(el){
        authorSaved = false;
        var re = new RegExp("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$");
        if(el.value ==$("confemail").value){
            clearError($("confemail"));
            confMail = true;
        }
        if(el.value.match(re)){
            clearError(el);
            return true;
        }
        $('errorLabel').innerHTML = "The email adress is not valid. Please enter a valid email adress.";
        el.style.borderColor="red";
        return false;
    }
    this.onlyEmailChar = function(el){
        authorSaved = false;
        if(el.value.match(/[\\'\\" ]/g)){
            el.value = el.value.replace(/[\\' \\" ]/g,"") ;  
            $('errorLabel').innerHTML = "Characters like ' and \" are not allowed";
            el.style.borderColor="red";
            return false;
        }
        return true;
    }
    this.validPhone = function(el){
        authorSaved = false;
    if(el.value.match(/[^0-9 + - ]/g)){
            el.value = el.value.replace(/[^0-9 \+ \- ]/g,"") ;  
            $('errorLabel').innerHTML = "Only letters, numbers and + - are allowed!";
            el.style.borderColor="red";
            return false;
        }
        return true;
    }
    this.confirmEmail = function(el){
        if(el.value !=$("email").value){
            alert("The email adresses are different!");
            confMail = false;
            el.style.borderColor="red";
            return false;
        }
        clearError(el);
        confMail = true;
        return true;   
    }
    this.clearError = function(el){
        $('errorLabel').innerHTML = "";
        el.style.borderColor="";
    }
    this.onlyLettersAndNumbersTextArea = function(el){
        authorSaved = false;
         if(el.value.match(/[\\'\\"]/g)){
            el.value = el.value.replace(/[\\'\\"]/g,"") ;   
            $('errorLabel').innerHTML = "Quotes and double quotes are not allowed!";
            return false;
        }
        return true;
    }
    this.submitRegistration = function(){
        var msg = "";
        if(!confirmEmail($("confemail"))) return false;
        if($("fname").value ==""){
            msg+="-The first name is empty. \n";    
        }
        if($("lname").value ==""){
            msg+="-The last name is empty. \n";    
        }
        if($("title").value =="-" ||$("title").value == ""){
            msg+="-The title is empty. \n";    
        }
        if($("university").value ==""){
            msg+="-The university is empty. \n";    
        }
        if($("address").value ==""){
            msg+="-The address is empty. \n";    
        }
        if($("postalcode").value ==""){
            msg+="-The zip/postal code is empty. \n";    
        }
        if($("city").value ==""){
            msg+="-The city is empty. \n";    
        }
        if($("country").value ==""){
            msg+="-The country is empty. \n";    
        }
        if($("email").value ==""){
            msg+="-The email adress is empty. \n";    
        }
        if($("phone").value ==""){
            msg+="-The phone number is empty. \n";    
        }
        if(msg !=""){
            alert("the following error(s) occurred:\n" + msg);
            document.getElementById('captcha').src = './includes/securimage/securimage_show.php?' + Math.random();
            return false;
        }
        checkCaptcha();
    }
    function checkAuthor(fname,lname,uni){
        new Ajax.Request('./submitting/processregistration.php',{
            method:'get',
             parameters: {'function': 'checkAuthor','fname':fname,'lname':lname,'university':uni},
            onSuccess: function(transport){
              eval('var foundAuthor = new Array("'+transport.responseText+'");');
              if(foundAuthor[0] != "null"){
                  if(confirm("An author with name "+foundAuthor[2]+" " +foundAuthor[1] + " from university "+foundAuthor[3]+" is already in the database. Do you want register as this author? " )){
                      finishRegistration(true,foundAuthor[0]);
                  }else{
                      finishRegistration(false);
                  }
                }else{
                      finishRegistration(false);
                }
            },
            //@TODO e-mail when something goes wrong!!
            onFailure: function(){}
          }); 
    }
    function finishRegistration(existingAuthor,authorId){
        var vogon = "no";
        var phd = "no";
        if($("radioVogonYes").checked){
            vogon = "yes";
        }
        if($("radioPHDYes").checked){
            phd = "yes";
        }
        if(existingAuthor){
            var par = {'function': 'saveRegistration','authorId':authorId,'AccompanyingPersonfn': $("AccompanyingPersonfn").value,'AccompanyingPersonln': $("AccompanyingPersonln").value,'specialRequests':$("specialRequests").value,"vogon":vogon,"phd":phd}; 
        }else{
            var par = {'function':'saveRegistration','fname':$("fname").value,'lname':$("lname").value, 'title':$("title").value, 'email':$("email").value, 'university':$("university").value,'address':$("address").value,'postalcode':$("postalcode").value, 'city':$("city").value, 'country':$("country").value,'AccompanyingPersonfn': $("AccompanyingPersonfn").value,'AccompanyingPersonln': $("AccompanyingPersonln").value,'specialRequests':$("specialRequests").value,"phone":$("phone").value,"vogon":vogon,"phd":phd };
        }

        new Ajax.Request('./submitting/processregistration.php',{
            method:'get',
             parameters: par,
            onSuccess: function(transport){
                  if(transport.responseText == "true"){
                      alert("Your registration has been submitted succesfully. A confirmation has been sent to your e-mail account.\n Please contact us at info@eres2011.com if you did not get the confirmation email within a day.");
                  } else{
                      alert("Your registration has not been submitted succesfully. Please contact us at info@eres2011.com and include the following error message: " + transport.responseText);    
                  }
                  loadMenuItem($("home"),"home","Conference 2011","Welcome to Eindhoven, The Netherlands, 15<sup>th</sup>-18<sup>th</sub> June 2011");
            },
            //@TODO e-mail when something goes wrong!!
            onFailure: function(){}
          }); 

    }
    function checkCaptcha(){
        setloading(true);
        var code = "";
        if(typeof($('captcha_code').value) !="undefined"){
             code = $('captcha_code').value;
        }
        var check = "test ";
        new Ajax.Request('./submitting/processregistration.php',{
          method:'get',
           parameters: {'function': 'checkCaptcha','code':code},
          onSuccess: function(transport){
            if(transport.responseText == "true"){
                setloading(true);
                 checkAuthor($("fname").value,$("lname").value,$("university").value);
                setLoading(false);
            
            }else{
                setloading(true);
                alert("the following error(s) occurred:\n -The security code from the security image is not filled in correctly.");
                document.getElementById('captcha').src = './includes/securimage/securimage_show.php?' + Math.random();
                setloading(false);
            }
          },
          //@TODO e-mail when something goes wrong!!
          onFailure: function(){}
        }); 
        return check; 
    }

}
