﻿function bookmarkMe(url, name) {
    try {
        window.external.AddFavorite(url,name);
    } 
    catch(e) { 
        var temp = "CTRL";
        if(isMac()) {
            temp = "CMD";
        }
        alert("Press " + temp + "-D to Bookmark this Page");
    } 
}

function isMac() {
    version = navigator.appVersion;
    if(version.toLowerCase().indexOf("mac") != -1) {
        return true;
    }    
    return false;
}

function isEnterPress(e) {
    var key = e.keyCode || e.which;
    if(key == 13) {
        return true;
    }
    return false;
}

function urlEncode(str) {
    return str.replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function trim(str) {
    return str.replace(/^\s+|\s+$/g,"");
}

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 lrgId = "lrg" + id;
    var src = document.getElementById(lrgId).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(lrgId).style.display = "none";
	if(show) {
		document.getElementById(lrgId).style.display = "block";
		// if browser is not IE
		if(!Browser()) {
			document.getElementById(lrgId).style.marginLeft = width;
		}
	}
}

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 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 moneyFilter(key, textbox) {			
	var dFilterNum = textbox.value;
	var period = 0;
	if(dFilterNum.indexOf(".") != -1) {
			period++;
	}	
	if(key == 9) {
        return true;
    }
	else if(key == 8 && textbox.value.length != 0) {
		if(dFilterNum.indexOf(".") == (dFilterNum.length-1)) {
			period--;
		}
		dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);		
	}
	if(dFilterNum.indexOf(".") != -1 && (dFilterNum.length - dFilterNum.indexOf(".")) == 3) {
		return false;
	}	
 	else if((key > 47 && key < 58) || (key > 95 && key < 106) || key == 110  || key == 190) {
		switch(key) {
            case 96:
                dFilterNum += 0;
                break;
            case 97:
                dFilterNum += 1;
                break;
            case 98:
                dFilterNum += 2;
                break;
            case 99:
                dFilterNum += 3;
                break;
            case 100:
                dFilterNum += 4;
                break;
            case 101:
                dFilterNum += 5;
                break;
            case 102:
                dFilterNum += 6;
                break;
            case 103:
                dFilterNum += 7;
                break;
            case 104:
                dFilterNum += 8;
                break;
            case 105:
                dFilterNum += 9;
                break;
			case 110:
			case 190:
				if(period == 0) {
					dFilterNum += ".";
					//period++;
				}				
				break;
            default:
                dFilterNum += String.fromCharCode(key);
                break;
		}
	}    
    textbox.value = dFilterNum;
    return false;
}

function formatCurrency(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) {
        num = "0";
    }
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if(cents < 10) {
        cents = "0" + cents;
    }
    for(var i=0; i<Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0,num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }
    return(((sign) ? '' : '-') + '$' + num + '.' + cents);
}

function formatPercentage(num) {
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num)) {
        num = "0";
    }    
    num *= 100;
    return num + "%";
}

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 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 if(condition.indexOf("|") != -1) {			    
				document.getElementById(containerid).innerHTML = xmlhttp.responseText; // xmlhttp.responseText object contains the text of 'url'
				var id = condition.substring(0, condition.indexOf("|"));
			    var file = condition.substring(condition.indexOf("|") + 1, condition.length);
			    getMp3(id, file);
			}
			else {							
				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>";
	}
}

// add text to field and position cursor at the end
function setCaretToEnd(el) {
    if(el.createTextRange) {
        var v = el.value;
        var r = el.createTextRange();
        r.moveStart('character', v.length);
        r.select();
    }
}
function insertAtEnd(el, txt) {
    el.value += txt;
    setCaretToEnd(el);
}

function swap(id) {
    id = id.replace("ddlRank","hidSwap");
    document.getElementById(id).value = true;
}

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;
}

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 clickLink(id) {
    var link = document.getElementById('img' + id);    

    // *********************************
    // FF/NN accept programmed events,
    // but default action will be taken only
    // from hardvare device input:
    if (document.createEvent) { // FF model
        var customClick = document.createEvent('MouseEvents');
        customClick.initEvent('click',0,0);
        link.dispatchEvent(customClick);
        // The old good click() is removed from link methods:
        try {link.click();} catch(e){}
    }
    // *********************************
    // IE accepts programmed events,
    // but default action will be taken only
    // from hardvare device input (like FF/NN):
    else if (document.createEventObject) { // IE model
        var customClick = document.createEventObject();
        link.fireEvent('onclick', customClick);
        // The old good click() was simply forgotten
        // by the brave IE team and allows to bypass security:
        link.click(); // equals to a hardware click
    }
}



