/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var xmlHttp = createXmlHttpRequestObject();
var whitespace = " ";
var type_state = '';
var g_profile = '';

function createXmlHttpRequestObject(){
    if (window.XMLHttpRequest) {
        if (navigator.userAgent.indexOf('MSIE') != -1) {
            xmlHttp = true;
        }
        return new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        xmlHttp = true;
        return new ActiveXObject("Microsoft.XMLHTTP");
    }

}

// function to get the state information
function StateInformation(place)
{
    if(place == 'MyProfile'){
        g_profile = 'MyProfile';
    }
    type_state = place;
    //alert("state");
    var url = '';
    url = '../includes/xml/state_info.php';
    document.getElementById('state_info_'+place).innerHTML = '<img src="images/ajax-loader.gif" />';
    if (xmlHttp)
    {
        // try to connect to the server
        try
        {
          if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0))
          {
            xmlHttp.open("GET", url, true);
            xmlHttp.onreadystatechange =  handleStateInfo;
            xmlHttp.send(null);
          }
        }
        // display the error in case of failure
        catch (e)
        {
            alert("Can't connect to server:\n" + e.toString());
        }
    }
}

// function to load xml file
function handleStateInfo()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // do something with the response from the server
        handleStateInfoResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" +
            xmlHttp.statusText);
    }
  }
}

// function to display state information
function handleStateInfoResponse()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement)
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror")
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;

  var state_name = '';
  var state_name_total = '';
  state_name_total = xmlRoot.getElementsByTagName("state-name-total")[0].firstChild.data;

  if(state_name == '-')
  {
      //document.getElementById('error_mess').innerHTML = '';
  }
  else
  {
    var stateinfo = '';

    // loop to check for gallery on messages with pagin
    var name = '';
    name = 'Land01.php';

    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    var cond = '';
    var land_state = '';

    strHref = window.location.href;
    if ( ( strHref.indexOf("Land01.php") > -1 ) || ( strHref.indexOf("land01.php") > -1 ) ){
        land_state  = 'T';
    }else{
        land_state  = 'F';
    }

    if(type_state == 'FreeJoin'){
        stateinfo = stateinfo + '<select name="state_option"  onchange="CityInfo();">';
        stateinfo = stateinfo + '   <option value="State" selected="selected">Select a State</option>';
    }else if(type_state == 'LandFreeJoin' && land_state == 'T'){
        stateinfo = stateinfo + '<select name="state_option"  onchange="CityInfo();">';
        stateinfo = stateinfo + '   <option value="State" selected="selected">Select a State</option>';
    }else if(type_state == 'MyProfile'){
        stateinfo = stateinfo + '<select name="state_option" class="Form-Text-Field"  onchange="CityInfo();">';
        stateinfo = stateinfo + '   <option value="State"   selected="selected">Change Your State</option>';
    }else{
        stateinfo = stateinfo + '<select name="state_option"  class="Form-Search" onchange="CityInfo();">';
        stateinfo = stateinfo + '   <option value="State" selected="selected">Select a State</option>';
    }
    
//    stateinfo = stateinfo + '   <option value="State" selected="selected">Select a State</option>';
    var city_count = new Array;
    var state_number = new Array();
    
    for (var i=0; i< state_name_total; i++)
    {
        city_count = xmlRoot.getElementsByTagName("state-name").item(i).firstChild.data.split('_');
        stateinfo = stateinfo + '   <option value="'+city_count[2]+'_'+city_count[0]+'" >'+city_count[0]+'</option>';
    }
    stateinfo = stateinfo + '</select>';
    document.getElementById('state_info_'+type_state).innerHTML = stateinfo;
    
    if(type_state == 'MyProfile'){
        StateInformation('QuickSearch');
    }

    if(type_state == 'FreeSearch' && land_state == 'T'){
        StateInformation('LandFreeJoin');
    }

  }
  if(type_state == 'QuickSearch' && type_state != 'FreeSearch'){
       //this the function which does name search
      var uid = '';
      var ccredit = '';
      
      init();
      
      //alert("current user id - "+document.getElementById('current_userid').value);
      ccredit = document.getElementById('current_userid').value
      //if(ccredit == 0){
        uid = document.getElementById('current_userid').value;
        CheckCredentials(uid);
      //}
  }
}

// function to load city information
function CityInfo() {
   var w = document.join.state_option.selectedIndex;
   var selected_text = document.join.state_option.options[w].value;
   selected_text = selected_text.split('_');

   if(selected_text == 'State'){
        var stateinfo = '';
        stateinfo = stateinfo + '<select name="City_option" class="Form-Text-Field">';
        stateinfo = stateinfo + '   <option value="City" selected="selected">City</option>';
        stateinfo = stateinfo + '</select>';
        document.getElementById('city_info').innerHTML = stateinfo;
   }
   else {
       selected_text = parseInt(selected_text[0]);
       var url = '';
       var params = '';
       url = '../includes/xml/city_info.php';
       params = 'state_id='+selected_text;
       if (xmlHttp){
        // try to connect to the server
        try
        {
          if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.setRequestHeader("Content-length", params.length);
            xmlHttp.setRequestHeader("Connection", "close");
            xmlHttp.onreadystatechange =  handleCityInfo;
            xmlHttp.send(params);
          }
        }
        // display the error in case of failure
        catch (e)
        {
            alert("Can't connect to server:\n" + e.toString());
        }
       }
   }
}

