
/*#################################################################################
 * 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)
{
	// Clip field down to its appropriate size
	if (field.value.length > maxlimit)
	{
		field.value = field.value.substring(0, maxlimit);
	}
	
	// Update charlimit count
	var newcount = maxlimit - field.value.length;
	document.getElementById('charlimit').innerHTML = newcount;
}

/*#################################################################################
 * 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 event
 #######################################################################################*/
function getXY(e)
{
	var posx = 0;
	var posy = 0;
	var mousePos = new Array();
	
	if (!e) var e = window.event;
	if (e.pageX || e.pageY)
	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	mousePos[0] = posx;
	mousePos[1] = posy;
	
	return mousePos;
}

/*##########################################
 * Makes an object appear at mouse position
 ###########################################*/
function show_layer(obj, e)
{
	// Set block to mouse position
	var myPos = getXY(e);
	document.getElementById(obj).style.left = myPos[0];
	document.getElementById(obj).style.top = myPos[1];
	
	document.getElementById(obj).style.display = 'block';
}

/*#####################################################
 * Let the photo loader box appear on form submission
 * Hack IE to ensure loader graphic doesn't freeze
 ######################################################*/
function setloader_dep()
{
	document.getElementById('loadbox').style.display = 'block';
	var myimg = new Image();
	myimg.src = 'images/pbar5.gif';
	
	var newimage = '<img src="' + myimg.src + '" />';
	document.getElementById('loadbox').innerHTML = '<span class="general">Loading...<br /></span>' + newimage;
}
 
function setimage()
{
	var rand = Math.floor(Math.random()*101);
	myimg=new Image();
	myimg.src = 'images/pbar5.gif?i=' + rand;
	document.getElementById('loading').src = myimg.src;
}
 
function setloader()
{
	document.getElementById('loadbox').style.display = 'block';
	var timer = setTimeout("setimage();", 50);
	document.addphoto.submit();
}

/*#################################################################################
 * 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;
	}
}

/*######################################################################################
 * Make the drop menus work for all major browsers
 #######################################################################################*/
function init_dropmenu()
{
	if(navigator.appVersion.indexOf("MSIE")==-1)
	{
		return;
	}
 
	var i, k, g, lg, r=/\s*menuhover/, nn='', c, cs='menuhover', bv='topmenu';
 
	for(i=0;i<10;i++)
	{
	 	g=document.getElementById(bv+nn);
		if(g)
		{
			lg=g.getElementsByTagName("LI");
			if(lg)
			{
				for(k=0;k<lg.length;k++)
				{
					lg[k].onmouseover=function()
					{
						c=this.className;
						cl=(c)?c+' '+cs:cs;
						this.className=cl;
					}
					
					lg[k].onmouseout=function()
					{
						c=this.className;
						this.className=(c)?c.replace(r,''):'';
					}
				 }
			 }
		}
		nn=i+1;
	}
}

/*######################################################################################
 * Manual checkout form; Syncs shipping fields with billing fields
 #######################################################################################*/
function syncShipping()
{
	var form1 = document.newbilling;
	form1.ship_name.value = form1.firstname.value + ' ' + form1.lastname.value;
	form1.ship_address1.value = form1.address1.value;
	form1.ship_address2.value = form1.address2.value;
	form1.ship_city.value = form1.city.value;
	form1.ship_zip.value = form1.zip.value;
	set_twin(form1.state, form1.ship_state);
}

function set_twin(src, obj)
{
	var select1 = src.selectedIndex;
	obj.options[select1].selected = true;
}

/*#################
 * Manage tooltips
 ##################*/
 function showtip(obj, e)
{
	// Make the box appear at the mouse position
	var myPos = getXY(e);
	document.getElementById(obj).style.left = myPos[0] - 300;
	document.getElementById(obj).style.top = myPos[1] + 10;
	document.getElementById(obj).style.display = 'block';
}
function hidetip(obj)
{
	document.getElementById(obj).style.display = 'none';
}