var Inscription_asproot = '/asp/';
var Inscription_phproot = '/php/';
jQuery.noConflict();
jQuery(document).ready(function() {
 jQuery('#nicknameLoading').hide();
 jQuery('#textreq_usr_nick').blur(function(){
 return textreq_usr_nick_check();
    });//end blur
 jQuery('#textreq_usr_nick').keyup(function(e){
    if(e.keyCode == 13){textreq_usr_nick_check();};
 });//end keyup
 
 jQuery('#useremailLoading').hide();
 jQuery('#textreqemail_usr_email').blur(function(){
    return textreqemail_usr_email_check();
 });//end blur
 jQuery('#textreqemail_usr_email').keyup(function(e){
    if(e.keyCode == 13){textreqemail_usr_email_check();};
 });//end keyup
 jQuery('#textreq6min_usr_password').blur(function(){
 Inscription_CheckPassword();
 });//end blur
 jQuery('#textreq_usr_password2').blur(function(){
 Inscription_CheckPassword();
 });//end blur
 jQuery('#selectreq_usr_birth_year').change(function(){
    selectCalendarChange('selectreq_usr_birth_day', 'selectreq_usr_birth_month', 'selectreq_usr_birth_year');
 });//end change
 jQuery('#selectreq_usr_birth_month').change(function(){
    selectCalendarChange('selectreq_usr_birth_day', 'selectreq_usr_birth_month', 'selectreq_usr_birth_year');
 });//end change

 var lg = jQuery('#hidden_lang_code').val();
 selectCalendarFillDays('selectreq_usr_birth_day');
 selectCalendarFillMonths('selectreq_usr_birth_month', lg);
 selectCalendarFillYears('selectreq_usr_birth_year', new Date().getFullYear() - 100, new Date().getFullYear());    
 selectCalendarChange('selectreq_usr_birth_day', 'selectreq_usr_birth_month', 'selectreq_usr_birth_year');


 if(document.getElementById('chkFrm001'))
 {
    Inscription_GetUser();
 };
});//end ready
function stopLoadingShowResult(idLoading, idResult, response) {
  if(idLoading != null) jQuery('#'+idLoading).hide();
  if(response != '')
  {
      jQuery('#'+idResult).html(unescape(response));
      jQuery('#'+idResult).fadeIn();
  };
} //end stopLoadingShowResult
function textreq_usr_nick_check()
{
 var nick = jQuery('#textreq_usr_nick').val();
 var current_nick = '' + jQuery('#hidden_current_nick').val();
 var current_shortname = '' + jQuery('#hidden_current_shortname').val();
 var lg = jQuery('#hidden_lang_code').val();
 if(nick.length == 0){document.getElementById('textreq_usr_nick').setAttribute('haserror', '1');jQuery('#check_button').show();jQuery('#save_button').hide();return false;};
 if(nick.toLowerCase() == current_nick.toLowerCase())
{document.getElementById('textreq_usr_nick').setAttribute('haserror', '0');jQuery('#nicknameResult').hide();jQuery('#check_button').hide();jQuery('#save_button').show();return true;};
 if(current_shortname != null){
 if(nick.toLowerCase() == current_shortname.toLowerCase())
{document.getElementById('textreq_usr_nick').setAttribute('haserror', '0');jQuery('#nicknameResult').hide();jQuery('#check_button').hide();jQuery('#save_button').show();return true;};
 };
    jQuery('#nicknameLoading').show();
    jQuery.post(Inscription_phproot + "check_nickname.php", 
              {textreq_usr_nick: nick, hidden_lang_code: lg},
              function(response){ 
                  jQuery('#nicknameResult').fadeOut(); 
                  response = eval(response);
                  document.getElementById('textreq_usr_nick').setAttribute('haserror', (response.isValid == '1') ? '0' : '1');
                  setTimeout("stopLoadingShowResult('nicknameLoading','nicknameResult', '"+escape(response.content)+"')", 400);
                  if(response.isValid == '1') {
                     jQuery('#check_button').hide();jQuery('#save_button').show();  
                  }
                  else {
                     jQuery('#check_button').show();jQuery('#save_button').hide();  
                  }
              }
        );//end post
    return false;
};
function textreqemail_usr_email_check()
{
    var email = jQuery('#textreqemail_usr_email').val();
    var lg = jQuery('#hidden_lang_code').val();
    if(email.length == 0){document.getElementById('textreqemail_usr_email').setAttribute('haserror', '0');return true;};
    var regEx_Email = new RegExp('^[\\w\\-]+(\\.[\\w\\-]+)*@[\\w\\-]+(\\.[\\w\\-]+)*\\.[\\w\\-]{2,}$', 'i');
    if(regEx_Email.test(email)){ //valid email address : check against the DB
        jQuery('#useremailLoading').show();
        jQuery.post(Inscription_phproot + "check_userinfo.php", 
            {textreq_usr_field: 'email',textreq_usr_value: email,hidden_lang_code: lg}, 
            function(response){
                      jQuery('#useremailResult').fadeOut(); 
                      response = eval(response);
                      document.getElementById('textreqemail_usr_email').setAttribute('haserror', (response.isValid == '1') ? '0' : '1');
                      setTimeout("stopLoadingShowResult('useremailLoading','useremailResult', '"+escape(response.content)+"')", 400);
            });//end post
    } else { //invalid e-mail address
     var msg = '';
     switch(lg){
         case 'NL' : msg = 'E-mailadres is niet geldig';break;
      case 'FR' : msg = 'L\'adresse e-amil n\'est pas valide';break;
      default : msg = 'E-mail address is not valid';
     };
     document.getElementById('textreqemail_usr_email').setAttribute('haserror', '1');
     setTimeout("stopLoadingShowResult('useremailLoading','useremailResult', '"+escape("<span style=\"color:#f00\">" + msg + "</span>")+"')", 5);
    };
    return false;
};
function Inscription_CheckPassword()
{
 var pwd1 = jQuery('#textreq6min_usr_password').val();
 var pwd2 = jQuery('#textreq_usr_password2').val();
 var lg = jQuery('#hidden_lang_code').val();
 if(pwd1.length > 0 && pwd1.length < 6)
 {
     document.getElementById('textreq_usr_password2').setAttribute('haserror', '1');
  switch(lg){
   case 'NL' : msg = 'Wachtwoord moet ten minste 6 karakters bevatten';break;
   case 'FR' : msg = 'Le mot de passe doit contenir 6 caractères minimum';break;
   default : msg = 'Password must be 6 or more characters in length';
  }; 
  setTimeout("stopLoadingShowResult(null,'userpasswdResult', '"+escape("<span style=\"color:#f00\">" + msg + "</span>")+"')", 5);
  return false;
 }
 else if(pwd2 != '' && pwd1 != pwd2)
 {
     document.getElementById('textreq_usr_password2').setAttribute('haserror', '1');
  switch(lg){
   case 'NL' : msg = 'Paswoorden zijn niet gelijk';break;
   case 'FR' : msg = 'Veuillez vous assurer que les deux mots de passe tapés sont identiques';break;
   default : msg = 'Password must be identical';
  }; 
  setTimeout("stopLoadingShowResult(null,'userpasswdResult', '"+escape("<span style=\"color:#f00\">" + msg + "</span>")+"')", 5);
  return false;
 };
 document.getElementById('textreq_usr_password2').setAttribute('haserror', '0');
 jQuery('#userpasswdResult').fadeOut(); 
 return true;
};