function handleCityInfo()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // do something with the response from the server
        handleCityInfoResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" +
            xmlHttp.statusText);
    }
  }
}

function handleCityInfoResponse()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement)
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror")
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;

  var stateinfo = '';
  var city_name_total = '';
  var town_name_total = '';
  
  city_name_total = xmlRoot.getElementsByTagName("city-name-total")[0].firstChild.data;
  town_name_total = xmlRoot.getElementsByTagName("town-name-total")[0].firstChild.data;

  if((city_name_total == '-') || (town_name_total == "-"))
  {
    stateinfo = stateinfo + '<select name="City_option" class="Form-Text-Field"  onchange="TownInfo();">';
    stateinfo = stateinfo + '   <option value="City" selected="selected">No-City</option>';
    stateinfo = stateinfo + '</select>';
    //document.getElementById('city_info_'+type_state).innerHTML = stateinfo;
    if(g_profile == 'MyProfile'){
        document.getElementById('city_info_MyProfile').innerHTML = stateinfo;
    }else{
        document.getElementById('city_info_'+type_state).innerHTML = stateinfo;
    }

    /*
    var statinfo = '';
    statinfo = statinfo + '<select name="Town_option" class="Form-Text-Field">';
    statinfo = statinfo + '   <option value="Town" selected="selected">No-Town</option>';
    statinfo = statinfo + '</select>';
    document.getElementById('town_info').innerHTML = statinfo;
    */
  }
  else
  {
    stateinfo = stateinfo + '<select name="City_option" class="Form-Text-Field">';
    stateinfo = stateinfo + '   <option value="City" selected="selected">Select a City</option>';

    var city_count = new Array;

    for (var i=0; i< city_name_total; i++)
    {
        city_count = xmlRoot.getElementsByTagName("city-name").item(i).firstChild.data.split('_');
        stateinfo = stateinfo + '   <option value="'+city_count[1]+'_'+city_count[0]+'" >'+city_count[0]+'</option>';
    }
    stateinfo = stateinfo + '</select>';
    if(g_profile == 'MyProfile'){
        document.getElementById('city_info_MyProfile').innerHTML = stateinfo;
    }else{
        document.getElementById('city_info_'+type_state).innerHTML = stateinfo;
    }
    /*
    var stateinfo1 = '';
    stateinfo1 = stateinfo1 + '<select name="Town_option" class="Form-Text-Field">';
    stateinfo1 = stateinfo1 + '   <option value="Town" selected="selected">Town</option>';

    for (var i=0; i< town_name_total; i++)
    {
        city_count = xmlRoot.getElementsByTagName("town-name").item(i).firstChild.data;
        stateinfo1 = stateinfo1 + '   <option value="'+i+'_'+city_count+'" >'+city_count+'</option>';
    }
    stateinfo1 = stateinfo1 + '</select>';
    document.getElementById('town_info').innerHTML = stateinfo1;
    */
  }
  
}

/*
// function to get town information
function TownInfo()
{
   var w = document.join.state_option.selectedIndex;
   var selected_text = document.join.state_option.options[w].value;
   selected_text = selected_text.split('_');
   
   if(selected_text == 'State')
   {
        // we restrict an error message
        var stateinfo = '';
        stateinfo = stateinfo + '<select name="City_option" class="Form-Text-Field">';
        stateinfo = stateinfo + '   <option value="City" selected="selected">City</option>';
        stateinfo = stateinfo + '</select>';
        document.getElementById('town_info').innerHTML = stateinfo;

   }
   else
   {
       selected_text = parseInt(selected_text[0])+1;
       var url = '';
       var params = '';
       url = '../includes/xml/town_info.php';
       params = 'city_id='+selected_text[0];
       if (xmlHttp)
       {
        // try to connect to the server
        try
        {
          if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0))
          {
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.setRequestHeader("Content-length", params.length);
            xmlHttp.setRequestHeader("Connection", "close");
            xmlHttp.onreadystatechange =  handleTownInfo;
            xmlHttp.send(params);
          }
        }
        // display the error in case of failure
        catch (e)
        {
            alert("Can't connect to server:\n" + e.toString());
        }
       }
   }
}

function handleTownInfo()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // do something with the response from the server
        handleTownInfoResponse();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" +
            xmlHttp.statusText);
    }
  }
}

function handleTownInfoResponse()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement)
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror")
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;

  var stateinfo = '';
  var town_name_total = '';
  town_name_total = xmlRoot.getElementsByTagName("town-name-total")[0].firstChild.data;

  if(town_name_total == '-')
  {
    stateinfo = stateinfo + '<select name="Town_option" class="Form-Text-Field">';
    stateinfo = stateinfo + '   <option value="Town" selected="selected">No-Town</option>';
    stateinfo = stateinfo + '</select>';
    document.getElementById('town_info').innerHTML = stateinfo;
  }
  else
  {
    stateinfo = stateinfo + '<select name="Town_option" class="Form-Text-Field">';
    stateinfo = stateinfo + '   <option value="Town" selected="selected">Town</option>';

    var city_count = new Array;

    for (var i=0; i< town_name_total; i++)
    {
        city_count = xmlRoot.getElementsByTagName("town-name").item(i).firstChild.data.split('_');
        stateinfo = stateinfo + '   <option value="'+i+'_'+city_count[0]+'" >'+city_count[0]+'</option>';
    }
    stateinfo = stateinfo + '</select>';
    document.getElementById('town_info').innerHTML = stateinfo;
  }
}


function Check_Validate()
{
     if((document.getElementById('Name').value) == 'NULL')
     {
         document.getElementById('error_mess_name').innerHTML = 'Please Enter Alias Name.';
         return false;
     }
     else
     {
         document.getElementById('error_mess_name').innerHTML = '';
         return true;
     }

     if( document.join.state_option.options[w].value == 'State' )
     {
         document.getElementById('error_mess_state').innerHTML = 'Please Select a State';
         return false;
     }
     else
     {
         document.getElementById('error_mess_state').innerHTML = '';
         return true;
     }

     if( document.join.City_option.options[w].value == 'City' )
     {
         document.getElementById('error_mess_city').innerHTML = 'Please Select a City';
         return false;
     }
     else
     {
         document.getElementById('error_mess_city').innerHTML = '';
         return true;
     }
}
*/

