// JavaScript Document
//document.write('<script type="text/javascript" src="/bookstore/scripts/prototype.js"></script>');
//document.write('<script type="text/javascript" src="/bookstore/scripts/scriptaculous.js"></script>');

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();
/* NEW AJAX */
function sendRequest(action, term, subject, course_number, section_number, book_or_item) {
    // Create a string to pass through url
    var queryString = "action="+action+"&";
    queryString += "term="+term+"&"
    queryString += "subject="+subject+"&"  
    queryString += "course_number="+course_number+"&"  
    queryString += "section_number="+section_number+"&"  
    queryString += "book_or_item="+book_or_item
    http.open('get', '/cwis412/bookstore/specialorder/ajax_requests.asp?'+queryString);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function handleResponse() { 
    if(http.readyState == 4){
        var response = http.responseText;     
        var toArray = new Array();
  
        // Get the object to update  
        toArray = response.split("|");
        if (toArray[0] == "alertUser") {
            if (toArray[1] > 0) {
                alert("As of 7:00am this morning, we had " + toArray[1] + " copies of "+ toArray[2] +" on the shelf.\n\nIf you would like to double-check the shelves, one of our friendly employees can help you.  You may, of course, continue and place the order for this item.");
            } else {
                alert("As of 7:00am this morning, we had no copies of " + toArray[2] +" on the shelf.\n\n Please place the order for this item.");            
            }
       
       } else {
            var target = document.getElementById(toArray[0]);                  
            DisableSelects(target.id);        
            target.removeAttribute("disabled");        
            var options = document.createElement("option");
            options.appendChild(document.createTextNode("select item..."));
            options.setAttribute("value", "");
            target.appendChild(options);

            for (i=1; i<toArray.length; i++) {
                options = document.createElement("option");
                      
                if (target.id != "book_or_item") {
                    options.appendChild(document.createTextNode(toArray[i]));
                    options.setAttribute("value", toArray[i]);               
                    target.appendChild(options);
                } else {
                    bookOrItemArray = toArray[i].split("^")
                    options.appendChild(document.createTextNode(bookOrItemArray[0]+" ("+bookOrItemArray[1]+")"));
                    options.setAttribute("value", bookOrItemArray[0]+"^"+bookOrItemArray[1]);
                    target.appendChild(options);
                }
            }
        }
    } 
}


/* 
DisableSelects()
* Requeired: string, usually the id of an element
* Cascades a node list disabling the elements.*/
function DisableSelects(string) {   
    var nodeList = document.getElementsByTagName("select");
    for (i=0; i<nodeList.length; i++) {
        if (nodeList[i].id == string) {
            x = i;
        }
    }
    for (x=x; x<nodeList.length; x++) {
        var element = document.getElementById(nodeList[x].id);
        while(element.hasChildNodes() == true) {
            element.removeChild(element.childNodes[0]);
       } // while
       element.setAttribute("disabled", "");
    } // for
} // function


function VerifyEmails() {
    var element = document.getElementById('notice');
	if (document.getElementById('email').value != document.getElementById('verify_email').value) {
	    element.style.display = "inline";
	    element.innerHTML = "<p>Emails do not match.</p>";
	} else {
	    element.style.display = "none";
	}
}

function Validate() {
    var errorArray = new Array();
    var dataArray = new Array;
    dataArray[0] = "first_name";
    dataArray[1] = "last_name";
    dataArray[2] = "phone";
    dataArray[3] = "email";

    var i = 0;
    while (i<dataArray.length) {
        var element = document.getElementById(dataArray[i]);
        if (element.value.length == 0) {
            errorArray.push(HumanName(element.id) + " is required.");   
        }
        i++;
    } // while
   
   if (errorArray.length > 0) {
       element = document.getElementById("errorNotice");
        var string = "<p style=\"margin: 10px 10px 10px 30px;\">Error:</p><ul style=\"margin: 0 10px 10px 35px;\">";
        for (i=0; i<errorArray.length; i++) {
            string = string + "<li>"+errorArray[i]+"</li>\n";
        } // for
        string = string + "</ul>\n";
        element.style.backgroundColor = "lightyellow";
        element.style.border = "1px solid #CC9900"
        element.style.color = "red";
        element.innerHTML = string;
        return false;
    } else {
        return true;
    } // if 
} // function

function HumanName(string) {
    var aString = "";
    var toArray = string.split("_")
    var i = 0;
    while(i<toArray.length) {
        var firstLetter = toArray[i].substr(0,1).toUpperCase();
        var trimmedWord = toArray[i].substr(1,toArray[i].length);
        var spacer = " ";
        var aString = aString + firstLetter + trimmedWord + spacer; 
        i++;
    }
    return aString;
} // HumanName