try {
    window.addEvent('domready', function() {    
        // remove link functionality from tabs if javascript is anabled        
        $$("a.tabLink").each(function(el) {	
		    el.addEvent('click', function(e) {
		        e = new Event(e).stop();
		    });
        });
        // add the display comments to input, select, checkbox, radio, and textarea fields on focus and blur events
        $$("input").each(function(el) {	
		    el.addEvent('focus', function(e) {
		        showInfo(this.id);
		    });
        });   
        $$("select").each(function(el) {	
		    el.addEvent('focus', function(e) {
		        showInfo(this.id);
		    });
        }); 
        $$("textarea").each(function(el) {	
		    el.addEvent('focus', function(e) {
		        showInfo(this.id);
		    });
        });        
        $$("input").each(function(el) {	
		    el.addEvent('blur', function(e) {
		        hideInfo(this.id);
		        showEmailError(this.id,this.value);
		    });
        });
        $$("select").each(function(el) {	
		    el.addEvent('blur', function(e) {
		        hideInfo(this.id);
		    });
        });
        $$("textarea").each(function(el) {	
		    el.addEvent('blur', function(e) {
		        hideInfo(this.id);
		    });
        });
    });
} catch(e) {}

function showInfo(id) {
    try {
        try {
            if(document.getElementById(id + "_alert").style.display == "none") {
                document.getElementById(id + "_info").style.display = "block";
            }
        } 
        catch(e) {
            document.getElementById(id + "_info").style.display = "block";
        }
    } catch(e) {}
}

function hideInfo(id) {
    try {
        document.getElementById(id + "_info").style.display = "none";
    } catch(e) {}
}

function isValidEmail(str) {
    if(str == "") {
        return true;        
    }
    var re = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(re);
    return regex.test(str);
}

function showEmailError(id, str) {
    try {
        if(!isValidEmail(str)) {
            document.getElementById(id + "_alert").style.display = "block";
        }
        else {
            document.getElementById(id + "_alert").style.display = "none";
        }
    } catch(e) {}
}

function showWinnerYear(year) {
    $$(".winner").each(function(el) {
        el.style.display = "none";
    });
    document.getElementById("winner" + year).style.display = "block";
}

function showWinnerCategory(id) {
    $$(".hideCategory").each(function(el) {
        if(el.id != "hide" + id) {            
            el.click();
        }
    });
    var linkId = "";
    $$(".category" + id).each(function(el) {
        if(el.title.indexOf("Expand") != -1) {
            el.title = el.title.replace("Expand", "Collapse");
            el.className = el.className.replace("down", "up");
            linkId = el.id;
        }
    });
    $$(".categoryLink").each(function(el) {
        if(el.id != linkId) {
            el.title = el.title.replace("Collapse", "Expand");
            el.className = el.className.replace("up", "down");
        }
    });            
    document.getElementById("toggle" + id).click();    
}


//Decodes Base64 formated data
function bd(data){
	data = data.replace(/[^a-z0-9\+\/=]/ig, '');// strip none base64 characters
	if (typeof(atob) == 'function') return atob(data);//use internal base64 functions if available (gecko only)
	var b64_map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
	var byte1, byte2, byte3;
	var ch1, ch2, ch3, ch4;
	var result = new Array();
	var j=0;
	while ((data.length%4) != 0) {
		data += '=';
	}
	
	for (var i=0; i<data.length; i+=4) {
		ch1 = b64_map.indexOf(data.charAt(i));
		ch2 = b64_map.indexOf(data.charAt(i+1));
		ch3 = b64_map.indexOf(data.charAt(i+2));
		ch4 = b64_map.indexOf(data.charAt(i+3));

		byte1 = (ch1 << 2) | (ch2 >> 4);
		byte2 = ((ch2 & 15) << 4) | (ch3 >> 2);
		byte3 = ((ch3 & 3) << 6) | ch4;

		result[j++] = String.fromCharCode(byte1);
		if (ch3 != 64) result[j++] = String.fromCharCode(byte2);
		if (ch4 != 64) result[j++] = String.fromCharCode(byte3);	
	}

	return result.join('');
}