// Function used onsubmit to determine if we should submit a form into a new window or not
function popupform(myform, windowname, viewtype)
{
	showscroll = 'yes';
//	if (viewtype == 'pdf')
//	{
		showscroll = 'no';
//	}

	try 		{ allowPopup = allowPopup}
	catch(err) 	{ allowPopup = true}


	if (viewtype == 'nextstep' || viewtype == 'prevstep' || viewtype == 'save' || allowPopup == false)
	{
		myform.target='_self';
	}
	else if (viewtype == 'inline')
	{
			myform.target='preview_iframe';

	}
	else
	{
		if (! window.focus)return true;
		// Close any processing bars
		//document.getElementById('file_upload').style.display = 'none';
		//document.getElementById('translucent').style.display = 'none';
		// Open the window
		
		// Lets check & see if any images are being uploaded
		uploading = false;
		for(i=0; i < myform.elements.length; i++)
		{
			if (myform.elements[i].type == 'file' && myform.elements[i].value != '')
			{
				uploading = true;
				
			}
   		}
   
		if (viewtype == 'pdf' || uploading)
		{
			window.open('', windowname, 'height=600,width=800,scrollbars=' + showscroll);
			myform.target=windowname;
		}
		else
		{
			show_viewtype = viewtype == 'pdf' ? 'PDF' : 'Low Res JPEG';
			TB_show(show_viewtype + ' Proof', '/blank.html?TB_iframe=true&height=505&width=675', 'group');
			myform.target='TB_iframeContent';
		}
		//
		return true;
	}
}

function numberformat(number)
{
	number += '';
	inD = '.';
	outD = '.';
	sep = ',';
	var dpos = number.indexOf(inD);
	var nStrEnd = '';
	if (dpos != -1) {
		nStrEnd = outD + number.substring(dpos + 1, number.length);
		number = number.substring(0, dpos);
	}
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(number)) {
		number = number.replace(rgx, '$1' + sep + '$2');
	}
	return number + nStrEnd;
	
	//alert(number + nStrEnd);
}

// Function to de something such as a deletion with just a javascript confirmation, and optionally delete in the background
function confirmation(question,redir,hidden)
{
	confirmit = '';
	var confirmit = confirm(question);

	if(confirmit != false )
	{
		if(hidden==1)
		{
			hiddenframe.location.href=redir;

		}
		else
		{
			document.location.href=redir;
		}
	}
	return false;
}

function confirmation_del (this_id,decrement_id,cart_total)
{
	if(this_id > 0)
	{
		parent.document.getElementById('row-' +this_id).style.display ='none';
		if(decrement_id != '')
		{
			// Update the count number at the top
			d = parent.document.getElementById(decrement_id);
			value = d.innerHTML;
			d.innerHTML = d.innerHTML - 1;
		}
	}
	if(cart_total)
	{
		
		decrement_cart_total(cart_total);
	}
}

function decrement_cart_total (line_total)
{
	d = parent.document.getElementById('cart_price');
	if (d)
	{
		cart_price = d.innerHTML;
		cart_price = cart_price.replace(',','');
		d.innerHTML = numberformat(round_off(cart_price - line_total,2));
	}
	
	d = parent.document.getElementById('cart_total_price');
	if (d)
	{
		cart_price = d.innerHTML;
		cart_price = cart_price.replace(',','');
		d.innerHTML = numberformat(round_off(cart_price - line_total,2));
	}
}

function openWindow(filename,wide,high,popname,scrollbars)
{
	if(typeof(popname)=="undefined")
	{
		popname = 'popwin';
	}
	popname = popname.replace(' ','');
	if(typeof(scrollbars)=="undefined")
	{
		scrollbars = 0;
	}
	else
	{
		scrollbars = 1;
	}
//	alert(popname);
	popupWin = window.open(filename, popname, 'scrollbars='+scrollbars+',status=1,resizable=yes,width='+wide+',height='+high+'')
}

function proofWindow(url)
{
	window.open('/order/proof.php?' + url,'Proof','height=600,width=800,scrollbars=no');
}

function showhide(id)
{
	if (document.getElementById)
		document.getElementById(id).style.display=document.getElementById(id).style.display=='none'?'block':'none';
	else if (document.layers)
		document.layers[id].visibility=document.layers[id].visibility=='hidden'?'visible':'hidden';
	else
		document.all.id.style.display=document.all.id.style.display=='none'?'':'none';
}


function toggle(id1,id2)
{
	showhide(id1);
	showhide(id2);
}