function Inscription_GetValue(name)
{    
    var lst = document.getElementsByName(name);
    var obj = null;
    if(lst.length == 0){obj = document.getElementById(name);}
    else{obj = lst[0];};
    if(obj == null){return '';};
    if(obj.tagName == 'SELECT'){return obj.options[obj.selectedIndex].value;};
    if(obj.tagName == 'INPUT' && obj.type == 'checkbox'){return obj.checked ? 1 : 0;};
    if(obj.tagName == 'INPUT' && obj.type == 'password'){return obj.value;};
    if(obj.tagName == 'INPUT' && obj.type == 'text'){return obj.value;};
    if(obj.tagName == 'INPUT' && obj.type == 'hidden'){return obj.value;};
    if(obj.tagName == 'INPUT' && obj.type == 'radio' && lst.length > 0)
    {
        for(var i = 0; i < lst.length; i++){if(lst[i].checked){return lst[i].value;};};
    };
    return '';
};
function Inscription_SetValue(name, value)
{    
    var lst = document.getElementsByName(name);
    var obj = null;
    if(lst.length == 0){obj = document.getElementById(name);}
    else{obj = lst[0];};
    if(obj == null){return;};
    if(obj.tagName == 'SELECT')
    {
        for(var i = 0; i < obj.options.length; i++)
        {
            obj.options[i].selected = (obj.options[i].value == value);
        };
        return;
    };
    if(obj.tagName == 'INPUT' && obj.type == 'checkbox'){obj.checked = value;return;};
    if(obj.tagName == 'INPUT' && obj.type == 'password'){obj.value = value;return;};
    if(obj.tagName == 'INPUT' && obj.type == 'text'){obj.value = value;return;};
    if(obj.tagName == 'INPUT' && obj.type == 'hidden'){obj.value = value;return;};
    if(obj.tagName == 'INPUT' && obj.type == 'radio')
    {
        for(var i = 0; i < lst.length; i++)
        {
            lst[i].checked = (lst[i].value == value);
        };
        return;
    };
    return '';
};

