// AJAX

// AJAX HANDLING SYSTEM

var xmlHttp = createXmlHttpRequestObject();
	
//creates an instance of the xmlHttp object
function createXmlHttpRequestObject() {
    // will store the reference to the XMLHttpRequest object
    var xmlHttp;
    try {
        // try to create XMLHttpRequest object
        xmlHttp = new XMLHttpRequest();
    }
    catch(e) {
        // assume IE6 or older
        var XmlHttpVersions = new Array("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
                xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
            }
            catch (e) {
				
            }
        }//for loop
    }//try1
		
    //check object was created ok, if so return object, if not print error message
    if (! xmlHttp) {
        alert("Error creating the XMLHttpRequest object.");
    }
    else {
        //alert("xmlHttp instance created successfully!");
        return xmlHttp;
    }
} // create xmlhttp object function

function doRequest (address, action) {
    if (xmlHttp) {
        xmlHttp.open ('GET', address);
        xmlHttp.onreadystatechange = action
        xmlHttp.send (null);
    }
}

function doRequestPost (address, params, action) {
    if (xmlHttp) {
        //alert('dorequest started.');
        xmlHttp.open ('POST', address);
        xmlHttp.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = action
        xmlHttp.send (params);
    }
} // function

function handleResponse() {
    //alert('handleResponse');
    //alert(xmlHttp.readyState);
    
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            try {
                return true;
                //response = xmlHttp.responseText;
                //alert(response);
            }
            catch(e) {
                alert('Error: action not executed correctly.');
            } // try - catch
        } 
        else { // status failed
            //alert('Error: Status not 200');
        } // status test
    }
    else {
        //alert('Error: readyState not 4');
    } // ready status check
} // function


// SUGGESTION / AUTOCOMPLETE SYSTEM

// gets input from the user and passes search string to php page, then repeats iteself every 3 secs
function get_suggestions() {
    //alert('get suggestions running.');
    
    var status = document.getElementById('ajax_suggestion_status');
    if(status.value == 'start'){
        // alert('started');
        var address = document.getElementById('ajax_address');
        var search_string = document.getElementById('main_search_box');
        var params = "search_string="+ search_string.value;
        if (search_string.length !=0) {
            doRequestPost(address.value, params, display_suggestions);
        }
        else {
            //alert('you must fill in both fields of the add comment form');
        }
        setTimeout("get_suggestions()", 3000);
    } // status test
    else{
        // alert('Suggestions stopped');
    }
} // function

// handles response from php page,  gets suggestion results from php page and displays them in the suggestion box
function display_suggestions(){
    if (handleResponse() == true) {
        response = xmlHttp.responseText;
        //alert(response);
        var pageContent = response;
        var target = document.getElementById('searchSuggestions') ;

        target.innerHTML = pageContent;
    } // no response
} // function

// autocompletes selected suggestion displayed in suggestion box, activated by onclick event
function select_suggestion(suggestion){
    var search_box = document.getElementById('main_search_box');
    var search_form = document.search_form;
    search_box.value = suggestion;
    search_box.focus();
    search_form.submit();

}

// starts the suggestion process (get_suggestions), triggered by onfocus event
function start_suggestions(){
    var status = document.getElementById('ajax_suggestion_status');
    status.value = 'start';
    get_suggestions();
    show('searchSuggestions');
}
// stops the get_suggestions function, triggered by onblur event
function stop_suggestions(){
    //alert('stop!');
    var status = document.getElementById('ajax_suggestion_status');
    var search_field = document.getElementById('main_search_box');
    status.value = 'stop';
    //search_field.focus();
    hide('searchSuggestions');
    
}

// RATING SYSTEM

// variable that stores the current selected rating value
var current_rating = 'FALSE';

// provides mouseover highlighting on selected rating target
function highlight_rating(target_id){
    var target = document.getElementById('rating'+target_id);
    target.style.fontWeight = 'bold';
    target.style.backgroundColor = 'blue';
    target.style.color = 'white';
}
// provides mouseout unhighlighting of the rating target element
function unhighlight_rating(target_id){
    var target = document.getElementById('rating'+target_id);
    
    if(current_rating == target_id){ // keep selected style in place
        target.style.backgroundColor = 'yellow';
        target.style.fontWeight = 'bold';
        target.style.color = 'white';
    }
    else{ // remove highlighed style from target
        target.style.fontWeight = 'normal';
        target.style.color = 'black';
        target.style.backgroundColor = 'white';
    }
}

// provides onclick selection of desired rating value
function select_rating(selected_rating){
    var selected_rating_label = document.getElementById('rating'+selected_rating);
    var rating_hidden_field = document.getElementById('review_rating');

    // clear all rating labels before selecting chosen one
    rating_hidden_field.value = 'FALSE';

    for(x = 1; x <= 5; x++){
        var rating_label = document.getElementById('rating'+x);
        rating_label.style.backgroundColor = 'white';
        rating_label.style.fontWeight = 'normal';
        rating_label.style.color = 'black';
    }

    // set new selected rating
    current_rating = selected_rating;
    rating_hidden_field.value = selected_rating;
    // style
    selected_rating_label.style.fontWeight = 'bold';
    selected_rating_label.style.color = 'white';
    selected_rating_label.style.backgroundColor = 'yellow';
}


// GENERAL FUNCTIONS

// sets a target element's visibility to visible
function show(target_id){
    document.getElementById(target_id).style.visibility = 'visible';
}
// sets a target element's visibilit to hidden
function hide(target_id){
    document.getElementById(target_id).style.visibility = 'hidden';
}

// highlights a target element
function highlight(target_id){
    var target = document.getElementById(target_id);
    target.style.backgroundColor = 'yellow';
    target.style.color = 'white';
}
// unhighlights a target element
function unhighlight(target_id){
    var target = document.getElementById(target_id);
    target.style.backgroundColor = 'white';
    target.style.color = 'black';
}

// sets the focus of the cursor to the target element
function setFocus(target_id){
    var target = document.getElementById('login_username');
    target.focus();
}

function showHide(target) {
    var elem, vis;
    if( document.getElementById ) // this is the way the standards work
        elem = document.getElementById(target);
    else if( document.all ) // this is the way old msie versions work
        elem = document.all[target];
    else if( document.layers ) // this is the way nn4 works
        elem = document.layers[target];

    vis = elem.style;
    // if the style.display value is blank we try to figure it out here
    if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
        vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
    vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

function playSound (sound) {
    target = document.getElementById("soundDiv");
    var flashCode =
    '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="1" height="1" id="bgSound1" align="middle">'
    +
    '<param name="allowScriptAccess" value="sameDomain" />'
    +
    '<param name="allowFullScreen" value="false" />'
    +
    '<param name="movie" value="'+sound+'" /><param name="loop" value="false" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" />	<embed src="'+sound+'" loop="false" quality="high" bgcolor="#ffffff" width="1" height="1" name="bgSound1" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
    +
    '</object>';

    target.innerHTML = flashCode;
}
