/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var counter = 0;
var tempX = 0;
var elt_posX,elt_posY,b;//
var elements = new Array();
//var up = new UP();
var xmlHttp = createXmlHttpRequestObject();
var forgot_type = '';


//// retrieves the XMLHttpRequest object
//function createXmlHttpRequestObject()
//{
//    // will store the reference to the XMLHttpRequest object
//    var xmlHttp;
//
//    try
//    {
//        // try to create XMLHttpRequest object
//        xmlHttp = new XMLHttpRequest();
//    }
//    catch(e)
//    {
//        var XmlHttpVersions = new Array("MSXML2.XMLHTTP.7.0",
//                                        "MSXML2.XMLHTTP.6.0",
//                                        "MSXML2.XMLHTTP.5.0",
//                                        "MSXML2.XMLHTTP.4.0",
//                                        "MSXML2.XMLHTTP.3.0",
//                                        "MSXML2.XMLHTTP",
//                                        "Microsoft.XMLHTTP");
//
//        // try every prog id until one works
//        for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++)
//        {
//            try
//            {
//                // try to create XMLHttpRequest object
//                alert(ActiveXObject(XmlHttpVersions[i]));
//                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
//                alert(xmlHttp);
//            }
//            catch (e)
//            {}
//        }
//    }
//
//    // return the created object or display an error message
//    if (!xmlHttp)
//        alert("Error creating the XMLHttpRequest object.");
//    else
//        return xmlHttp;
//}


// function for username and password

function LoadForgotForm(type){
    //alert("Load forgot form");
    LoadPopUp(type);
    CenterPopUp(type);
    forgot_type = type;
}

function CenterPopUp(type){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact_"+type).height();
    var popupWidth = $("#popupContact_"+type).width();

    var winH = $(window).height();
    var winW = $(window).width();
    var centerDiv = $('#popupContact_'+type);
    centerDiv.css('top', winH/2-centerDiv.height()/2);
    centerDiv.css('left', winW/2-centerDiv.width()/2);

    //only need force for IE6
    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

function LoadPopUp(type){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("fast");
        $("#popupContact_"+type).fadeIn("fast");
        popupStatus = 1;
    }
}

function DisablePopUp(type){
  //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact_"+type).fadeOut("slow");
        popupStatus = 0;
    }
}

function SendUP(){

    var email  = '';
    var url    = '';
    var params = '';
    var ftype  = '';
    
    url = '../includes/xml/forgot_username_password.php';
    email = document.getElementById('forgot_'+forgot_type).value;
    ftype = document.getElementById('forgotten_type').value;

    if(email == ''){
        document.getElementById('errorwindow_'+forgot_type).innerHTML = 'Wrong Email address, Please Re-Enter Email.';
    }else{

        document.getElementById('errorwindow_'+forgot_type).innerHTML = '<img src="Members/images/ajax-loader.gif" />';
        params = "Type="+forgot_type+"&email="+email+'&FType='+ftype;
//        alert(params);
        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 =  handleUpdateUPMess;
                xmlHttp.send(params);
              }
            }
            // display the error in case of failure
            catch (e){
                alert("Can't connect to server:\n" + e.toString());
            }
        }
    }
}

function handleUpdateUPMess(){
  // 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
        handleUpdateResponseUPMess();
      }
      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 handleUpdateResponseUPMess(){
    // 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
    var xmlRoot = '';
    xmlRoot = xmlResponse.documentElement;

    var sucess_up = '';
    var type = '';

    sucess_up = xmlRoot.getElementsByTagName("operation-success-up")[0].firstChild.data;
    type = xmlRoot.getElementsByTagName("operation-type-up")[0].firstChild.data;
    //alert(type);
    if(sucess_up == '0'){
        if(type == 'Username'){
             document.getElementById('errorwindow_'+forgot_type).innerHTML = 'Wrong Email address, Please Re-Enter Email.';
        }else{
             document.getElementById('errorwindow_'+forgot_type).innerHTML = 'Wrong Email address, Please Re-Enter Email.';
        }
    }else{
        if(type == 'Username'){
            document.getElementById('errorwindow_'+forgot_type).innerHTML = '';
            document.getElementById('messagewindow_'+forgot_type).innerHTML = 'Your Username sent Successfully.';
        }else{
            document.getElementById('errorwindow_'+forgot_type).innerHTML = '';
            document.getElementById('messagewindow_'+forgot_type).innerHTML = 'Your Password sent Successfully.';
        }
    }
    
    
}

