// *****************************************************************************
// General Functions
// *****************************************************************************
function open_window(link,w,h) {
	w = w + 20;
	h = h + 20;
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=no,scrollbars=no";
	var wishWin = window.open(link,'popupwin',win);
	return wishWin;
}
function open_window_named(winname,link,w,h) {
	w = w + 20;
	h = h + 20;
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=no,scrollbars=no";
	var wishWin = window.open(link,winname,win);
	return wishWin;
}

function open_picture(link,header,w,h) {
	w = w + 22;
	h = h + 60;
	var win = "width="+w+",height="+h+",menubar=no,location=no,resizable=no,scrollbars=no";
	var wishWin = window.open('','largethumb',win);
	wishWin.document.clear();
	wishWin.document.write('<html><body topmargin="10" leftmargin="10" rightmargin="10" bgcolor="#FFFFFF" text="#000000">');
	wishWin.document.write('<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr>');
	wishWin.document.write('<td nowrap width="100%">&nbsp;</b></td>');
	wishWin.document.write('<td><table border="0" cellspacing="1" cellpadding="0" bgcolor="#000000"><tr>');
	wishWin.document.write('<td bgcolor="#FFFFFF" nowrap width="40" align="center" onmouseover="this.style.backgroundColor=\'#6CB6FF\';" onmouseout="this.style.backgroundColor=\'#FFFFFF\';" style="cursor:pointer" onclick="javascript:window.close();"><font size="1">&nbsp;Close&nbsp;</font></td>');
	wishWin.document.write('</tr></table></td></tr></table><table width="100%" border="0" cellspacing="1" cellpadding="0" bgcolor="#000000"><tr><td align="center" bgcolor="#FFFFFF">');
	wishWin.document.write('<img src=\''+link+'\'></td></tr></table><b><font face="Arial" size="1">'+header+'</font></body></html>');
	wishWin.document.close();
}
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}
function savescrollbarloaction(){
	if (navigator.appName == "Microsoft Internet Explorer") {
		document.cookie="scrollx="+ document.body.scrollLeft;
		document.cookie="scrolly="+ document.body.scrollTop;
	}	else {
		document.cookie="scrollx="+ window.pageXOffset;
		document.cookie="scrolly="+ window.pageYOffset;
	}
}
function loadscrollbarloaction(){
	if (navigator.appName == "Microsoft Internet Explorer") {
		document.scrollTo(getCookie("scrollx"),getCookie("scrolly"));
	}	else {
		window.scrollTo(getCookie("scrollx"),getCookie("scrolly"));
	}
}

function printlayer(lname) {
	var txt = document.getElementById(lname).innerHTML;
	var wishWin=open_window('',800,600);
	wishWin.document.open();
	wishWin.document.write(txt);
	wishWin.document.close();
	wishWin.window.print();
	// the previous line may need to be...
	// document.print();
}

function resizeiFrame(elem)
{
  var the_height=elem.contentWindow.document.body.scrollHeight;
  elem.height=the_height;
}

// *****************************************************************************
// Conversion Calculators
// *****************************************************************************
function calculate_temp(field_c,field_f,t) {
	var c = document.getElementById(field_c);
	var f = document.getElementById(field_f);
	if (c == "") { c.value = 1;	}
	if (f == "") { f.value = 1;	}
	if (t == "1") {
		var z = ( ( c.value * 9 ) / 5 ) + 32;
		f.value = z.toFixed(2);
	} else {
		var z = ( ( f.value - 32 ) * 5 ) / 9;
		c.value = z.toFixed(2);
	}
}
function calculate_volume(field_l,field_g,t) {
	var l = document.getElementById(field_l);
	var g = document.getElementById(field_g);
	if (l == "") { l.value = 1; }
	if (g == "") { g.value = 1; }
	if (t == "1") {
		var z = l.value * 0.2642;
		g.value = z.toFixed(2);
	} else {
		var z = g.value * 3.785;
		l.value = z.toFixed(2);
	}
}
function calculate_length(field_m,field_i,t) {
	var m = document.getElementById(field_m);
	var i = document.getElementById(field_i);
	if (m == "") { m.value = 1; }
	if (i == "") { i.value = 1; }
	if (t == "1") {
		var z = m.value * 0.0393700787;
		i.value = z.toFixed(2);
	} else {
		var z = i.value / 0.0393700787;
		m.value = z.toFixed(2);
	}
}
function calculate_tankvolume(field_x,field_y,field_z,field_l,field_g) {
	var x = document.getElementById(field_x);
	var y = document.getElementById(field_y);
	var z = document.getElementById(field_z);
	var l = document.getElementById(field_l);
	var g = document.getElementById(field_g);
	if (x == "") { x.value = 1;	}
	if (y == "") { y.value = 1;	}
	if (z == "") { z.value = 1;	}
	var a = x.value * y.value * z.value / 1000000;
	l.value = a.toFixed(2);
	a = l.value * 0.2642;
	g.value = a.toFixed(2);
}


