﻿try {
    window.addEvent('domready', function() {
        var tip = new Tips($$('.tip'), {
	        initialize:function(){
		        this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
	        },
	        onShow: function(toolTip) {
		        this.fx.start(1);
	        },
	        onHide: function(toolTip) {
		        this.fx.start(0);
	        }
        });
        var tip = new Tips($$('.eventTip'), {
	        showDelay: 400,
	        hideDelay: 400,
	        fixed: true
        });
    });
} catch(e) {}

function writeEmail(name, domain, ext) {
    var at = "@";
    var dot = ".";
    var email = name + at + domain + dot + ext;
    document.write('<a href="mailto:' + email + '" title="' + email + '">' + email + '<\/a>');
}

function writeEmailName(email, domain, ext, name) {
    var at = "@";
    var dot = ".";
    var email = email + at + domain + dot + ext;
    document.write('<a href="mailto:' + email + '" title="' + name + '">' + name + '<\/a>');
}

function Browser() {
	var ua, s, i;
	ua = navigator.userAgent;
	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {		
		return true; // if browser is IE
	}
	return false; // if browser is not IE
}

function toggleLrgImg(id,show) {
    var src = document.getElementById("lrg"+id).src;
    if(src.indexOf(".jpg") == -1 && src.indexOf(".gif") == -1 && src.indexOf(".png") == -1 && src.indexOf(".bmp") == -1) {
        return;
    }
	
	var width = document.getElementById(id).width + 5 + "px";		
	document.getElementById("lrg"+id).style.display = "none";
	if(show) {
		document.getElementById("lrg"+id).style.display = "block";
		// if browser is not IE
		if(!Browser()) {
			document.getElementById("lrg"+id).style.marginLeft = width;
		}
	}
}

function viewMore(id) {
    var max = document.getElementById("max").value;
    for(var i=0; i<max; i++) {        
        if(i != id) {            
            try {
                document.getElementById("hide" + i).click();
                document.getElementById("lnk" + i).innerHTML = "View More";
            }
            catch(e) {}
            try {
                document.getElementById("div" + i).className = "viewMore";
            }
            catch(e) {}
        }
    }
    document.getElementById("toggle" + id).click();
    try {
        var lnk = document.getElementById("lnk" + id);
        if(lnk.innerHTML == "View More") {
            lnk.innerHTML = "View Less";
            lnk.title = "View Less";
        }
        else {
            lnk.innerHTML = "View More";
            lnk.title = "View More";
        }
    }
    catch(e) {}
    try {
        var div = document.getElementById("div" + id);
        if(div.className == "viewMore") {
            div.className = "viewLess";            
        }
        else {
            div.className = "viewMore";
        }
    }
    catch(e) {}
}

/*function viewEvent(id) {
    var max = document.getElementById("max").value;
    for(var i=0; i<max; i++) {
        var hid = document.getElementById("hide" + i);
        if(hid.value != id) {
            hid.click();        
        }
    }
    document.getElementById("slideIn" + id).click();   
}*/

function swap(id) {
    id = id.replace("ddlRank","hidSwap");
    document.getElementById(id).value = true;
}

function lightup(id, opacity){            
    var speed = 5;
    var obj = document.getElementById(id);    
    if(!Browser()) {
        obj.style.MozOpacity = opacity/100;
    }
    else {
        obj.filters.alpha.opacity = opacity;
    }
    if(opacity < 100) {       
        setTimeout("lightup('" + id + "', " + (opacity + speed) + ")", 1);
    }
}

function signUp(fromBlog) {
    var id = "newsletterMessage";
    var obj = document.getElementById(id);    
    //obj.style.marginLeft = "130px";
    if(!Browser()) {
        //obj.style.marginLeft = "320px";
    }
    if(fromBlog) {
        //obj.style.marginLeft = "-400px";        
    }
    //obj.style.marginTop = "150px";
    obj.style.display = "block";
    lightup(id, 0);
    setTimeout("hideNewsletterMessage()", 10000);
}

function isValidEmail(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;   
   if(!reg.test(email)) {
      return false;
   }
   return true;
}

function newsletterSignUp(nameMessage, emailMessage) {
    var name = document.getElementById("txtName").value.replace(nameMessage);
    var email = document.getElementById("txtEmail").value.replace(emailMessage);
    var error = document.getElementById("lblError");
    if(name == "") {
        error.innerHTML = "Error! Name was left blank";
        return;
    }
    if(email == "") {
        error.innerHTML = "Error! Email was left blank";
        return;
    }
    if(!isValidEmail(email)) {
        error.innerHTML = "Error! Invalid email address";
        return;
    }
    url = "/Default.aspx?submitNewsletter=true&name=" + escape(name) + "&email=" + escape(email);
    document.getElementById("iframe").src = url;
}

