//---------------------------------------------------------------------------------------------------------------------------
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
            + "&PropertyReference=" + escape( "MLS No. " + csMLSId )
            + "&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; 
  // alert(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;


}


