
/*#################################################################################
 * Reset all CAPTCHA instances on the page. No parameters are necessary as each
 * page should have only one CAPTCHA.
 #################################################################################*/
function reset_captcha() {
	//document.getElementById('captcha').innerHTML = "<img src=includes/function_captcha.php>";
	document.getElementById('captcha').src = 'images/progress.gif';
	timer = setTimeout("set_captcha();", 300);
}

function set_captcha()
{
	var rand = Math.floor(Math.random()*101)
	myimg=new Image()
	myimg.src = 'includes/function_captcha.php?id=' + rand;
	document.getElementById('captcha').src = myimg.src;
}

/*#################################################################################
 * Activates loader box and ensures loader animation doesn't freeze in Internet
 * Explorer.
 #################################################################################*/
function form_submit() {
	var load = document.getElementById('loadbox');
	document.form.submit();
	document.getElementById('loader').innerHTML = '<DIV id="loadbox"><SPAN class="general">Please Wait...</SPAN><BR><img src="images/pbar5.gif"></DIV>';
}

/*#################################################################################
 * Runs an AJAX script in ajax_click.php to record clicks on external links
 * 
 * Parameters:
 *		id => Link ID as used in maj_links table
 #################################################################################*/
function linkout(id) {
	var req = null;
	
	// Browser compatible AJAX connection
	if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		if (req.overrideMimeType) {
			req.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	};
	
	// Send link ID to ajax_click.php to record the action
	var queryString = "?id=" + id;
	req.open("GET", "includes/ajax_click.php" + queryString, true);
	req.send(null);
	return true;
}


/*#################################################################################
 * Runs an AJAX script in ajax_spam.php to report spam in the user comment boxes.
 * 
 * Parameters:
 *		media => Media type as used in maj_comments table
 *		id => The comment id
 *		url => A link to the page containing spam
 #################################################################################*/
function comspam(media, id, url) {
	var req = null;
	
	// Browser compatible AJAX connection
	if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
		if (req.overrideMimeType) {
			req.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	};
	
	// Run script to report spam
	var queryString = "?id=" + id + "&type=" + media + "&url=" + url;
	req.open("GET", "includes/ajax_spam.php" + queryString, true);
	req.send(null);
	
	// Change the link so that users can't report again
	var item = media + '_' + id;
	document.getElementById(item).innerHTML = "(<SPAN class=\"general\" style=\"font-size: 11px; color: red;\">Reported</SPAN>)";
}

/*#################################################################################
 * Enforces textareas to maintain specific character limits
 * 
 * Parameters:
 *		field => The textarea path "Example: this.form.field1 "
 *		maxlimit => Maximum characters allowed
 #################################################################################*/
function textCounter(field, maxlimit)
{
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
}

/*#################################################################################
 * Comment form validation; Makes submit button on comment forms available
 #################################################################################*/
function validate()
{
	if (document.commentform.comment.value.length > 0)
	{
		document.commentform.addcomment.disabled = false;
	}
	else
	{
		document.commentform.addcomment.disabled = true;
	}
}

/*######################################################################################
 * Return an array with the mouse cursor's X and Y position; Parameter is self-defined
 #######################################################################################*/
function getXY(e)
{
	var mousePos = new Array();
	xPos = (window.Event) ? e.pageX : event.clientX;
	yPos = (window.Event) ? e.pageY : event.clientY;
	
	mousePos[0] = xPos;
	mousePos[1] = yPos;
	
	return mousePos;
}

/*#################################################################################
 * Runs a simple Javascript that adds options to a select menu
 * 
 * Parameters:
 *		field => What field are we adding to?
 *		txt => The text to place between the OPTION tags
 *		val => The value of the option
 #################################################################################*/
function addOption(field, txt, val)
{
	var oSelect = field;
	nextOptIndex = oSelect.length + 1;
	newOpt = document.createElement( 'OPTION' );
	newOpt.text = txt;
	newOpt.value = val;
	oSelect.options.add( newOpt, nextOptIndex );
}

/*#################################################################################
 * This function is used with the get_dateform PHP function. It updates the drop
 * menu, giving each month its appropriate number of days
 #################################################################################*/
