//////////////////////////////////////////////////////////////////////////////////////////////////////////////

//lda functions called by flash app 
function switchLang(lang) {
        //set cookie to language
		setCookie('language',lang,'1');
        document.location.reload(true);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

<!--
function popupwindow(url, name) {
	var newwindow = window.open(url,name,'height=500,width=650,scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}

// -->

function setCookie (name, value, days) {
 
    var today = new Date();
    var expires = new Date( today.getTime()
 
         + (days * 1000 * 60 * 60 * 24) );
 
     document.cookie = name + '=' + escape(value) + ";";
	 
	 if(days!=-1)
	 {
      document.cookie = document.cookie + " expires=" + expires.toGMTString()+";";
	 }
 
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}


 function signupVal() {
	this.emailaddress = document.getElementById('email');
	this.firstname = document.getElementById('firstName');
	this.lastname = document.getElementById('lastName');
	this.address = document.getElementById('address');
	this.city = document.getElementById('city');
	this.province = document.getElementById('province');
	this.postalcode = document.getElementById('postal1');
	this.postalcode2 = document.getElementById('postal2');
	this.dobmonth = document.getElementById('dob_month');
	this.dobday = document.getElementById('dob_day');
	this.dobyear = document.getElementById('dob_year_short');
	this.lda = document.getElementById('lda');
	this.errors = document.getElementById('errors');
	this.inputerrors = false;
 
 	this.verify = function() {
     
	 signup.clear_errors();
     
	 var input = null;
     
	 input = signup.emailaddress;
	 if (!verify_emailaddress(input)) {
        if (!signup.inputerrors) {
          input.select();
          signup.inputerrors = true;
        }
      }
  		 input = signup.firstname;
		 	if (input.value=='') {
			input_error(input);
   
			if (!signup.inputerrors) {
					input.focus();
					signup.inputerrors = true;
				}
			}
	  input = signup.lastname;
     		if (input.value=='') {
					input_error(input);
		if (!signup.inputerrors) {
					input.focus();
					signup.inputerrors = true;
				}
			}
	  input = signup.address;
  		if (input.value=='') {
				input_error(input);
				if (!signup.inputerrors) {
					input.focus();
					signup.inputerrors = true;
				}
			}
	  input = signup.city;
     		if (input.value=='') {
					input_error(input);
		if (!signup.inputerrors) {
					input.focus();
					signup.inputerrors = true;
				}
			}
      input = signup.postalcode;
      input2 = signup.postalcode2;
      if (!verify_postalcode(input.value+input2.value)) {
			input_error(input);
	 		input_error(input2);
	 
	 		if (!signup.inputerrors) {
         	 input.focus();
          	signup.inputerrors = true;
        	}
      }
	 
	  
   input = signup.province;
      if (input.selectedIndex==0) {	
        input_error(input);
        if (!signup.inputerrors) {
          input.focus();
          signup.inputerrors = true;
        }
      }
	  	
				input = signup.dobmonth;
			if (!verify_month(input)) {
				if (!signup.inputerrors) {
					input.focus();
					signup.inputerrors = true;
				}
			}
			input = signup.dobday;
			if (!verify_day(input)) {
				if (!signup.inputerrors) {
					input.focus();
					signup.inputerrors = true;
				}
			}
			input = signup.dobyear;
			if (!verify_year(input)) {
				if (!signup.inputerrors) {
					input.select();
					signup.inputerrors = true;
				}
			}
			input = signup.lda;
			if (!input.checked) {
			   input_error(input);
     			signup.errors.innerHTML="You must confirm that you are of legal drinking age."
				if (!signup.inputerrors) {
					input.select();
					signup.inputerrors = true;
				}
			}
			
			if (!signup.inputerrors) {
				var now = new Date();
				var months = (now.getFullYear() - parseInt('19' + signup.dobyear.value)) * 12 + ((now.getMonth() + 1) - parseInt(signup.dobmonth.value));
				if (months < 19 * 12 || (months == 19 * 12 && now.getDate() < parseInt(signup.dobday.value))) {
					signup.errors.innerHTML = 'You must be legal drinking age to enter.';
					input_error(signup.dobyear);
					signup.inputerrors = true;
				}
			}
			
			
      if (signup.inputerrors) {
        return false;
      }
   
	
    }
    this.clear_errors = function() {
      signup.emailaddress.style.backgroundColor = '';
      signup.postalcode.style.backgroundColor = '';
      signup.dobmonth.style.backgroundColor = '';
      signup.dobday.style.backgroundColor = '';
      signup.dobyear.style.backgroundColor = '';
	  signup.firstname.style.backgroundColor = '';
	  signup.lastname.style.backgroundColor = '';
signup.address.style.backgroundColor = '';
 signup.city.style.backgroundColor = '';
   signup.province.style.backgroundColor = '';
     signup.postalcode2.style.backgroundColor = '';
     signup.lda.style.backgroundColor = '';
      signup.inputerrors = false;
    }
  }
  function verify_emailaddress(input) {
    if (!input.value.match(/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}$/)) {
      input_error(input);
      return false;
    }
    return true;
  }

  function verify_postalcode(input) {
	    expression = /^\s*[a-z]\d[a-z](\s)?\d[a-z]\d\s*$/i;
                  
    if (!input.match(expression)) {
      return false;
    }
    return true;
  }
	function verify_month(input) {
		if (!input.value.match(/^(0?[1-9]|1[0-2])$/)) {
			input_error(input);
			return false;
		}
		return true;
	}
	function verify_day(input) {
		if (!input.value.match(/^(0?[1-9]|[1-2]\d|3[0-1])$/)) {
			input_error(input);
			return false;
		}
		return true;
	}
	function verify_year(input) {
		if (!input.value.match(/^\d\d$/)) {
			input_error(input);
			return false;
		}
		return true;
	}
  function input_error(input) {
    input.style.backgroundColor = '#DDA242';
  }
  
 
              
function validateForm() {

                  var theForm = document.signUpForm;
                  var isComplete = true;
                  var isLDA = false;
                  var errorMessage = "";
                  theForm.dob_year.value = '19'+theForm.dob_year_short.value;
                  
		var values = new Array();
    if (theForm.budOffers.checked) {
      values[values.length] = 1;
    }
	//if (theForm.NFLOffers.checked) {
   //   values[values.length] = 2;
    //}
    if (theForm.isLDA.checked) {
      values[values.length] = 1;
    }
    theForm.promoids.value = values.join(',');

if (theForm.lang.value=='en') { 
errPostal = "Please enter a valid postal code.\n";
errEmail = "Please enter a valid email address.\n";
errPin = "There is a problem with your PIN format. Please try again.\n";
errDay="You must enter a valid day.\n";
errYear="You must be of legal drinking age.\n";
errLDA="You must be of legal drinking age.\n";
errMandatory="Please fill all mandatory form fields.\n";
errProblem="There is a problem with your entry. Please try again.\n";
}
else {
errPostal = "SVP entrer un code postal valide.\n";
errEmail = "SVP entrer une adresse de courriel valide.\n";
errPin = "Il y a un problème avec le format du NIP. Essaie de nouveau.\n";
errDay="Veuillez entrer un jour valide.\n";
errYear="Veuillez entrer une année valide.\n";
errLDA="Tu dois avoir l'age légal consommer de l'alcool.\n";
errMandatory="SVP remplir tous les champs obligatoires.\n";
errProblem="Il y a un problème avec ton inscription. Essaie de nouveau.\n";
}
var myDate=new Date();
var dob_check=new Date();
var today=new Date();

                  if (
                      theForm.firstName.value == "" ||
                      theForm.lastName.value == "" ||
                      theForm.email.value == "" ||
                      theForm.SEX.value == "" ||
                      theForm.dob_day.value == "" ||
                      theForm.dob_month.value == "" ||
                      theForm.dob_year_short.value == "" ||
                      theForm.Provincestate.value == "" ||
                      theForm.postalzip.value == "" ||
                      theForm.areaphone.value == "" ||
                      theForm.hphone1.value == "" ||
                      theForm.hphone2.value == "" || 
                      !theForm.isLDA.checked || 
                      !theForm.budOffers.checked
                  ) { isComplete = false; }

                    expression = /^\s*[a-z]\d[a-z](\s)?\d[a-z]\d\s*$/i;
                    if ((theForm.postalzip.value != "")&&(!expression.test(theForm.postalzip.value))) 
                    {
                        isComplete = false;
                        errorMessage += errPostal;
                    }

                    expression = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
                        if ((theForm.email.value != "")&&(!expression.test(theForm.email.value))) 
                        {
                            isComplete = false;
                            errorMessage += errEmail;
                        }
         
                    expression = /[0-9]{1,2}/;
                    if ((theForm.dob_day.value != "")&&(!expression.test(theForm.dob_day.value))) {
                        isComplete = false;
                        errorMessage += errDay;
                    } else {
						if (parseFloat(theForm.dob_day.value) > 31) {
					      isComplete = false;
                        errorMessage += errDay;
                   	
						}
					}
					
                    expression = /[0-9]{2}/;
                    if ((theForm.dob_year_short.value != "")&&(!expression.test(theForm.dob_year_short.value))) {
                        isComplete = false;
                        errorMessage += errYear;
                    }
                    
dob_check.setFullYear((theForm.dob_year.value),theForm.dob_month.value,theForm.dob_day.value);
dob_check.setFullYear(parseInt(theForm.dob_year.value) + parseInt(18));
myDate.setFullYear((theForm.dob_year.value),theForm.dob_month.value,theForm.dob_day.value);


                  if (isComplete) 
                  {
                    isLDA = theForm.isLDA.checked;
                  }
                  
                    if (!isComplete) 
                    {
                        errorMessage += errMandatory;
                        alert (errorMessage);
                    } 
                    else if (!isLDA) 
                    {
                        alert (errMandatory);
                    }
                    
                    
                   /* else if (dob_check.getFullYear()>=today.getFullYear()) 
                    {
                        isLDA = false;
                        alert ("You must be of legal drinking age");
                           
                    }
                    else if ((dob_check.getDate()>today.getDate())&&(dob_check.getMonth()-1>today.getMonth()))
                    {
                        isLDA = false;
                        alert ("You must be of legal drinking age");
                       
                    }
                    */
                    
                    return (isLDA && isComplete && isNineteenOrOlder());
              }
              
              function isNineteenOrOlder()
              {
                    var theForm = document.signUpForm;
                    var today=new Date();
                    var dob_check=new Date();
                    dob_check.setFullYear(theForm.dob_year.value,theForm.dob_month.value-1,theForm.dob_day.value );
                    
              
                    if(today.getFullYear() - dob_check.getFullYear() > 19)
                    {
                        return true;
                    }
                    else if(today.getFullYear() - dob_check.getFullYear() < 19)
                    {
                        alert (errLDA);
                        return false;
                    }
                    else
                    {
                        if(today.getMonth() < dob_check.getMonth())
                        {
                            alert (errLDA);
                            return false;
                        }
                        else if(today.getMonth() > dob_check.getMonth())
                        {
                            return true;
                        }
                        else
                        {
                            if(today.getDate() < dob_check.getDate())
                            {
                                alert (errLDA);
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        }
                    }
              }
              
            function gup(name) {
                name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
                var regexS = "[\\?&]"+name+"=([^&#]*)";
                var regex = new RegExp(regexS);
                var results = regex.exec( window.location.href );
                if(results == null)
                    return false;
                else
                    return results[1];
            }
                
            if (gup('problem')) { alert(errProblem); }
            
            function errorsToForm() {
                var theForm = document.signUpForm;
                
                theForm.firstName.value = gup('firstName').replace(/\+/g,' ');
                theForm.lastName.value = gup('lastName').replace(/\+/g,' ');
                theForm.email.value = gup('email');
                theForm.SEX.value = gup('SEX');
                theForm.dob_day.value = gup('dob_day');
                theForm.dob_month.value = gup('dob_month');
                theForm.dob_year_short.value = gup('dob_year_short');
                theForm.address.value = gup('address').replace(/\+/g,' ');
                theForm.city.value = gup('city').replace(/\+/g,' ');
                theForm.Provincestate.value = gup('Provincestate');
                theForm.postalzip.value = gup('postalzip').replace(/\+/g,' ');
                theForm.hphone.value = gup('hphone').replace(/\+/g,' ');
                //theForm.pin.value = gup('pin').replace(/\+/g,' ');
            }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