// *****************************************************************************
// Site Config Functions
// *****************************************************************************
function do_searchcat(catid){
	document.cookie="searchtype=i";
	document.cookie="wordsearch=";
	document.cookie="itemsearch="+catid;
	location.replace("/shop_list.php?searching_category="+catid);
}
function do_searchpid(pid){
	document.cookie="pid="+pid;
	location.href="./shop_item.php?searching_product="+pid;
}
function do_emptybasket(){
	document.cookie="basketaction=empty";
	savescrollbarloaction();
	location.replace(window.location.pathname);
}
function do_addtobasket(pid){
	document.cookie="basketaction=increase";
	document.cookie="pid="+pid;
	savescrollbarloaction();
	location.replace(window.location.pathname);
}
function do_removefrombasket(pid){
	document.cookie="basketaction=decrease";
	document.cookie="pid="+pid;
	savescrollbarloaction();
	location.replace(window.location.pathname);
}
function do_togglepid(pid){
	document.cookie="basketaction=toggle";
	document.cookie="pid="+pid;
	savescrollbarloaction();
	location.replace(window.location.pathname);
}
function expMenu(id) {
	var tr=document.getElementsByTagName("tr");
	for(var i=0;i<tr.length;i++){
		if (tr[i].id == "tr_" + id){
			if(tr[i].style.display == "none") {
				tr[i].style.display = "";
				tr[i].visibility = "show";
				var img=document.getElementsByTagName("img");
				for(var j=0;j<img.length;j++){
					if (img[j].id == "img_" + id){
						img[j].src="./images/minus.gif";
					}
				}
			}	else {
				tr[i].style.display = "none";
				tr[i].visibility = "hide";
				var img=document.getElementsByTagName("img");
				for(var j=0;j<img.length;j++){
					if (img[j].id == "img_" + id){
						img[j].src="./images/plus.gif";
					}
				}
			}
		}
	}
}

function hidealltables(){
	document.cookie="showcategorydetailtable=0";
	document.cookie="showproducttable=0";
	document.cookie="showproductdetailtable=0";
	
	document.cookie="addcategory=0";
	document.cookie="parentcatid=-1";
}
function selectcat(mydoc,catid,parentcat){
	mydoc.getElementById('cat_detail').src='/includes/shop_admin_catdetail.php?catid='+ catid +'&parentcat='+ parentcat;
	mydoc.getElementById('product_list').src='/includes/shop_admin_productlist.php?catid='+ catid;
}
function selectproduct(pid){
	savescrollbarloaction();
	document.cookie="selecttype=product";
	document.cookie="selectpid=" + pid;
	document.cookie="adminbuttonid=" + pid;
	hidealltables();
	document.cookie="adminpage=products";
	document.cookie="adminbuttonpress=editproduct";
	location.reload();
}
function selectproject(pid){
	savescrollbarloaction();
	document.cookie="selecttype=projects";
	document.cookie="selectpid=" + pid;
	document.cookie="adminbuttonid=" + pid;
	hidealltables();
	document.cookie="adminpage=projects";
	if (pid == -1) {
		document.cookie="adminbuttonpress=addproject";
	} else {
		document.cookie="adminbuttonpress=editproject";
	}
	location.reload();
}
function makeselection(option){
	savescrollbarloaction();
	hidealltables();
	if (option=="addcategory") {
		document.cookie="selecttype=cat";
		document.cookie="parentcatid=" + getCookie("selectcatid");
		document.cookie="selectcatid=-1";
		document.cookie="showcategorydetailtable=1";
		document.cookie="addcategory=1";
		location.reload();
	}
	if (option=="savecategory") {document.cookie="savecategory=1";document.f_cat.submit();}
	if (option=="deletecategory") {
		var temp = window.confirm('Are you sure you want to delete this category? All products will be moved to this categories parent.');
		if (temp) {document.cookie="deletecategory=1";document.f_cat.submit();}
	}
	if (option=="addproduct") {
		document.cookie="selecttype=product";
		document.cookie="selectpid=-1";
		document.cookie="showproductdetailtable=1";
		document.cookie="addproduct=1";
		document.cookie="saveproduct=0";
		document.cookie="deleteproduct=0";
		document.cookie="adminpage=productdetail";
		location.reload();
	}
	if (option=="saveproduct") {document.cookie="saveproduct=1";document.cookie="deleteproduct=0";document.cookie="adminpage=products";document.f_product.submit();}
	if (option=="deleteproduct") {
		var temp = window.confirm('Are you sure you want to delete this product?');
		if (temp) {document.cookie="saveproduct=0";document.cookie="deleteproduct=1";document.f_product.submit();}
	}
	if (option=="savenews") {document.cookie="savenews=1";document.cookie="deletenews=0";document.f_config_news.submit();}
	if (option=="deletenews") {
		var temp = window.confirm('Are you sure you want to delete this news item?');
		if (temp) {document.cookie="savenews=0";document.cookie="deletenews=1";document.f_config_news.submit();}
	}
	if (option=="logout") {document.location="./includes/shop_admin_logout.php";}
}
function deleteproject(pid){
	var temp = window.confirm('Are you sure you want to delete this project?');
	if (temp) {document.cookie="adminbuttonpress=deleteproject";document.cookie="deleteproject="+ pid;document.f_project.submit();}
}
function setsearchvars(searchtype){
	document.cookie="searchtype="+searchtype;
	if (searchtype=='w') {
		document.cookie="wordsearch="+document.fwordsearch.search.value;
		document.cookie="itemsearch=";
		document.cookie="page=-1";
	} else {
		document.cookie="wordsearch=";
		document.cookie="itemsearch="+document.fitemsearch.search.value;
		document.cookie="page=-1";
	}
}
function movespecial(pid,y){
	savescrollbarloaction();
	document.cookie="movespecialpid=" + pid;
	document.cookie="movespecialy=" + y;
	location.replace(window.location.pathname);
}
function savepricelist(catid){
	savescrollbarloaction();
	document.cookie="savepricelist=" + catid;
}