function hideNewsletterMessage() {
    document.getElementById("newsletterMessage").style.display = "none";    
}

function search(searchMessage) {
    window.location = "/Search.aspx?SearchTerm=" + document.getElementById("txtSearch").value.replace(searchMessage,"");
}

function selectBulb(days) {
    var ddl = document.getElementById("ctl00_ContentPlaceHolder1_ddlBulb");
    for(var i=0; i<ddl.length; i++) {
        if(ddl[i].value == days) {
            ddl[i].selected = true;
        }
    }
}

function updateBulb(name, days) {
    var url = "UpdateBulb.aspx?name=" + name + "&days=" + days;
    document.getElementById("iframe").src = url;
}

function writeSwf(file,id,width,height,version,bgcolor,loop) {
    var html = 
        '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ' +
        'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version + ',0,0,0" ' +
        'width="' + width + '" height="' + height + '" id="top" align="middle">' +
        '<param name="allowScriptAccess" value="sameDomain" />' +
        '<param name="movie" value="' + file + '" />' +
        '<param name="quality" value="high" />' +
        '<param name="wmode" value="transparent" />' +
		'<param name="scale" value="noscale" />' +
	    '<param name="loop" value="' + loop + '" />' +
        '<param name="bgcolor" value="' + bgcolor + '" />' +
        '<embed src="' + file + '" quality="high" wmode="transparent" bgcolor="' + bgcolor + '" ' +
        'width="' + width + '" height="' + height + '" name="top" align="middle" allowScriptAccess="sameDomain" ' +
        'type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
        '</object>';
    document.getElementById(id).innerHTML = html;
}

function openCloseTree(id) {

    var max = document.getElementById("maxResource").value;
    
    for(var i=0; i<max; i++) {        
        if(i != id) {            
            document.getElementById("hideResource" + i).click();
            document.getElementById("lnkResource" + i).innerHTML = "Expand";
            document.getElementById("lnkResource" + i).title = "Expand";
            document.getElementById("divResource" + i).className = "open";            
        }
    }
    
    document.getElementById("toggleResource" + id).click();
    
    var lnk = document.getElementById("lnkResource" + id);
    
    if(lnk.innerHTML == "Expand") {
        lnk.innerHTML = "Collapse";
        lnk.title = "Collapse";
    }
    else {
        lnk.innerHTML = "Expand";
        lnk.title = "Expand";
    }
    
    var div = document.getElementById("divResource" + id);
    
    if(div.className == "open") {
        div.className = "close";            
    }
    else {
        div.className = "open";
    }
}


function searchResource(page) {

    var keyword = document.getElementById("ctl00_ResourceTree1_txtKeyword").value;
    var county = document.getElementById("ctl00_ResourceTree1_ddlCounty").value;
    var categoryId = document.getElementById("hidCategoryId").value;
    var subCategoryId = document.getElementById("hidSubCategoryId").value;

    var url = "Search.aspx?keyword=" + 
        escape(keyword) + 
        "&county=" + 
        county + 
        "&Page=" + 
        page + 
        "&categoryId=" + 
        categoryId + 
        "&subCategoryId=" + subCategoryId;
        
    ajaxload(url,"resourceSearch","");
    document.getElementById("resourceCopy").style.display = "none";   
    scroll(0,0); 
}

function newSearchResource() {
    
    if (document.URL.toLowerCase().indexOf("/resourcecenter") != -1) {
    
        document.getElementById("hidCategoryId").value = 0;
        document.getElementById("hidSubCategoryId").value = 0;
        searchResource(1);  
        
        
    } else {
    
        document.location = "/ResourceCenter/?k=" + 
            encodeURI(document.getElementById("ctl00_ResourceTree1_txtKeyword").value) + 
            "&c=" + encodeURI(document.getElementById("ctl00_ResourceTree1_ddlCounty").selectedIndex) + 
            "&f=2";
    
    }    
    
    
}

function changeResource(categoryId, subCategoryId) {

    if (document.URL.toLowerCase().indexOf("/resourcecenter") != -1) {
    
        document.getElementById("hidCategoryId").value = categoryId;
        document.getElementById("hidSubCategoryId").value = subCategoryId;
        searchResource(1);
        
    } else {
    
        document.location = "/ResourceCenter/?c=" + 
            encodeURI(categoryId) + 
            "&s=" + encodeURI(subCategoryId) + 
            "&f=1";
    
    }
    
}