// this is the function which validates the search form for members and free site as well
function SearchValidate(){
    var w = document.next.state_option.selectedIndex;
    var selected_text = document.next.state_option.options[w].value;
    selected_text = selected_text.split('_');
    
    if(selected_text == 'State'){
        alert("Please Select a State Option:-");
        return false;
    }else{
        return true;
    }
}

// this is the function which validates the search form for members and free site as well
function SearchValidateQuickSearch(){
    var w = document.nextquick.state_option.selectedIndex;
    var selected_text = document.nextquick.state_option.options[w].value;
    selected_text = selected_text.split('_');

    if(selected_text == 'State'){
        alert("Please Select a State Option:-");
        return false;
    }else{
        return true;
    }
}

// this is the function which validates the advanced search form for members and free site as well
function ValidateADVSearch(){
    var w = document.adv_search.Form_Seeking.selectedIndex;
    var selected_text = document.adv_search.Form_Seeking.options[w].value;

    if(selected_text == 'FS'){
        alert("Please Select Seeking Option:-");
        return false;
    }else{
        return true;
    }
}

var urlcc = '';
urlcc = '../includes/xml/checkcredentials.php';
function CheckCredentials(userid){
    var params = '';
    uid = userid;
    params = "memberid="+userid;
    if (xmlHttp){
        // try to connect to the server
        try
        {
          if ((xmlHttp.readyState == 4 || xmlHttp.readyState == 0)){
            xmlHttp.open("POST", urlcc, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.setRequestHeader("Content-length", params.length);
            xmlHttp.setRequestHeader("Connection", "close");
            xmlHttp.onreadystatechange =  handleCheckCredentialsInfo;
            xmlHttp.send(params);
          }
        }
        // display the error in case of failure
        catch (e)
        {
            alert("Can't connect to server:\n" + e.toString());
        }
   }
}

// function to load xml file
function handleCheckCredentialsInfo()
{
  // when readyState is 4, we are ready to read the server response
  if (xmlHttp.readyState == 4)
  {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200)
    {
      try
      {
        // do something with the response from the server
        handleCheckCredentialsResponseInfo();
      }
      catch(e)
      {
        // display error message
        alert("Error reading the response herer: " + e.toString());
      }
    }
    else
    {
      // display status message
      alert("There was a problem retrieving the data:\n" +
            xmlHttp.statusText);
    }
  }
}

// function to display state information
function handleCheckCredentialsResponseInfo()
{
  // read the message from the server
  var xmlResponse = xmlHttp.responseXML;
  // catching potential errors with IE and Opera
  if (!xmlResponse || !xmlResponse.documentElement)
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // catching potential errors with Firefox
  var rootNodeName = xmlResponse.documentElement.nodeName;
  if (rootNodeName == "parsererror")
    throw("Invalid XML structure:\n" + xmlHttp.responseText);
  // obtain the XML's document element
  xmlRoot = xmlResponse.documentElement;

  var sucess_cuser = '';
  var check = '';
  var check_nm = '';
  var check_winks = '';
  var nomess = '';
  var nowinks = '';
  var nobroadcast = '';
  var three_blocks = '';
  var cnt_obj = '';
  var broadcast_winks = new Array();
  var broadcast_nm = new Array();
  var broadcast_data = new Array();
  var height_1 = '142px';
  var height_2 = '284px';
  var height_neg = '-241px';
  var speed_1 = 1500;
  var speed_2 = 3500;
  var speed_3 = 5500;
  var speed_down = 250;

  sucess_cuser = xmlRoot.getElementsByTagName("operation-succ")[0].firstChild.data;
  //alert(sucess_cuser);
}
