﻿var currentSelectedID = 0;
var lastSelectedID = 0;

/* ========================================================================= */
/* AJAX STUFF */
/* ========================================================================= */
function loadOtherDDs() {
    ajaxUpdate("Loading...");
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'ajax/defaultDropDowns.aspx', true);
	
      /* The callback function */
    xmlhttp.onreadystatechange = function() {	    
        if (xmlhttp.readyState == 4) {        
            if (xmlhttp.status == 200)
            {
                loadOtherDDsResponse(xmlhttp.responseText); //OR responseXML
                ajaxUpdate("Done");                
            } else {
                ajaxUpdate("Error in changeDD");                
            }
        }
    }

    /* Setup and send the POST request */
    var sendForm = "";
    for(i=0; i<document.aspnetForm.elements.length; i++)
    {
        if (document.aspnetForm.elements[i].name.indexOf("_AJXS") > 0) {
            sendForm += document.aspnetForm.elements[i].name + "=" + document.aspnetForm.elements[i].value + "&";
         }
    }    
    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(sendForm);
}
function loadOtherDDsResponse(responseXML)
{    
    document.getElementById("divDynamicDDs").innerHTML = responseXML;
}
/* ========================================================================= */

/* ========================================================================= */
function changeDDandSearch(perfTypeDD) {
    if (perfTypeDD == "perfTypeDD") {
        loadOtherDDs();
        document.getElementById("column3").style.display = "block";
        document.getElementById("column2").style.width = "390px";
        document.getElementById("column2").style.backgroundImage = "url(images/column2_bgd.jpg)";
        
        // Need to reset each dropdown manually, wierd as the loadOtherDDs should do this ?
        try {
            document.getElementById("performanceStyleID_AJXS").selectedIndex = -1;
            document.getElementById("gender_AJXS").selectedIndex = -1;
            document.getElementById("actingAgeRange_AJXS").selectedIndex = -1;
            document.getElementById("ethnicOrigin_AJXS").selectedIndex = -1;
            document.getElementById("Height_AJXS").selectedIndex = -1;
            document.getElementById("Weight_AJXS").selectedIndex = -1;
        }catch(e){};
    }
    // Always set page back to zero if a dropdown was changed
    if (perfTypeDD != "pageChange") {document.getElementById('hdnPageNumber').value = 0;}

    ajaxUpdate("Searching...");
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'ajax/searchProfiles.aspx', true);
	
      /* The callback function */
    xmlhttp.onreadystatechange = function() {
	
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200)
            {
                changeDDAndSearchResponse(xmlhttp.responseText); //OR responseXML
                ajaxUpdate("Done");
            } else {
                ajaxUpdate("Error in changeDD");                
            }
        }
    }

    /* Setup and send the POST request */
    var sendForm = ""
    for(i=0; i<document.aspnetForm.elements.length; i++)
    {
        if (document.aspnetForm.elements[i].name.indexOf("_AJXS") > 0) {
            sendForm += document.aspnetForm.elements[i].name + "=" + document.aspnetForm.elements[i].value + "&";
         }
    }    
    
    sendForm += "&pageNum=" + document.getElementById('hdnPageNumber').value;
    
    
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(sendForm);
}
function changeDDAndSearchResponse(responseXML)
{    
    // Display search results
    document.getElementById("searchResults").innerHTML = responseXML;
    
    if (responseXML.indexOf("no matches") < 0 && responseXML.indexOf("more search options") < 0) {
    // Now pick a random user to display (look for loadProfile(NN) in return string
    var userIDtextLocation = responseXML.indexOf("loadProfile(") + 12;
    var nextBracket = responseXML.indexOf(")", userIDtextLocation);
    var userID = responseXML.substring(userIDtextLocation, nextBracket);
    loadProfile(userID);
    }
    /*
    var re = new RegExp("loadProfile\((\d+)\)");
    var m = re.exec(responseXML);    
    
    for (i = 0; i < m.length; i++) {
     alert(m[i]);
    }    
    
    var randomnumber=Math.floor(Math.random()*m.length)    
    loadProfile(m[randomnumber]);
     */ 
    // RegEx: loadProfile\((.+)\)
}
/* ========================================================================= */

/* ========================================================================= */
function loadProfile(profileID)
{
    ajaxUpdate("Loading Profile...");
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'ajax/loadProfile.aspx', true);
	
      /* The callback function */
    xmlhttp.onreadystatechange = function() {
	
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200)
            {
                loadProfileResponse(xmlhttp.responseText); //OR responseXML
                ajaxUpdate("Done");                
            } else {
                ajaxUpdate("Error in loadProfile");
            }
        }
    }
  
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('profileID=' + profileID);
    
    if (currentSelectedID > 0) {
        try {document.getElementById("searchResult" + currentSelectedID).innerHTML = "" } catch(e){}     
    }
    document.getElementById("searchResult" + profileID).innerHTML = "<img src='http://www.doejo.co.uk/images/highlight_outline.png'>";
    currentSelectedID = profileID;
    
}
function loadProfileResponse(responseXML)
{    
    document.getElementById("showProfile").innerHTML = responseXML;
}