// *****************************************************************************
// Site Admin Functions
// *****************************************************************************
function change_admin_page(option){
	document.cookie="adminpage="+ option;
	document.cookie="adminbuttonpress=";
	document.cookie="selectcatid=-1";
	document.location="/shop_admin.php";
}
function admin_button_press(option,option1){
	document.cookie="adminbuttonpress=" + option;
	document.cookie="adminbuttonid=" + option1;
	document.location="/shop_admin.php";
}

function admin_button_press_confirm(option,option1,msg){
	var temp = window.confirm(msg);
	if (temp) { admin_button_press(option,option1); }
}

function deletecategory(option){
	var temp = window.confirm('Are you sure you want to delete this category?');
	if (temp) {
		parent.document.cookie="adminbuttonpress=deletecategory";
		parent.document.cookie="adminbuttonid="+ option;
		parent.document.location="/shop_admin.php";
	}
}

function deleteproduct(option,option1){
	var temp = window.confirm('Are you sure you want to delete this product?');
	if (temp) {
		parent.document.cookie="adminbuttonpress=deleteproduct";
		parent.document.cookie="adminbuttonid="+ option;
		document.location="shop_admin_productlist.php?catid="+ option1;
	}
}

function print_category(option){
	open_window('/includes/shop_admin_print_category.php?cid='+option,800,600);
}
// *****************************************************************************
// Site Config Functions
// *****************************************************************************
function admin_showconfigopt(x,id){
	document.cookie="adminpage=config";
	document.cookie="adminpageoption=" + x;
	window.location='/shop_admin.php';
}
// *****************************************************************************
// Spell Cheaker
// *****************************************************************************
var Spell_Win_Name;
var Work_Form_Name;
var Work_Field_Name;

function SpellCheck(form_name,field_name) 
{
  var textform = self.document[form_name][field_name].value;
  if ( ! Spell_Win_Name || Spell_Win_Name.closed || (Work_Form_Name != form_name || Work_Field_Name != field_name)) {
		Spell_Win_Name = window.open('','WIN','width=600,height=380,left=210,top=210,resizable=yes,scrollbars=yes,status=yes');
		if (Spell_Win_Name.focus) { Spell_Win_Name.focus(); }
		self.document.hidden_form.form_name.value = form_name;
		self.document.hidden_form.field_name.value = field_name;
		self.document.hidden_form.first_time_text.value = textform;
		self.document.hidden_form.submit();
		Work_Form_Name=form_name;
		Work_Field_Name=field_name;
	} else {
		if (Spell_Win_Name.focus) { Spell_Win_Name.focus(); }
  }
}