function ajaxload(url, containerid, condition) {
	try { // Browser object detection
		
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
		new ActiveXObject("Microsoft.XMLHTTP");		
	}
	catch (e){alert(e.description)} // browser doesn't support ajax. handle however you want
	xmlhttp.onreadystatechange = function(){statuschanged(containerid,condition)}; // every time ready status changes, statuschanged() function executes
	xmlhttp.open("GET", url, true); // Usage: open(HTTP method, url, and asynchronous = true or false)
	xmlhttp.send(null); // send the request.
}

function statuschanged(containerid, condition){
	if((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) { // readyState codes: 0=Uninitialised 1=Loading 2=Loaded 3=Interactive 4=Completed; status code: 200=OK		
		if(xmlhttp.responseText != "") {
			if(condition == "h") { // populating a hidden field
				document.getElementById(containerid).value = xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}
			else if(condition == "a") { // if condition is append
				document.getElementById(containerid).innerHTML += xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}
			else {			
				//alert(xmlhttp.responseText)
				document.getElementById(containerid).innerHTML = xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'		
			}			
		}
	}
	else { // else, readyState is not yet Completed and status is not yet OK, so here we display loading text
		document.getElementById(containerid).innerHTML = 
		    "<div id='loading'>" +
		        "<div class='left'><img src='/App_Themes/Default/Images/loading.gif' alt='Loading...' /></div>" +
		        "<div class='right'>Loading...</div>" +
		        "<div class='clear'></div>" +
		    "</div>";
	}
}

function showSubMenu(id, show) {
    var div = document.getElementById(id);
    div.style.display = "none";
    if(show) {
        div.style.display = "block";
    }
}

/* from eggheadcafe -- for parsing querystring variables */
function PageQuery(q) {
    if(q.length > 1) this.q = q.substring(1, q.length);
    else this.q = null;
    this.keyValuePairs = new Array();

    if(q) {
        for(var i=0; i < this.q.split("&").length; i++) {
            this.keyValuePairs[i] = this.q.split("&")[i];
        }
    }

    this.getKeyValuePairs = function() { return this.keyValuePairs; }
    this.getValue = function(s) {
    for(var j=0; j < this.keyValuePairs.length; j++) {
        if(this.keyValuePairs[j].split("=")[0] == s)
            return this.keyValuePairs[j].split("=")[1];
    }

    return false;
    }

    this.getParameters = function() {
        var a = new Array(this.getLength());
    
        for(var j=0; j < this.keyValuePairs.length; j++) {
            a[j] = this.keyValuePairs[j].split("=")[0];
        }
    return a;
    }

    this.getLength = function() { return this.keyValuePairs.length; }
    }

    function queryString(key){
        var page = new PageQuery(window.location.search);
    return unescape(page.getValue(key));
    }  
    
function goTo(url) {
	top.location=url;
}

function checkMe(chk) {
    var ids = document.getElementById("ctl00_ContentPlaceHolder1_hidIds").value;
    ids = ids.replace("|" + chk.value + "|","");
    ids = ids.replace(",,",",");
    if(ids.indexOf(",") == 0) {
        ids = ids.substring(1,ids.length);
    }
    if(ids.lastIndexOf(",") == 0) {
        ids = "";
    }
    if(chk.checked) {
        ids += "|" + chk.value + "|,";
    }
    document.getElementById("ctl00_ContentPlaceHolder1_hidIds").value = ids;
    
    // unchecked ids
    var unchekedIds = document.getElementById("ctl00_ContentPlaceHolder1_hidUnchekedIds").value;    
    unchekedIds = unchekedIds.replace("|" + chk.value + "|","");
    unchekedIds = unchekedIds.replace(",,",",");
    if(unchekedIds.indexOf(",") == 0) {
        unchekedIds = unchekedIds.substring(1,unchekedIds.length);
    }
    if(unchekedIds.lastIndexOf(",") == 0) {
        unchekedIds = "";
    }
    if(!chk.checked) {
        unchekedIds += "|" + chk.value + "|,";
    }
    document.getElementById("ctl00_ContentPlaceHolder1_hidUnchekedIds").value = unchekedIds;
}

function checkEmAll(all) {
    var chk = document.getElementsByName("chk");
    for(var i=0; i<chk.length; i++) {
        chk[i].checked = false;
        if(all.checked) {
            chk[i].checked = true;            
        }
        checkMe(chk[i]);
    }
}

function passInnerHTMLAndPrint() {

    window.frames.ifrPrintFrame.loadAndPrint( $("decMainContent2009").innerHTML );

}