function setDays()
{
	var mfield = document.getElementById('monthSel');
	var dfield = document.getElementById('daysSel');
	var yfield = document.getElementById('yearSel');
	var select1 = dfield.selectedIndex;
	
	var theday = parseInt(dfield.options[dfield.selectedIndex].value);
	var themonth = parseInt(mfield.options[mfield.selectedIndex].value);
	var theyear = parseInt(yfield.options[yfield.selectedIndex].value);
	
	if (themonth == 1 || themonth == 3 || themonth == 5 || themonth == 7 || themonth == 8 || themonth == 10 || themonth == 12)
	{
		// Months with 31 days
		var days = 31 + 1;
	}
	else
	{
		// Months with 30 days
		var days = 30 + 1;
	}
	if (themonth == 2)
	{
		// Feb
		var days = 28 + 1;
		if (theyear != '--' && theyear % 4 == 0)
		{
			// Leap year!
			days = 29 + 1;
		}
	}
	
	// Clear days field
	dfield.innerHTML = '';
	addOption(dfield, '--', '');
	
	// Add options
	for(var i = 1; i < days; i++)
	{
		addOption(dfield, i, i);
	}
	
	// Reset day field back to original position
	if (dfield.options.length - 1 >= select1)
	{
		dfield.options[select1].selected = true;
	}
	else
	{
		select1 = dfield.options.length - 1;
		dfield.options[select1].selected = true;
	}
}

/* Check Flash Player version */
function MM_CheckFlashVersion(reqVerStr, msg)
{
	with(navigator)
	{
		var isIE  = (appVersion.indexOf("MSIE") != -1 && userAgent.indexOf("Opera") == -1);
		var isWin = (appVersion.toLowerCase().indexOf("win") != -1);
		if (!isIE || !isWin)
		{
			var flashVer = -1;
			if (plugins && plugins.length > 0)
			{
				var desc = plugins["Shockwave Flash"] ? plugins["Shockwave Flash"].description : "";
				desc = plugins["Shockwave Flash 2.0"] ? plugins["Shockwave Flash 2.0"].description : desc;
				if (desc == "")
				{
					flashVer = -1;
				}
				else
				{
					var descArr = desc.split(" ");
					var tempArrMajor = descArr[2].split (".");
					var verMajor = tempArrMajor[0];
					var tempArrMinor = (descArr[3] != "") ? descArr[3].split("r") : descArr[4].split("r");
					var verMinor = (tempArrMinor[1] > 0) ? tempArrMinor[1] : 0;
					flashVer =  parseFloat(verMajor + "." + verMinor);
				}
			}
			// WebTV has Flash Player 4 or lower -- too low for video
			else if (userAgent.toLowerCase().indexOf("webtv") != -1)
			{
				flashVer = 4.0;
			}

			var verArr = reqVerStr.split(",");
			var reqVer = parseFloat(verArr[0] + "." + verArr[2]);
			
			if (flashVer < reqVer)
			{
				if (confirm(msg))
				{
					window.open = "http:// www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
				}
			}
		}
	}
}

// Enforce Yes/No fields in eval form
function uncheck_yesno(obj)
{
	// Ucheck all numeric fields when NO is selected
	if (!obj)
	{
		alert(obj + ' does not exist');
		return false;
	}
	
	for (var i=0; i<obj.length; i++)
		obj[i].checked = false;
}

// Check the first numeric field when YES is selected
function check_yesno(obj)
{
	if (!obj)
	{
		alert(field + ' does not exist');
		return false;
	}
	
	for (var i=0; i<obj.length; i++)
	{
		if (obj[i].value==1)
		{
			obj[i].checked = true;
		}
	}
}

// Select YES when a number is selected
function yesselect(obj)
{
	if (!obj)
	{
		alert(field + ' does not exist');
		return false;
	}
	
	for (var i=0; i<obj.length; i++)
	{
		if (obj[i].value==1)
		{
			obj[i].checked = true;
		}
	}
}

/*######################################################################################
 * Prevent double-clicking submit button on billing form
 #######################################################################################*/
 function oneClick(obj)
 {
 	obj.disabled = true;
	document.billconfirm.submit();
 }
 
 /* ##### Collapsing Table ##### */
function collapseObj(e)
{
	var item = document.getElementById(e);
	
	if (item.style.display=='block')
	{
		item.style.display = 'none';
	}
	else if (item.style.display=='none')
	{
		item.style.display = 'block';
	}
	else
	{
		item.style.display = 'none';
	}
}


function viewVideo(url)
{
	var newwindow;
	newwindow=window.open(url,'poptastic','width=840,height=680,toolbar=no,status=no,scrollbars=yes,resizable=yes,menubar=no,location=no,direction=no');
	
	if (window.focus)
	{
		newwindow.focus();
	}
}
