function checkRefresh() {
  // Display email_check field if necessary
  if (document.registration_form.email.value != '') {
    myShow('show_email_check');
    checkEmail();
  }

  // Check incorrrect fields
  if ((document.registration_form.email.value != '')
      || (document.registration_form.email_check.value != '')
      || (document.registration_form.username.value != '')
      || (document.registration_form.password.value != '')
      || (document.registration_form.password2.value != ''))
  {
    if (document.registration_form.email.value == '') {
      document.registration_form.email.style.border = '1px solid red';
    }
    
    if (document.registration_form.email_check.value == '') {
      document.registration_form.email_check.style.border = '1px solid red';
    }
    
    if (document.registration_form.username.value == '') {
      document.registration_form.username.style.border = '1px solid red';
    } else {
      CheckUser(document.registration_form.username);
    }
    
    if (document.registration_form.password.value == '') {
      document.registration_form.password.style.border = '1px solid red';
    } else {
      checkPass();
    }
    
    if (document.registration_form.password2.value == '') {
      document.registration_form.password2.style.border = '1px solid red';
    } else {
      checkPass();
    }
    
    if (document.registration_form.pin_value.value == '') {
      document.registration_form.pin_value.style.border = '1px solid red';
    } else {
      checkPIN();
    }
  }

}

function form_submit() {
  document.registration_form.operation.value = '';
  document.registration_form.edit_refresh.value = '1';
  document.registration_form.submit();
}

function copy_email_value() {
  document.registration_form.email_check.value = document.registration_form.email.value;
}

function copy_password_value() {
  document.registration_form.password2.value = document.registration_form.password.value;
}

function CheckUser(id) 
{ 
   var xmlHttp; 
    
   if (window.XMLHttpRequest) { 
         var xmlHttp = new XMLHttpRequest(); 
          
         if (XMLHttpRequest.overrideMimeType) { 
               xmlHttp.overrideMimeType('text/xml'); 
         } 
      } 
       
      else if (window.ActiveXObject) { 
       
         try { 
            var xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
         } 
         catch(e) { 
            try { 
               var xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
            } 
            catch(e) {} 
         } 
      } 
       
      else { 
         alert('Your browser does not support XMLHTTP!'); 
         return false; 
      } 
       
   var uName    = id.value; 
   var Url    = 'register_username_check.php'; 
   var Action    = Url + '?username=' + uName; 
    
   if (uName == '') { 
      document.getElementById('available').style.display = 'none';
      document.getElementById('not_available').style.display = 'none';
      document.registration_form.username.style.border = '';
      document.registration_form.username_img.style.display = 'none';
   } 
   else { 
      xmlHttp.onreadystatechange = function() { showResult(xmlHttp, id); }; 
      xmlHttp.open("GET", Action, true); 
      xmlHttp.send(null); 
   } 
} 
 
function showResult(xmlHttp, id) 
{ 
   if (xmlHttp.readyState == 4) 
   { 
      var response = xmlHttp.responseText;
       
      if (response == '1') { 
         document.getElementById('available').style.display = 'none';
         document.getElementById('not_available').style.display = 'block'; 
         document.registration_form.username.style.border = '1px solid red';
         document.registration_form.username_img.style.display = 'none';
      } 
      else { 
         document.getElementById('not_available').style.display = 'none';
         document.getElementById('available').style.display = 'block';
         document.registration_form.username.style.border = '1px solid green';
         document.registration_form.username_img.style.display = 'inline ';
      } 
   } 
} 

function isInteger(s) {
  var i;
  for (i = 0; i < s.length; i++)
  {   
      var c = s.charAt(i);
      if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}


function myShow(id) {
  document.getElementById(id).style.display = '';
}	

function checkEmail() {
  var email = document.registration_form.email;
  var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (filter.test(email.value)) {
    document.registration_form.email_img.style.display = 'inline';
    document.registration_form.email.style.border = '1px solid green';
    if (document.registration_form.email_check.value == document.registration_form.email.value) {     
      document.registration_form.email_check_img.style.display = 'inline';
      document.registration_form.email_check.style.border = '1px solid green';
    } else {
      document.registration_form.email_check_img.style.display = 'none';
      document.registration_form.email_check.style.border = 'none';
      if (document.registration_form.email_check.value.length >= document.registration_form.email.value.length) {
        document.registration_form.email_check.style.border = '1px solid red';
      }
    }
  } else {
    document.registration_form.email_img.style.display = 'none';
    document.registration_form.email.style.border = '1px solid red';
  }
}

function checkPass() {
  var pass1 = document.registration_form.password.value;
  if (pass1.length > 5) {
    document.registration_form.password_img.style.display = 'inline';
    document.registration_form.password.style.border = '1px solid green';
    if (document.registration_form.password.value == document.registration_form.password2.value) {
      document.registration_form.password2_img.style.display = 'inline';
      document.registration_form.password2.style.border = '1px solid green';
    } else {
      document.registration_form.password2_img.style.display = 'none';
      document.registration_form.password2.style.border = 'none';
      if (document.registration_form.password2.value.length >= document.registration_form.password.value.length) {
        document.registration_form.password2.style.border = '1px solid red';
      }
    }
  } else {
    document.registration_form.password_img.style.display = 'none';
    document.registration_form.password.style.border = '1px solid red';
  }
}

function checkPIN() {
  var pin = document.registration_form.generated_pin.value;
  var mypin = document.registration_form.pin_value.value;

  if (pin == mypin) {
    document.registration_form.pin_value_img.style.display = 'inline';
    document.registration_form.pin_value.style.border = '1px solid green';
  } else {
    document.registration_form.pin_value_img.style.display= 'none';
    if (mypin.length >= pin.length) {
      document.registration_form.pin_value.style.border = '1px solid red';
    }
  }
}

function checkName() {
  var s = document.registration_form.name.value;
  var i;
  var hasspace;
  
  for (i = 0; i < s.length; i++)
  {
      // Check that current character is a space.
      var c = s.charAt(i);
      if (c == " ")
        hasspace = true;
  }
  if (s.length > 4) {
    document.registration_form.name_img.style.display="inline";
    document.registration_form.name.style.border = '1px solid green';
  } else {
    document.registration_form.name_img.style.display="none";
    document.registration_form.name.style.border = '1px solid red';
  }
}

function checkPhone() {
  var phone = document.registration_form.phone_b.value;
  if ((isInteger(phone_b.value)) && (phone_b.value.length > 9)) {
    document.registration_form.phone_img.style.display="inline";
    document.registration_form.phoneb_img.style.display="inline";
  } else {
    document.registration_form.phone_img.style.display="none";
    document.registration_form.phoneb_img.style.display="none";
  }
}

function checkOffer() {
  var offer1 = document.getElementById('offer1');
  if (document.ad_create_form.is_offer.checked == false) {
    offer1.style.display = 'none';
  } else {
    offer1.style.display = '';
  }
}