var MAXNUM = 10;
var eq = new Array("+","-","*","/");

var rel = 0; // Math.floor(Math.random() * 4);
var l = Math.floor(Math.random() * MAXNUM) + 1;
var r = Math.floor(Math.random() * MAXNUM) + 1;

var answ;

switch(rel) {
case 0 : answ = l + r; break;
case 1 : 
    if (l < r) {
        k = l;
        l = r;
        r = k; 
    }
    answ = l - r;
    break;
case 2 : answ = l * r; break;
case 3 : 
        answ = l * r;
        k = l;
        l = answ;
        answ = k;
    break;
}

answ = '<input name="equ" type="hidden" value="'+answ+'">';
var equation = l + " " + eq[rel] + " " + r + " = ";



function searchs(){

    var val = document.getElementById("search").value;

    if (val == "") return false;

    if (val.length < 2) {
        alert("Please enter a search with at least 2 character!");
        return false;
    }

    var sdiv = getSearchDiv();

    sdiv.innerHTML = '<div class="searchhead">Searching...&nbsp;&nbsp;&nbsp;&nbsp;<img src="images/close.gif" onclick="closeSearch()"></div>';
        
    var ajx = new Ajax();
/*
    ajx.onSend   = showProgress;
    ajx.onReturn = hideProgress;*/

    ajx.addParameter("search",     val);
    ajx.addParameter("callback", "showSearch");       
    ajx.sendRequest("search.php","POST",Ajax.evalResult); 

    return false;
}

contactList = null;
categoryList = null;

function setContactlist(list){
    contactList = list;
}
function setCategorylist(list){
    categoryList = list;
}

function debugString(str){
    var nl2br = arguments.length > 1 ? arguments[1] : false;

    var dbg = window.open('','debug','scrollbars=yes,resizable=yes,width=400,height=300');
    str = unescape(str);

    if (nl2br) str = str.replace(/\n/g, "<br>\n");
    
    dbg.document.write(str);
    dbg.document.close();
    dbg.focus();
}

function showSearch(list){

    var h = [];

    h.push('<div class="searchhead">Search Result &nbsp;&nbsp;&nbsp;&nbsp;<img src="images/close.gif" onclick="closeSearch()"></div>');
    h.push('<div class="searchresult">');

    j = 0;

    if (contactList.length > 0) {
        h.push('<div class="searchtitle">Doctors:</div>');
    }

    for (i in contactList) {
        h.push('<div><a href="doctor_index_'+ contactList[i]["contactid"] +'.html" target="_blank">'+ unescape(contactList[i]["lname"]) +', ' + unescape(contactList[i]["fname"]) +  '</a></div>');        
        j++;
    }

    if (categoryList.length > 0) {
        h.push('<div class="searchtitle">Categories:</div>');
    }

    for (i in categoryList) {

        var link = "";

        switch (categoryList[i]["categorytypeid"]){
        case "2": link = "specialty_"; break;
        case "3": link = "condition_"; break;
        case "4": link = "topics_"; break;
        default: link = "category_"; break;
        }

        h.push('<div><a href="'+ link + categoryList[i]["categoryid"] +'.html" target="_blank">'+ unescape(categoryList[i]["name"]) +'</a></div>');        
        j++;
    }

    if (list.length > 0) {
        h.push('<div class="searchtitle">Stories:</div>');
    }

    for (i in list) {        
    //    h.push('<div><a href="#" onclick="showstory('+list[i]["mediaid"]+')">'+ unescape(list[i]["name"]) +'</a></div>');
        h.push('<div><a href="story_'+ list[i]["mediaid"] +'.html" target="_blank">'+ unescape(list[i]["name"]) +'</a></div>');        
        j++;
    }

    if (j == 0) {
        h.push('<div align="center">Search returned no result.</div>');
    }

    h.push('</div>');

    var sdiv = getSearchDiv();
    sdiv.innerHTML = h.join("\n");
}

function getSearchDiv(){

    var wh = getWindowSize();

    var wtop = 50;

    if (typeof(SEARCHDIV) == "undefined") {    
        SEARCHDIV = document.createElement("div");
        SEARCHDIV.style.position   = "absolute";
        SEARCHDIV.style.zIndex = 1000;
        SEARCHDIV.style.border = "#000000 solid 1px";
        SEARCHDIV.style.background = "#FFFFFF";
        //SEARCHDIV.style.overflow = "auto";
        //SEARCHDIV.className     = "searchresult";    
        document.body.appendChild(SEARCHDIV);
    }

    SEARCHDIV.style.left   = "0px";
    SEARCHDIV.style.top    = wtop + "px";
    SEARCHDIV.style.width  = "200px";
    SEARCHDIV.style.height = (wh[1] - wtop) + "px";    
    SEARCHDIV.style.visibility = "visible";

    return SEARCHDIV;
}

