﻿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 writeEmailUrl(email, domain, ext, url) {
    var at = "@";
    var dot = ".";
    var email = email + at + domain + dot + ext;
    document.write('<a href="' + url + '" title="' + email + '">' + email + '<\/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 = id + "Lrg";
    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;
		}
	}    
    setTimeout(function() {
        textbox.value = dFilterNum;
    },1);
    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;
    if(num.toString().indexOf(".") != -1) {
        num = num.toString().substring(0, num.toString().indexOf(".") + 3);
    }
    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 == "p") {
			    var temp = xmlhttp.responseText;
				temp = temp.substring(temp.indexOf('<table class="grid"'),temp.lastIndexOf("</table>")+8);
				document.getElementById(containerid).innerHTML = temp; // 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>";
	}
}

// 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 closeSmoothbox() {
    TB_remove();
}



function clickLink(id) {
    var link = document.getElementById(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
    }
}