function toggleImg(id, img1, img2)
{
	image = document.getElementById(id);
	image_src = image.src.replace(document.domain, '');
	image_src = image_src.replace(/http(s)?:\/\//, '');
	//alert(image_src + ' ' + img1 + ' ' + img2);
	image.src = image_src == img1 ? img2 : img1;
}
function round_off(number,precision)
{
	if (isNaN(number)) return '';
	var number = (((number*Math.pow(10,precision)) / Math.pow(10,precision).toString()) + '.0').split('.');
	return precision?number[0]+'.'+((number[1]+'0000000000').substr(0,precision)):number[0];
}

function round_off_clear(number,precision) {// return empty if value is zero
	var number = round_off(number,precision);
	if (number == '' || parseFloat(number) == 0) return '';
	return number;		
}

function currency_format(f)
{
	if (f.value=='') return;
	f.value = round_off(f.value,2);
}

function toggle_demo(id,opt)
{
	div	= document.getElementById('demos-' + id);
	img	= document.getElementById('img-' + id);
	view	= img.title;
	display = div.style.display;
	if (display == 'none')
	{
		div.style.display	= 'block';
		img.src 		= '/images/icons/expand-minus-open.gif';
		img.title 		= 'Collapse';
		save_view		= 'Expand';
		
	}
	else
	{
		div.style.display 	= 'none';
		img.src			= '/images/icons/expand-plus.gif';
		img.title 		= 'Expand';
		save_view		= 'Collapse';
	}
	if (opt == 'form')
	{
		document.getElementById('demo_view-' + id).value = save_view;
	}
}

// Function to check if a variable is set
function isset(variable)
{
    var undefined;
    return ( variable == undefined ? false : true );
}

// Function to check if a variable is a number
function is_numeric(variable)
{
	var validChars = "0123456789.-";
	var character;
	var result = true;
	
	if (variable.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < variable.length && result == true; i++)
	{
		character = variable.charAt(i);
		if (validChars.indexOf(character) == -1)
		{
			result = false;
		}
	}
	return result;
}

// Function to copy shipping address to billing address
function copy_address(copy)
{
	fields = new Array('name','company','address_1','address_2','city','state','zip','phone');
	copy_to 	= 'shipping';
	copy_from	= 'billing';
	if (copy == 'billing')
	{
		copy_to 	= 'billing';
		copy_from	= 'shipping';
	}
	//field_same = document.getElementById(copy_to + '_same');

	//if (field_same.checked)
	//{
		for (i=0; i<fields.length; i++)
		{
			document.getElementById(copy_to + '_'+fields[i]).value = document.getElementById(copy_from + '_'+fields[i]).value;
		}
	//}
	//thisBox = document.getElementById('bill_addy');
	//thisBox.options[thisBox.options.length]	= new Option('Other',0); 
	//thisBox.selectedIndex = thisBox.options.length-1;	
}

// Check or uncheck all checkboxes on the page
function checkUncheckAll(theElement,checkit)
{
	if (typeof(checkit) == 'undefined')
	{
		var checkit = theElement.checked;
		var theForm = theElement.form, z = 0;
	}
	else
	{
		var theForm = theElement, z = 0;
	}

	if (checkit == 'checked')
	{
		checkit = true;
	}
	while (theForm[z])
	{
		if(theForm[z].type == 'checkbox')
		{
			theForm[z].checked = checkit;
		}
		z++;
	}
}
// Function highlights the selected row when appropriate checkbox is checked
function chkoxHlRow(rowid,defaultclass)
{
	var row = document.getElementById('row_'+rowid);
	
	if (document.getElementById('checkbox_'+rowid).checked == true)
	{
		row.className = 'highlightall' + (defaultclass != '' ? '-'+defaultclass:'');
	}
	else
	{
		row.className = defaultclass;
	}
}
// Highlights all rows in a table
function hlAllRows(table)
{
	table 	= document.getElementById(table);
	for (var c = 0; c < table.rows.length; c++)
	{
		rowid 		= table.rows[c].id;
		rowid = rowid.replace(/row_/g,'');
		defaultclass 	= table.rows[c].className;
		defaultclass	= defaultclass.replace(/highlightall(-)?/g,'');
		
		if (rowid != '')
		{
			chkoxHlRow(rowid,defaultclass);
		}
	}	
}

// Function to de something such as a deletion with just a javascript confirmation, and optionally delete in the background
function confirmation(question,redir,hidden)
{
	confirmit = '';
	var confirmit = confirm(question);

	if(confirmit != false )
	{
		if(hidden==1)
		{
			hiddenframe.location.href=redir;

		}
		else
		{
			document.location.href=redir;
		}
	}
	return false;
}


function highlight(action,id)
{
	// Function to highlight selected row & column in an "L" format
	var ids = new Array();
	ids = id.split('-');
	
	
	if (action == 's')
		changeClass = 'color1';
	else
	{
		changeClass = '';
		// Remove style from the active cell
		document.getElementById(id).className = '';
	}	
	// Highlight column
	for (i = ids[1]; i > 0; i--)
	{
		this_id = ids[0] + '-' + i + '-' + ids[2];
		document.getElementById(this_id).className=changeClass;
	}
	// Highlight row
	for (i = ids[2]; i > 0; i--)
	{
		this_id = ids[0] + '-' + ids[1] + '-' + i;
		document.getElementById(this_id).className=changeClass;
	}
	if (action == 's')
	{
		// Apply style to the active cell
		document.getElementById(id).className = 'active-cell';
	}	
}

// Change the opacity for different browsers
function changeOpac(opacity, id, hide)
{
	var object 		= document.getElementById(id).style;
	object.opacity 		= (opacity / 100);
	object.MozOpacity 	= (opacity / 100);
	object.KhtmlOpacity 	= (opacity / 100);
	if (opacity == 100)
	{
		object.filter	= '';
	}
	else
	{
		object.filter 		= "alpha(opacity=" + opacity + ")";
	}
	// Optionally hide the layer if opacity is 0
	if (hide == 'hide' && opacity == 0)
	{
		document.getElementById(id).style.display = 'none';
	}
} 

// Get all elements by class name
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

// Cool tooltips
var tipper = {
	tipdiv : '',
	tipTitles : new Array(),
	toolTip : function (obj, action)
	{
		offset = new Array();
		offset['x'] = 0;
		offset['y'] = 20;
		tipdiv = document.getElementById('tooltip');
	
		if (action != 'hide')
		{
			if (tipdiv == undefined)
			{
				// Create our tipdiv
				tipdiv 		= document.createElement('div');
				tipdiv.id 	= 'tooltip';
				document.getElementsByTagName('body')[0].appendChild(tipdiv);
			}
		
			position = findPos(obj);
			// Remember our title by using the querystring from out link since it will normally be unique & we hafta remove the title from the link so we don't get the browser tooltip popping up
			if (obj.href != undefined && this.tipTitles[obj.href] != undefined && this.tipTitles[obj.href])
			{
				title = this.tipTitles[obj.href];
			}
			else
			{
				title = obj.title;
				
				if (title != '')
				{
					this.tipTitles[obj.href] = title;
				}
			}
			
			obj.removeAttribute('title');
			
			// Define div contents
			tipdiv.innerHTML = '<p>' + title + '</p>';
			// Set div position
			tipdiv.style.left	= position[0] + offset['x'] + 'px';
			tipdiv.style.top 	= position[1] + offset['y'] +'px';
			// Set visibility
			tipdiv.style.visibility = 'visible';
			tipdiv.style.opacity = '.1';
			// Fade it!
			this.fade(10);
		}
		else
		{
			tipdiv.style.visibility = 'hidden';
		}
		
	},
	
	
	fade: function (opac)
	{	
		var passed 	= parseInt(opac);
		var newOpac 	= parseInt(passed+10);
		if ( newOpac < 80 )
		{
			tipdiv.style.opacity = '.'+newOpac;
			tipdiv.style.filter = "alpha(opacity:"+newOpac+")";
			opacityID = window.setTimeout("tipper.fade('"+newOpac+"')",20);
		}
		else
		{ 
			tipdiv.style.opacity = '.80';
			tipdiv.style.filter = "alpha(opacity:80)";
			clearTimeout(opacityID);
		}
	}

};
// Find position of an object on the page.  needed by tooltip func
function findPos(obj)
{
	var curleft = curtop = curright = curbottom = curwidth = currheight = 0;
	if (obj.offsetParent)
	{
		curleft 	= obj.offsetLeft;
		curtop 		= obj.offsetTop;
		curwidth 	= obj.offsetWidth;
		curheight 	= obj.offsetHeight;
		while (obj = obj.offsetParent)
		{
			curleft 	+= obj.offsetLeft;
			curtop 		+= obj.offsetTop;
		
		}
		curright 	= curwidth + curleft;
		curbottom 	= curheight + curtop;
	}
	return [curleft,curtop,curright,curbottom,curheight,curwidth];
}

var gSafeOnload = new Array();
function SafeAddOnload(f)
{
	if  (window.onload)
	{
		if (window.onload != SafeOnload)
		{
			gSafeOnload[0] = window.onload;
			window.onload = SafeOnload;
		}		
		gSafeOnload[gSafeOnload.length] = f;
	}
	else
		window.onload = f;
}
function SafeOnload()
{
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload[i]();
}