function closeSearch(){
    try{ SEARCHDIV.style.visibility = "hidden"; } catch(e){}
    try{ STORYDIV.style.visibility = "hidden"; } catch(e){}
}

function getWindowSize() {

    var w = 0, y = 0;

    if( typeof(window.innerWidth) == 'number') { //Non-IE
        w = window.innerWidth;
        h = window.innerHeight;
    } 
    else if(document.documentElement && 
           (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight;
    } 
    else if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
    //IE 4 compatible
        w = document.body.clientWidth;
        h = document.body.clientHeight;
    }
    return [w, h];  
}

function viewVideo(id){
    var width = 600, height = 440;
    var left  = (screen.width/2) - width/2;
    var top   = (screen.height/2) - height/2;
    var displ = 'resizable=yes,';
	displ+= 'width='+width+',height='+height+',';
	displ+= 'left='+left+',top='+top+',';
	displ+= 'screenX='+left+',screenY='+top;

    url = 'media_'+id+'.html';

    viewWin = window.open(url,'View',displ);
    viewWin.focus();
}

function isemail(val){
   return (val.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

function sendform(){
    if (document.form.fname.value == ""){
        alert("Please enter first name!");
        return false;
    }
   
    if (document.form.lname.value == ""){
        alert("Please enter last name!");
        return false;
    }

    if (document.form.phone.value == ""){
        alert("Please enter phone number!");
        return false;
    }

    if (document.form.email.value == "" ||
        !isemail(document.form.email.value)){
        alert("Please enter a valid email address!");
        return false;
    }

    return true;
}

function printBrochure(){
    window.print();
}

function Ajax(){

    this.xmlhttp            = false;
    this.parameters         = [];
    this.callbackfunction   = null;
    this.responsetype       = "TEXT";
    this.onSend             = null; // called when request sent
    this.onReturn           = null; // called when request returned

    this.addParameter = function(name, value){
        this.parameters[name] = escape(value);
    }

    /** 
     * Associative array name value pairs
     */    
    this.addParameterArray = function(array) {
        var name;

        for (name in array) {
            this.parameters(i) = array[name];
        }
    }
     
    this.setResponseType = function(responsetype) {
        if (responsetype != "TEXT" && 
            responsetype != "XML") {
            return;
        }
        this.responsetype = responsetype;
    }

    this.getQueryString = function(){

        var params = "";

        for (i in this.parameters) {
            if (params != "") params+= "&";
            params+= i + "=" + this.parameters[i];
        }

        return params;
    }

    this.sendRequest = function(url, requestMethod, callback) {

        if (requestMethod != "GET" && 
            requestMethod != "POST") {
            return;            
        }

        var params = this.getQueryString();


        if (!this.xmlhttp) {
            if (window.XMLHttpRequest) {
                try {
                    this.xmlhttp = new XMLHttpRequest();
                } 
                catch(e) {
                    this.xmlhttp = false;             
                }
            }
            else if(window.ActiveXObject) {
                try {
                    
                    this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch(e) {
                    try {
                        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch(e){
                        this.xmlhttp = false;
                    }
                }
            }
        }

        if (this.xmlhttp) {

            if (requestMethod == "GET") {
                url+= "?" + params;
            }

            //local variables needed for the callback to work 
            var responsetype = this.responsetype; 
            var xmlhttp      = this.xmlhttp;
            var onReturn     = this.onReturn;

            if (Ajax.debug) {
                Ajax.log+= url + "\n";
                Ajax.params = params + "\n";
            }

            xmlhttp.open(requestMethod, url, true);
            xmlhttp.onreadystatechange = function(){

                if (xmlhttp.readyState==4) {

                    if (xmlhttp.status == 200) {

                        var content = responsetype == "TEXT" ? 
                            xmlhttp.responseText : 
                            xmlhttp.responseXML;

                        if (callback != null) callback(content);
                        if (onReturn != null) onReturn();
                    } 
                    else {
                        alert("Error retrieving the XML data from: "+ url +"\n" + 
                              xmlhttp.statusText);
                    }                    
                }
            }            

            if (requestMethod == "POST") {
                xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlhttp.send(params);                		
            }
            else {
                xmlhttp.send(null);
            }

            if (this.onSend != null) this.onSend();            
        }       
    }
}

/**
 * Ajax static properties, methods
 */
Ajax.debug  = true;
Ajax.result	= '';
Ajax.log    = '';
Ajax.params = '';

Ajax.evalResult = function(result){
    if (Ajax.debug) Ajax.result = result;

    try{
        eval(result);
    }
    catch(e){
        evalError("Ajax.evalResult:" + e.message, result);
    }
}

function evalError(err, result){
    debugString("Error: " + err + "\n" + result);
}
