function cacheBurst() { var cb = cb + 1;var s = Math.round(Math.random()*100000,0);return s;}

//Clock
var clockId = 0;
var serverDate = new Date();
function CreateClock(y, m, d, h, M, S) {serverDate = new Date(y, m, d, h, M, S);DisplayTime();clockID = setTimeout("UpdateClock()", 1000);}
function DisplayTime() {var hours = Number(serverDate.getHours());var mins = Number(serverDate.getMinutes());var sec = Number(serverDate.getSeconds());if (hours < 10) hours = "0" + hours;if (mins < 10) mins = "0" + mins;if (sec < 10) sec = "0" + sec;var serverClock = document.getElementById("serverClock");serverClock.innerHTML = "" + hours + ":" + mins + ":" + sec;}
function UpdateClock() {if(clockId) {clearTimeout(clockId);clockId=0;}serverDate.setTime(serverDate.getTime() + 1000);DisplayTime();clockId = setTimeout("UpdateClock()", 1000);}
function KillClock() {if(clockId) {clearTimeout(clockId);clockId = 0;}}

//Windows
function liveResults(){var resultsPopup;var tmp;if (resultsPopup){tmp=resultsPopup;resultsPopup=null;}resultsPopup=window.open('live-results.aspx', 'LiveResults','height=580,location=0,menubar=0,personalbar=0,scrollbars=1,status=0,toolbar=0,width=720,resizable=1,screenx=0,screeny=0,left=0,top=0');}


function MonthDays(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this;
}

function Feb (year){return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );}

function DateCheck(dday, mmonth, yyear){
    var daysInMonth = MonthDays(12);
	
	var month=parseInt(mmonth);
	var day=parseInt(dday);
	var year=parseInt(yyear);
	
	if ((year == -1) || (day == -1) || (month == -1)) {
		return false;
    }else {
    	if (dday.length<1 || day<1 || day>31 || (month==2 && day>Feb(year)) || day > daysInMonth[month]){
            return false;
        }
    }
   return true; 
}

function EmailCheck(field) {
	var good = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (good){return true} else {return false}
}

function UrlEncode(str) {
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    ret = encodeURIComponent(ret);
    for (search in histogram) {
        replace = histogram[search];
        tmp_arr = ret.split(search); // Custom replace
        ret = tmp_arr.join(replace); 
    }
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    return ret;
}


//Ajax
function updateBalance(updown)
{
    var textColor = "black";
    if (updown=="up") textColor = "green";
    else if (updown=="down") textColor = "red";
    else textColor = "black";
    var url = "/handlers/ajaxfunctions.ashx?command=getbalance&cb=" + cacheBurst();     
    $j.get(url, function(result){
        $j("#ctl00_login___lblBalance").fadeOut("slow").css("color",textColor).fadeIn("slow").html(result);
    },"text");
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (dblValue + '.' + strCents);
}


