/*******************************************************************************************************************
Javascript Library

	addToNewsvine(url, headline)
		- Adds story to newsline
	ajax_makeRequest
		- Standard AJAX caller
	formatCurrency(num)
		- Given a number, formats it as currency
	highlight(sid)
		- Used for onmouseover highlights of table rows
	hover_over(oRow)
	hover_out(oRow)
	isValidEmail(obj)
	link_over(oLink)
	link_out(oLink)
	quickMod(id, action)
	expand
	collapse

*******************************************************************************************************************/

	/*** GLOBAL VARIABLES ***/
	// Set OverLib Variables
	ol_width = 300;
	ol_offsety = 20;
	ol_cellpad = 0;
	ol_border=0;

	function addToNewsvine(url, headline) {
		var h = 480;
		var w = 590;
		var t = (screen.height - h) / 4;
		var l = (screen.width - w) / 4;

		window.open('/cgi-bin/ntlinktrack.cgi?http://www.newsvine.com/_wine/save?u='+url+'&amp;h='+headline, 'Newsvine', 'resizable=yes,scrollbars=yes,width='+w+',height='+h+',top='+t+',left='+l)
	}

	function ajax_makeRequest(url, parameters) {
		var httpRequest;

		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			httpRequest = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) { // IE
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

		if (!httpRequest) {
			alert('Invalid httpRequest object');
			return false;
		}

		if (parameters == '') {
			method = 'GET';
		} else {
			method = 'POST';
		}
		httpRequest.onreadystatechange = function() {ajax_exec(httpRequest);};
		httpRequest.open(method, url, true);
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-length", parameters.length);
		httpRequest.send(parameters);
	}

	function createAJAXRequest(url, parameters, responseFunction) {
		var httpRequest;

		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			httpRequest = new XMLHttpRequest();
		} else if (window.ActiveXObject) { // IE
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

		if (!httpRequest) {
			alert('Invalid httpRequest object');
			return false;
		}

		if (parameters == '') {
			method = 'GET';
		} else {
			method = 'POST';
		}
		httpRequest.onreadystatechange = function() {eval(responseFunction);}
		httpRequest.open(method, url, true);
		httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpRequest.setRequestHeader("Content-length", parameters.length);
		httpRequest.send(parameters);
	}

	function OLDajax_makeRequest(url) {
		var httpRequest;

		if (window.XMLHttpRequest) { // Mozilla, Safari, ...
			httpRequest = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) { // IE
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {}
			}
		}

		if (!httpRequest) {
			alert('Invalid httpRequest object');
			return false;
		}
		httpRequest.onreadystatechange = function() {ajax_exec(httpRequest);};
		httpRequest.open('GET', url, true);
		httpRequest.send('');
	}

	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 highlight(sid) {
		oCheckbox = document.getElementById('c'+ sid);
		oRow = document.getElementById('r'+ sid);
		if (oCheckbox.checked) {
			oRow.style.backgroundColor = '#c0c0c0';
		} else {
			oRow.style.backgroundColor = '';
		}
	}

	var prevColor = new Array();
	var adjustment = 16;  // Adjust colors
	function hover_over(oRow) {
		for (x = 0; x < oRow.cells.length; x++) {
			bgColor = oRow.cells[x].style.backgroundColor;
			if (bgColor == 'transparent' || bgColor == '') { // Get background color
				bgColor = document.bgColor;
				if (bgColor == 'transparent' || bgColor == '') { // Set color = white
					bgColor = '#FFFFFF';
				}
			}
			prevColor[x] = bgColor;

			if (bgColor[0] == '#') {
				bgc1 = parseInt(bgColor.substr(1,2), 16) - adjustment;
				bgc2 = parseInt(bgColor.substr(3,2), 16) - adjustment;
				bgc3 = parseInt(bgColor.substr(5,2), 16) - adjustment;
			} else if (bgColor[0] == 'r') {
				lp = bgColor.indexOf('(');
				rp = bgColor.indexOf(')');
				bgColor = bgColor.substring(lp+1, rp);
				bgColor = bgColor.split(', ');
				bgc1 = parseInt(bgColor[0]) - adjustment;
				bgc2 = parseInt(bgColor[1]) - adjustment;
				bgc3 = parseInt(bgColor[2]) - adjustment;
			} else {
				bgc1 = 255 - adjustment;
				bgc2 = 255 - adjustment;
				bgc3 = 255 - adjustment;
			}

			// Ensure that we're not out of bounds
			if (bgc1 < 0) {bgc1 = 0}
			if (bgc2 < 0) {bgc2 = 0}
			if (bgc3 < 0) {bgc3 = 0}

			// Pad the number
			bgc1 = bgc1.toString();
			bgc2 = bgc2.toString();
			bgc3 = bgc3.toString();

			oRow.cells[x].style.backgroundColor = 'rgb('+ bgc1 +', '+ bgc2 +', '+ bgc3 +')';
		}
//		alert(oRow.cells[0].style.backgroundColor);
	}

	function hover_out(oRow) {
//		alert(prevColor[0]);
		for (x = 0; x < oRow.cells.length; x++) {
			oRow.cells[x].style.backgroundColor = prevColor[x];
		}
	}

	function isValidEmail(obj) {
		/*
			Inputs:	obj - Any valid form element (input type="text")
			Returns:	True, if obj.value is a valid email address, False otherwise.
			Actions:	Throws focus to field on False.
		*/

		var re = /\w+@\w+\.\w+/;
//		var re = /^\w+((-\w+)(\.\w+))*\@[A-Za-z0-9]+((\.-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
		if ( (obj.value == '') || (!obj.value.match(re)) ) {
			alert('You must enter a valid Email Address!.');
			obj.focus();
			return false;
		}
		return true;
	}

	function link_over(oLink) {
		var lnk = oLink.href.substr(oLink.href.indexOf('?') + 1);
		window.status = lnk;
		return true;
	}

	function link_out(oLink) {
		window.status = '';
		return true;
	}

	function quickMod(id, action) {
		frmQM.id.value = id.toString();
		frmQM.action.value = action;
		frmQM.submit();
	}

	function setCookie(name, value, days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; domain=.hdtvmagazine.com; path=/";
	}

	function getCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function eraseCookie(name) {
		setCookie(name,"",-1);
	}

	function expand_collapse(id, flg_setcookie) {
		divBody = document.getElementById(id+'_body');
		if (divBody.style.display == 'none') {
			Effect.BlindDown(id+'_body');
			if (flg_setcookie) setCookie(id+'_showhide', 'show', 365);
		} else {
			Effect.BlindUp(id+'_body');
			if (flg_setcookie) setCookie(id+'_showhide', 'hide', 365);
		}
	}
