// JavaScript Document

//Registeration form validation script

var http = ajaxFunction();



function validate_login(process) {



     var url = adminurl+"ajax_serverPage.php?case=directlogin&sid="+Math.random();

     var username = get_field_value('id_username');

     var userpass = document.login.password.value;

     var params = "username="+username+"&password="+userpass;

     

     if (process == 'activation') {

          var activate_code = get_field_value('id_activate');

          params += "&activate="+activate_code;

     }

     

     http.open("POST", url, true);

     

     //Send the proper header information along with the request

     http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

     http.setRequestHeader("Content-length", params.length);

     http.setRequestHeader("Connection", "close");

     

     http.onreadystatechange = function() {//Call a function when the state changes.

          

          if(http.readyState == 4 && http.status == 200) {

               

               if (http.responseText != "") {

                    //alert("return text:" +http.responseText);

                    var rtn_string = http.responseText.split("~~~");

                    var first_string = '';

                    var second_string =  '';



                    if (rtn_string.length==2) {

                     first_string = rtn_string[0];

                     second_string =  rtn_string[1];

                    }

                    if (first_string == 'property_view.php' || first_string  == 'profile.php') {

                         window.location.href = first_string;

                    }

                    else {

                         if (first_string == "3") {

                              var functionname = " redirect_page('login.php?activate=yes') " ;

                         }

                         else {

                              

                              var functionname = " close_popup_div('id_error_msg'); ";

                         } 

                         

                         

                    	var html = "<table width=\"100%\" border=\"0\" cellpadding=\"3\" align=\"center\" class=\"smalltext3\">"

                             html += "  <tr> ";

                             html += "    <td align=\"left\" bgcolor='#000099'> ";

                             html += "      <span style='font-family:verdana; color:yellow;'>";

                             html += "  <b>Login Failed</b> ";

                             html += "      </span> ";

                             html += "    </td>";

                             html += "  </tr> ";

                             html += "  <tr> ";

                             html += "    <td align=\"center\"> ";

                             html += "      <span class=\"alert2\">";

                             html +=          second_string;

                             html += "      </span> ";

                             html += "      </br></br> <input type='button' id='id_OK_message' onclick=\""+functionname+"\" value='OK'></br>";

                             html += "    </td> ";

                             html += "  </tr> ";

                             html += "</table> ";

                              form_cover();

                             show_element('id_error_msg');

                             inner_html('id_error_msg',html);

                             //document.getElementById('in_form_cover').innerHTML = html;

                    

                    }

               }

               else {

                         window.location.href = "index.php";

               }

          }

     }

     http.send(params);

     return false;

}



function check_duplicate(thisObj) {

     var field_value = thisObj.value;

     var field_name = thisObj.name;

     var duplicate_email = duplicate_user = '';

     var url = adminurl+"ajax_serverPage.php?case=duplicate_check&field_name="+field_name+"&field_value="+field_value+"&sid="+Math.random();

     if (field_value != "") {

          if (field_name == 'email') {

            duplicate_email = set_field_value('id_duplicate_email','Validating Email address...Please wait for few seconds and submit again!');

          }

          

          if (field_name == 'uName') {

            duplicate_user = set_field_value('id_duplicate_username','Validating Login name...Please wait for few seconds and submit again!');

          }

          

          http.open("GET", url, true);

          http.onreadystatechange = function() {//Call a function when the state changes.

               if(http.readyState == 4 && http.status == 200) {

                    if (http.responseText != "") {

                         var rtn_text = http.responseText;

                         if (rtn_text=="duplicate") {

                              var err_div_id = "id_error_msg_"+field_name;

                              set_field_value(thisObj.id);

                              if (field_name == "email") {

                                   var error_message = " The E-Mail address '" + field_value + "' is already used <br> Please enter different E-Mail Address";

                                   duplicate_email = set_field_value('id_duplicate_email','Email address is already exist');                              

                              }

                              if(field_name == "uName") {

                                   var error_message = " The User name '" + field_value + "' is  already used <br> Please enter different User name";

                                   duplicate_user = set_field_value('id_duplicate_username','User name is already exist');

                              }

                              

                              var html = "<table width=\"100%\" border=\"0\" cellpadding=\"3\" align=\"center\" class=\"smalltext3\">"

                                  html += "  <tr> ";

                                  html += "    <td align=\"left\" bgcolor='#000099'> ";

                                  html += "      <span style='font-family:verdana; color:yellow;'>";

                                  html += "  <b>Duplicate occured</b> ";

                                  html += "      </span> ";

                                  html += "    </td>";

                                  html += "  </tr> ";

                                  html += "  <tr> ";

                                  html += "    <td align=\"center\"> ";

                                  html += "      <span class=\"alert2\">";

                                  html +=         error_message

                                  html += "      </span> ";

                                  html += "      </br></br> <input type='button' id='id_OK_message' onclick=\"close_popup_div('"+err_div_id+"');\" value='OK'></br>";

                                  html += "    </td> ";

                                  html += "  </tr> ";

                                  html += "</table> ";

                                   form_cover();

                                  show_element(err_div_id);

                                  inner_html(err_div_id,html);     

                                  return false;                    

                         }

                         else {

                              if (field_name == "email") {

                                   duplicate_email = set_field_value('id_duplicate_email','no');                              

                              }

                              if(field_name == "uName") {

                                   duplicate_user = set_field_value('id_duplicate_username','no');

                              }

                              return true;

                         }

                    }               

               }

          }

          http.send(null);

          return false;

     }

}



function redirect_page(href_page) {

     window.location.href=href_page;

}

