var MAXNUM = 10;
var eq = new Array("+","-","*","/");

var rel = 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 sid = "";
    try{sid = getsid();} catch(e){}

    var ajx = new Ajax();
    ajx.addParameter("search",     val);
    ajx.addParameter("callback", "showSearch");       
    ajx.addParameter("sid", sid);       
    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.php?id='+ contactList[i]["contactid"] +'" 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.php?id="; break;
        case "3": link = "condition.php?id="; break;
        case "4": link = "topics.php?id="; break;
        default: link = "category.php?id="; break;
        }

        h.push('<div><a href="'+ link + categoryList[i]["categoryid"] +'" 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.php?id='+ list[i]["mediaid"] +'" 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 = 195;

    if (typeof(SEARCHDIV) == "undefined") {    
        SEARCHDIV = document.createElement("div");
        SEARCHDIV.className = "searchdiv";

        //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);
    }

    //alert(SEARCHDIV.style.top);

    //var wtop = parseInt(SEARCHDIV.style.top);
    //SEARCHDIV.style.left   = "0px";
    //SEARCHDIV.style.top    = wtop + "px";
    //SEARCHDIV.style.width  = "200px";
    //alert(SEARCHDIV.style.top);
    //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.php?id='+id;

    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 isdate(val){
    var d = new Date(val);

    return !isNaN(d);
}

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 sendform2(){
    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;
    }

    var ck = parent.document.getElementById("appointment");

    if (ck.checked) {
        if (document.form.date1.value == "" &&
            document.form.date2.value == "" &&
            document.form.date3.value == ""){
            alert("Please enter a date!");
            return false;
        }

        if (document.form.date1.value != "" && 
            !isdate(document.form.date1.value)) {
            alert("Please enter a valid date for date1!");
            return false;
        }
        if (document.form.date2.value != "" && 
            !isdate(document.form.date2.value)) {
            alert("Please enter a valid date for date2!");
            return false;
        }
        if (document.form.date3.value != "" && 
            !isdate(document.form.date3.value)) {
            alert("Please enter a valid date for date3!");
            return false;
        }
    }

    return true;
}


function printBrochure(){
    window.print();
}

function showDateDiv(val){
    var ediv = parent.document.getElementById("datediv");
    ediv.style.visibility = val ? 'visible' : 'hidden';
}

function showGMap(latitude, longitude, zoom, info) {

    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("gmap"));
    
        var point = new GLatLng(latitude, longitude);
        map.setCenter(point, zoom);
    
        var marker = new GMarker(point);
        map.addOverlay(marker);
    
        
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(info);
        });
    
        marker.openInfoWindowHtml(info);  
    }
}
