﻿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 setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+";path=/"+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function checkRegistration(url){
  var theForm=document.forms['Registration'];

  //If registerred, submit the form and download the file
  if (getCookie("ADVName")){

    //Fill and submit the form
    theForm.Name.value=getCookie("ADVName");
    theForm.Title.value=getCookie("ADVTitle");
    theForm.Organization.value=getCookie("ADVOrganization");
    theForm.Email.value=getCookie("ADVEmail");
    theForm.Phone.value=getCookie("ADVPhone");
    theForm.redirect.value=url;
    theForm.Resource.value=url;
    theForm.submit();

    //Open the url in the new window
    //window.location.replace(url);
  }
  
  //Otherwise, register
  else{
    //Set the redirect URL
    theForm.redirect.value=url;
    
    //Show the form
    document.getElementById('RegistrationDiv').style.visibility="visible";
  }
}


function validate_required(field,alerttxt){
with (field){
  if (value==null||value==""){
    alert(alerttxt);return false;
  }
  else{
  return true;
  }
}
}


function validate_email(field,alerttxt){
with (field){
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {alert(alerttxt);return false;}
  else {return true;}
  }
}


function FormValidator(theForm){
with (theForm){
  if (validate_required(Name,"A name is required!")==false)
    {Name.focus();return false;}
  else if (validate_required(Title,"A title is required!")==false)
    {Title.focus();return false;}
  else if (validate_required(Organization,"An organization is required!")==false)
    {Organization.focus();return false;}
  else if (validate_required(Email,"An email address is required!")==false)
    {Email.focus();return false;}
  else if(validate_email(Email,"Please provide a valid email address!")==false)
    {Email.focus();return false;}
    
  //Set the resource field to be the value of the redirect field
  Resource.value=redirect.value;
}

  //Set cookies
  setCookie("ADVName",theForm.Name.value,90);
  setCookie("ADVTitle",theForm.Title.value,90);
  setCookie("ADVOrganization",theForm.Organization.value,90);
  setCookie("ADVEmail",theForm.Email.value,90);
  setCookie("ADVPhone",theForm.Phone.value,90);
  
  //Hide the form
  document.getElementById('RegistrationDiv').style.visibility="hidden";
  
  return (true);
}
