﻿function showHideAdresData(e)
{
var addQuestions = document.getElementById("addQuestions");
if(e.value=="hide"){addQuestions.style.display="none";}else{addQuestions.style.display="block";}
}

function showHideCountry(e)
{
var addQuestions = document.getElementById("showCountry");
if(e.value=="hide"){addQuestions.style.display="none";}else{addQuestions.style.display="block";}
}

function validateInput(lang)
{
    var valid = true;
    if(Trim(document.getElementById("txt_Mail").value) == "")
    {
        document.getElementById("txt_Mail").focus();
        valid = false;
    }
    if(document.getElementById("ddl_TypeQuestion").value == "show")
    {
        if(document.getElementById("ddl_Country").value == "show")
        {
            if(Trim(document.getElementById("txt_Country").value) == "")
            {
                document.getElementById("txt_Country").focus();
                valid = false;
            }
        }    
        if(Trim(document.getElementById("txt_City").value) == "")
        {
            document.getElementById("txt_City").focus();
            valid = false;
        }    
        if(Trim(document.getElementById("txt_PostalCode").value) == "")
        {
            document.getElementById("txt_PostalCode").focus();
            valid = false;
        }    
        if(Trim(document.getElementById("txt_Number").value) == "")
        {
            document.getElementById("txt_Number").focus();
            valid = false;
        }     
        if(Trim(document.getElementById("txt_Street").value) == "")
        {
            document.getElementById("txt_Street").focus();
            valid = false;
        }
    }  
    if(Trim(document.getElementById("txt_FirstName").value) == "")
    {
        document.getElementById("txt_FirstName").focus();
        valid = false;
    }    
    if(Trim(document.getElementById("txt_Name").value) == "")
    {
        document.getElementById("txt_Name").focus();
        valid = false;
    }   
    
    if(!valid)
    {
        var alertMsg;
        switch(lang)
        {
            case 0: // English
            alertMsg = "Please fill all mandatory fields.";
            break;
            
            case 1: // French
            alertMsg = "Veuillez remplir les champs obligatoires.";
            break;
            
            case 2: // Dutch
            default:            
            alertMsg = "Gelieve alle verplichte velden in te vullen.";
            break;
        }
        alert(alertMsg);
    }
    else
    {
        // Validate email address
        valid = echeck(Trim(document.getElementById("txt_Mail").value));
        if (!valid)
        {
            var alertMsg;
            switch(lang)
            {
                case 0: // English
                alertMsg = "Please enter a valid email address.";
                break;
                
                case 1: // French
                alertMsg = "Veuillez écrire un email address valide.";
                break;
                
                case 2: // Dutch
                default:            
                alertMsg = "Gelieve te gaan een geldig e-mailadres in.";
                break;
            }
            alert(alertMsg);
        }
    }
    return valid;
}

function Trim(s) 
   {
   	// Remove leading spaces and carriage returns
   	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
   	 { s = s.substring(1,s.length); }
     
   	// Remove trailing spaces and carriage returns
 while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
   	 { s = s.substring(0,s.length-1); }
     
   	return s;
}


function echeck(str)
{

  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
     return false
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     return false
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      return false
  }

   if (str.indexOf(at,(lat+1))!=-1){
      return false
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
      return false
   }

   if (str.indexOf(dot,(lat+2))==-1){
      return false
   }
  
   if (str.indexOf(" ")!=-1){
      return false
   }

    return true     
 }