function Inscription_Right(str, ln){
    if(ln <= 0){return '';};
    if(ln > String(str).length){return str;};
    var sln = String(str).length;
    return String(str).substring(sln, sln - ln);
};
function Inscription_GetUser()
{
    var userid = Inscription_GetValue('hidden_userid');
    var lg = Inscription_GetValue('hidden_lang_code');
    if(isNaN(parseInt(userid))){return;};
    userid = parseInt(userid);
    if(userid <= 0){return;};
    jQuery.post(Inscription_asproot + 'UserInfoGet.asp', 
        {userid: userid,lgcode: lg}, 
        function(response){
            response = eval(response);
            if(response == null){return;};
            Inscription_FillInputs(response);
        });//end post
};
function Inscription_BuildQueryStringFromObject(object)
{
    var qs = '';
 for(var prop in object)
 {
     if(object[prop] != null)
     {
         if(qs != ''){qs += '&'};
         qs += prop + '=' + escape(object[prop] + '');
     };
 };
 return qs;
};
function Inscription_FillInputs(obj)
{
    
    if(obj.firstname){Inscription_SetValue('textreq_usr_fname', obj.firstname);};
    if(obj.lastname){Inscription_SetValue('textreq_usr_name', obj.lastname);};
    if(obj.street){Inscription_SetValue('textreq_usr_street', obj.street);};
    if(obj.nr){Inscription_SetValue('textreq_usr_nr', obj.nr);};
    if(obj.box){Inscription_SetValue('text_usr_box', obj.box);};
    if(obj.zipcode){Inscription_SetValue('textreq_usr_zip', obj.zipcode);};
    if(obj.city){Inscription_SetValue('textreq_usr_city', obj.city);};
    if(obj.country){Inscription_SetValue('select_usr_country', obj.country);};
    
    if(obj.phonenr){Inscription_SetValue('text_usr_telnr', obj.phonenr);};
    if(obj.mobilenr){Inscription_SetValue('text_usr_gsmnr', obj.mobilenr);};
    if(obj.gender){Inscription_SetValue('selectreq_usr_sex', obj.gender);};
    
    if(obj.birthdate && obj.birthdate != '')    
    {
        tmp = parseInt(obj.birthdate.substr(0, 4), 10);
        if(tmp == 0){tmp = '';};
        Inscription_SetValue('selectreq_usr_birth_year', tmp);
        tmp = parseInt(obj.birthdate.substr(4, 2), 10);
        if(tmp == 0){tmp = '';};
        Inscription_SetValue('selectreq_usr_birth_month', tmp);
        tmp = parseInt(obj.birthdate.substr(6, 2), 10);
        if(tmp == 0){tmp = '';};
        Inscription_SetValue('selectreq_usr_birth_day', tmp);
        selectCalendarChange('selectreq_usr_birth_day', 'selectreq_usr_birth_month', 'selectreq_usr_birth_year');
    };    
          
    if(obj.opt_in_news){Inscription_SetValue('cb_newsletter', (obj.opt_in_news == '_on' || obj.opt_in_news == '1'));};  
    if(obj.opt_in_smb){Inscription_SetValue('cb_smb', (obj.opt_in_smb == '_on' || obj.opt_in_smb == '1'));};  
    if(obj.opt_in_partners){Inscription_SetValue('cb_optinpartners', (obj.opt_in_partners == '_on' || obj.opt_in_partners == '1'));};  
    if(obj.opt_out_dm){Inscription_SetValue('cb_dmoptout', (obj.opt_out_dm == '_on' || obj.opt_out_dm == '1'));};  
    if(obj.newslgcode){Inscription_SetValue('selectreq_usr_newslgcode', obj.newslgcode);};    
};
function Inscription_PrepareForSubmit()
{
    var userid = Inscription_GetValue('hidden_userid');
    var lg = Inscription_GetValue('hidden_lang_code');
    if(isNaN(parseInt(userid))){userid = 0;}else{userid = parseInt(userid);};
    //if(userid <= 0){return true;};
    
    var obj = document.getElementById('hidden_userid');
    var frm = obj.parentNode;
    while(frm != null && frm.tagName != 'FORM'){frm = frm.parentNode;};
    
    var lg = Inscription_GetValue('hidden_lang_code');
    if(frm == null){return false;};
    if(!checkForm(frm, lg)){return false;};
    
    var post = new Object();
    post.lgcode = lg;
    //post.userid = userid;
    post.firstname = Inscription_GetValue('textreq_usr_fname');
    post.lastname = Inscription_GetValue('textreq_usr_name');
    post.street = Inscription_GetValue('textreq_usr_street');
    post.nr = Inscription_GetValue('textreq_usr_nr');
    post.box = Inscription_GetValue('text_usr_box');
    post.zipcode = Inscription_GetValue('textreq_usr_zip');
    post.city = Inscription_GetValue('textreq_usr_city');
    post.country = Inscription_GetValue('select_usr_country');
    post.phonenr = Inscription_GetValue('text_usr_telnr');
    post.mobilenr = Inscription_GetValue('text_usr_gsmnr');
    post.gender = Inscription_GetValue('selectreq_usr_sex');
    
    var year, month, day;  
    year = Inscription_GetValue('selectreq_usr_birth_year');
    month = Inscription_GetValue('selectreq_usr_birth_month');
    day = Inscription_GetValue('selectreq_usr_birth_day');
    
    post.birthdate = Inscription_Right('0000' + '' + year, 4)
                            + Inscription_Right('00' + '' + month, 2)
                            + Inscription_Right('00' + '' + day, 2);
  
    post.opt_in_news = (Inscription_GetValue('cb_newsletter') == 1) ? '_on' : '_';
    post.opt_in_smb = (Inscription_GetValue('cb_smb') == 1) ? '_on' : '_';
    post.opt_in_partners = (Inscription_GetValue('cb_optinpartners') == 1) ? '_on' : '_';
    post.opt_out_dm = (Inscription_GetValue('cb_dmoptout') == 1) ? '_on' : '_';
    post.newslgcode = Inscription_GetValue('selectreq_usr_newslgcode');
    document.getElementById('hidden_qsuser').value = '&' + Inscription_BuildQueryStringFromObject(post);
    
    return true;
};