﻿// JScript File
function validate(lingua) {
 var i = 0, k = 0;
 var email = "", msgemail = "", msg = "";
 msg = "Dato obbligatorio.";
 msgemail = "L'indirizzo e-mail non e' valido.";
 
 switch(lingua) 
 {
   case "it":
        msg = "Dato obbligatorio.";
        msgemail = "L'indirizzo e-mail non e' valido.";
        break;
   case "de":
        msg = "Mußangabe.";
        msgemail = "Bitte die Daten vervollständigen.";
        break;
   case "en":
        msg = "Please, write your e-mail address.";
        msgemail = "Wrong e-mail address.";
        break;
   case "dk":
        msg = "Please, write your e-mail address.";
        msgemail = "Wrong e-mail address.";
        break;
   case "nl":
        msg = "Please, write your e-mail address.";
        msgemail = "Wrong e-mail address.";
        break;
       
 }
 

 for(i=0;i<document.forms[0].length;i++) {
 if(document.forms[0].elements[i].type == "text") {
   if(document.forms[0].elements[i].name.indexOf("txtEmail") != -1) {
      email = document.forms[0].elements[i].value;
      //alert(document.forms[0].elements[i].name + " - " + document.forms[0].elements[i].value);
       k = i;
   }
 }
} 

 if(trim(email) == "") {
    alert(msg);
    document.forms[0].elements[k].focus();
    return(false);
 }
 if(isEmail(email) == false || email.indexOf(" ") != -1) {
    alert(msgemail);
    //document.forms[0].elements[k].focus();
    return(false)
 }
 
 self.location = "http://www.unionlido.com/newsletter.aspx?email=" + email;
 return(true);
}


function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) {
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
    }
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,4})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

function depliantClientValidate(source, arguments)
{
  //alert("Sono qui. " + arguments.Value);
  var counter = 0;
  
  for(i=0;i<document.forms[0].length;i++) {
   if(document.forms[0].elements[i].type == "checkbox") {
     //alert("ecbcomi");
      if(document.forms[0].elements[i].checked) counter++;
   }
  }
  arguments.IsValid = (counter > 0);
}

