//---------------------------------------------------------------------------------------------------------------------------
function PopulateForm( theForm  )
{

  if ( GetCookie( "FirstName" ) != null )
    theForm.FirstName.value = GetCookie( "FirstName" );

  if ( GetCookie( "LastName" ) != null )
    theForm.LastName.value = GetCookie( "LastName" );

  if ( GetCookie( "Email" ) != null )
    theForm.Email.value = GetCookie( "Email" );

  if ( GetCookie( "PhoneNumber" ) != null )
    theForm.PhoneNumber.value = GetCookie( "PhoneNumber" );

}


//---------------------------------------------------------------------------------------------------------------------------
function ProcessPageSubmit( theForm ) 
{

  //1.  Validate the form.

  if ( theForm.FirstName.value == "" && cbRequiredFirstName ) 
  {
    alert( "Please enter your first name!" );
    theForm.FirstName.focus();
    return false;
  }

  if ( theForm.LastName.value == "" && cbRequiredLastName ) 
  {
    alert( "Please enter your last name!" );
    theForm.LastName.focus();
    return false;
  }

  if ( theForm.Email.value == "" && cbRequiredEmail ) 
  {
    alert( "Please enter your e-mail!" );
    theForm.Email.focus();
    return false;
  }

  if( !validEmail( theForm.Email.value ) && cbRequiredEmail ) 
  {
    alert("Please enter your valid e-mail address!");
    theForm.Email.focus();
    return false;
  }

  if ( theForm.PhoneNumber.value == "" && cbRequiredPhoneNumber ) 
  {
    alert( "Please enter your phone number!" );
    theForm.PhoneNumber.focus();
    return false;
  }


  //2.  Save the form values to cookies.

  SetCookie( "FirstName", theForm.FirstName.value );
  SetCookie( "LastName", theForm.LastName.value );
  SetCookie( "Email", theForm.Email.value );
  SetCookie( "PhoneNumber", theForm.PhoneNumber.value );


  //3.  Build the URL of the survey.

  var lsUrl = csCosmoUrl
            + "&ProcessId="       + csProcessId 
            + "&MicroSiteCity="   + escape( csCity ) 
            + "&MicroSiteState="  + escape( csState )
            + "&TemplatePage="    + escape( csTemplatePage )
            + "&ReplyPage="       + escape( csReplyPage )
            + "&FormName="        + escape( csFormName )
            + "&FirstName="       + escape( theForm.FirstName.value )
            + "&LastName="        + escape( theForm.LastName.value )
            + "&Email="           + escape( theForm.Email.value )
            + "&PhoneNumber="     + escape( theForm.PhoneNumber.value );


  //4.  Open the survey.

  window.location = lsUrl; 

}


//---------------------------------------------------------------------------------------------------------------------------
function ProcessPageSubmitPop( theForm ) 
{

  //1.  Validate the form.

  if ( theForm.FirstName.value == "" && cbRequiredFirstName ) 
  {
    alert( "Please enter your first name!" );
    theForm.FirstName.focus();
    return false;
  }

  if ( theForm.LastName.value == "" && cbRequiredLastName ) 
  {
    alert( "Please enter your last name!" );
    theForm.LastName.focus();
    return false;
  }

  if ( theForm.Email.value == "" && cbRequiredEmail ) 
  {
    alert( "Please enter your e-mail!" );
    theForm.Email.focus();
    return false;
  }

  if( !validEmail( theForm.Email.value ) && cbRequiredEmail ) 
  {
    alert("Please enter your valid e-mail address!");
    theForm.Email.focus();
    return false;
  }

  if ( theForm.PhoneNumber.value == "" && cbRequiredPhoneNumber ) 
  {
    alert( "Please enter your phone number!" );
    theForm.PhoneNumber.focus();
    return false;
  }


  //2.  Save the form values to cookies.

  SetCookie( "FirstName", theForm.FirstName.value );
  SetCookie( "LastName", theForm.LastName.value );
  SetCookie( "Email", theForm.Email.value );
  SetCookie( "PhoneNumber", theForm.PhoneNumber.value );


  //3.  Build the URL of the survey.

  var lsUrl = csCosmoUrl
            + "&ProcessId="       + csProcessId 
            + "&MicroSiteCity="   + escape( csCity ) 
            + "&MicroSiteState="  + escape( csState )
            + "&TemplatePage="    + escape( csTemplatePage )
            + "&ReplyPage="       + escape( csReplyPage )
            + "&FormName="        + escape( csFormName )
            + "&FirstName="       + escape( theForm.FirstName.value )
            + "&LastName="        + escape( theForm.LastName.value )
            + "&Email="           + escape( theForm.Email.value )
            + "&PhoneNumber="     + escape( theForm.PhoneNumber.value );


  //4.  Build the survey window properties.

  var lsWindowProps = "";

  lsWindowProps += csPopWindowLocation; 

  if ( lsWindowProps != "" )
    lsWindowProps += ","; 

  lsWindowProps += csPopWindowSize; 

  if ( lsWindowProps != "" )
    lsWindowProps += ","; 

  lsWindowProps += csPopWindowProperies ; 


  //5.  Pop the window with the survey.

  wind = window.open( lsUrl, "", lsWindowProps); 


  //6.  Open the survey in the page if the Pop-Up blocker got us.

  try
  { 
    wind.focus();
  } 
  catch( e )
  { 
    window.location = lsUrl; 
  } 

}


//---------------------------------------------------------------------------------------------------------------------------
function validEmail( Email ) 
{

  //1.  Does the email contain an @ sign.

  atpos = Email.indexOf( "@", 1 )

  if (atpos == -1) 
    return false;


  //2.  Does the email contain another @ sign.

  if (  Email.indexOf( "@", atpos + 1 ) != -1 )
    return false
	

  //3.  Does the email contain a period after the @ sign.

  periodPos = Email.indexOf( ".", atpos )

  if ( periodPos == -1 ) 
    return false;


  //4.  Does the email contain the proper number of characters after the period.

  if ( periodPos + 3 > Email.length )
    return false


  //5.  We made it, return true.
  return true;


}


//---------------------------------------------------------------------------------------------------------------------------
function SetCookie( asCookieName, asCookieValue ) 
{ 

  //1.  Expire the cookie in 1 year.
  var expdate = new Date();
  expdate.setTime ( expdate.getTime() + ( 24 * 60 * 60 * 1000 * 365 ) ); 

  //2.  Set The cookie.
  document.cookie = asCookieName + "=" + escape( asCookieValue ) + ";expires=" + expdate.toGMTString(); 

} 


//---------------------------------------------------------------------------------------------------------------------------
function GetCookie( name ) 
{ 

  //1.  Read the cookie string.

  var dc = document.cookie; 

  //2.  Get the index of the beginning of the cookie.

  var prefix = name + "="; 
  var begin = dc.indexOf("; " + prefix); 

  if (begin == -1) 
  { 
    begin = dc.indexOf(prefix); 
    if (begin != 0) 
      return null; 
  } 
  else 
  { 
    begin += 2; 
  } 

  //3.  Get the index of the end of the cookie.

  var end = document.cookie.indexOf(";", begin); 

  if (end == -1) 
  { 
    end = dc.length; 
  } 

  //4.  Return the cookie.

  return unescape(dc.substring(begin + prefix.length, end)); 

} 
