var picdesc="0";
var pnp=0.85;
var pnplim=18.00;
var imgtemplate="<img src=\"content\"><div><span class=imspan onClick=\"delimage('content')\">delete</span><span class=imspan onClick=\"closediv('medimagediv')\">close</span></div>";


var ferts=",TAMA1,CU501,CUK1,CAS1,SM01,SHSA,BON1,RPH1,DOL1,GYP1,HOH1,";
prices = new Array();
prices[0]=6;
prices[1]=0;
prices[2]=2.05;
prices[3]=6.25;
prices[4]=11;
prices[5]=16.50;
prices[6]=21;

function adjbasket(bcountjs){
	getTotalCost(bcountjs);
	discount(bcountjs);
}

function nondel(value,place){	
	if(isNaN(value) || parseInt(value) != value || value==0){
		alert("Please enter a valid quantity. If you wish to delete this item, please use the button on the right.");
		place.value=1;
	}
}


function formatAsMoney(mnt) {
    mnt -= 0;
    mnt = (Math.round(mnt*100))/100;
    return (mnt == Math.floor(mnt)) ? mnt + '.00' 
              : ( (mnt*10 == Math.floor(mnt*10)) ? 
                       mnt + '0' : mnt);
}


function discount(bcountjs){
	
	var fertquant=0;
	var disc=0;
	if(bcountjs){
		for(i = 1; i < 5*bcountjs; i += 5){
			code=document.valquant[i].value;
			quant=document.valquant[i+2].value;
			
			if(ferts.match(code)){
				fertquant+=parseInt(document.valquant[i+2].value);
			}
		}
		
		if(fertquant>1){
			if(fertquant>6){
				disc=(fertquant-6)*4+prices[6];
			}
			else{
				if(fertquant>1 && fertquant<=6){
					disc=prices[parseInt(fertquant)];
					}
			}
		}
	
		
		if(disc){
			document.carrier.discounted.value=formatAsMoney(disc);
		}
		else{
			document.carrier.discounted.value=0;
		}
			
		
		if(document.carrier.post){
			document.carrier.totals.value=formatAsMoney(parseFloat(document.carrier.totals.value)+parseFloat(document.carrier.post.value)-parseFloat(disc));
		}
		else{
			document.carrier.totals.value=formatAsMoney(parseFloat(document.carrier.totals.value)-parseFloat(disc));
			}
		document.valquant.forderval.value=document.carrier.totals.value;
		if(document.carrier.post)document.valquant.fpnp.value=formatAsMoney(document.carrier.post.value);
		document.valquant.fdiscount.value=formatAsMoney(disc);
	}
}

function round (n) {
    	n = Math.round(n * 100) / 100;
    	n = (n + 0.001) + '';
    	return n.substring(0, n.indexOf('.') + 3);
}

function mymenu(loc){
	if(loc != 'catalogue'){
		parent.document.getElementById('main').src=loc+'.htm';
	}
	else{
		parent.document.getElementById('main').src='pages/'+loc+'.php';
	}
}

function mymenu_nw(loc){
	location=loc;
}

function mydata_nw(status,prod,cat,typename,xprod){
	document.trans.status.value=status;
	document.trans.prod.value=prod;
	document.trans.xprod.value=xprod;
	document.trans.cat.value=cat;
	document.trans.typename.value=typename.replace(/_/ig," ");
	if(document.trans.action==''){
		document.trans.action='catalogue.php';
		}
	if(document.getElementById('shopwindow') && document.getElementById('shopwindow').scrollTop > 0){
		document.trans.vpos.value=document.getElementById('shopwindow').scrollTop;
		}
	
	trans.submit();
}

function mydata(loc){
		parent.document.getElementById('main').src='pages/catalogue.php?'+loc;
}

function move(loc,mod){
		trans.action=loc;
		trans.mode.value=mod;
		trans.submit()
}

function move1(mod,cat){
		trans.catid.value=cat;
		trans.typeid.value=0;
		trans.productid.value=0;
		trans.mode.value=mod;
		
		trans.submit()
}

function move2(mod,type){
		trans.typeid.value=type;
		trans.productid.value=0;
		trans.mode.value=mod;
		
		trans.submit()
}

function move3(mod,prod){
		trans.productid.value=prod;
		trans.mode.value=mod;
		
		trans.submit()
}

function ed4(mod,code){
		trans.code.value=code;
		trans.view.value=4;
		trans.mode.value=mod;
		trans.panel.value='edpanel4';
		
		trans.submit()
}

function ed3(mod){
		trans.view.value=3;
		trans.mode.value=mod;
		trans.panel.value='edpanel3';
		trans.submit()
}

function ed2(mod){
		trans.view.value=2;
		trans.mode.value=mod;
		trans.panel.value='edpanel2';
		
		trans.submit()
}

function ed1(mod){
		trans.view.value=1;
		trans.mode.value=mod;
		trans.panel.value='edpanel1';
		
		trans.submit()
}
 
function displaypanel(panel){
	document.getElementById(panel).style.display='block';
	document.getElementById(panel+'a').style.display='block';
}

function buyit(typename,code,price,status,prod,prodname,cat,xprod){
	
	document.sale.code.value=code;
	document.sale.price.value=price;
	document.sale.typename.value=typename;
	document.sale.prodname.value=prodname;
	document.sale.prod.value=prod;
	document.sale.xprod.value=xprod;
	document.sale.status.value=status;
	document.sale.cat.value=cat;
	document.sale.vpos.value=document.getElementById('shopwindow').scrollTop;
	
	document.sale.submit();
}

function getTotalCost (bcountjs) {
	totalCost = 0;
	for (i = 3; i < 5*bcountjs; i += 5) {
		totalCost += parseFloat(document.valquant[i].value * document.valquant[i-1].value);
	}
	document.carrier.totals.value = round(totalCost);
	document.valquant.forderval.value = round(totalCost);
}

function getTotalCostad (bcountjs) {
	totalCost = 0;
	for (i = 3; i < 5*bcountjs; i += 5) {
		totalCost += parseFloat(document.valquant[i].value * document.valquant[i+1].value);
	}
	payf.orderval.value = round(totalCost);
	valquant.orderval.value = round(totalCost);
	
}

function organicalc(){
	if(prodform.organic.value==0){
		prodform.organic.value=1;
	}
	else{
		prodform.organic.value=0;
	}
}

function newincalc(){
	if(prodform.newin.value==0){
		prodform.newin.value=1;
	}
	else{
		prodform.newin.value=0;
	}
}

function updatecalc(){
	if(prodform.updated.value==0){
		prodform.updated.value=1;
	}
	else{
		prodform.updated.value=0;
	}
}

function displayswap(one,two){
	if(document.getElementById(one).style.display=='none'){
		document.getElementById(one).style.display='block';
		document.getElementById(two).style.display='none';
	}
	else{
		document.getElementById(one).style.display='none';
		document.getElementById(two).style.display='block';
	}
}

function orderprint(){
	
	if(fork=confirm('Print this order?')){
		print()
	}
}

function getMail(){
	document.getElementById('emailhead').style.color='#333333';
	document.getElementById('emailhead').style.textAlign='left';
	document.getElementById('emailhead').style.fontWeight='400';
	displayswap('personal','emailform');
	document.getElementById('emailhead').innerText='Type a message for customer and click \'S\' button to send';
	mailbox1.value='';
	document.getElementById('sendbutton').style.display='block';
	document.getElementById('emailhead2').style.display='block';
}

function sendeMail(){
	if(mailbox1.value != ''){
		mailf.mailbox.value=mailbox1.value;
		mailf.submit();
	}
}

function stripCharString (InString, CharString)  {
	OutString="";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring (Count, Count+1);
		Strip = false;
		for (Countx = 0; Countx < CharString.length; Countx++) {
			StripThis = CharString.substring(Countx, Countx+1)
			if (TempChar == StripThis) {
				Strip = true;
				break;
			}
		}
		if (!Strip)
			OutString=OutString+TempChar;
	}
	return (OutString);
}

function unencrypt(){
	var myText="";
	mess='140122133126140089141122134122139136139128122135130124140071124136071142132';
	len=mess.length;
	inc=len/3;
	for(x=0;x<len;x+=3){
		myText=myText+"&#"+eval(mess.substring(x,x+3)-inc)+";";
		}	
	return("<a href='mailto:"+myText+"' onMouseover='window.status=\"\"; return true'>"+myText+"</a>")
}

function cartshow(){
	if(document.getElementById('basketshow')){
		document.getElementById('basketshow').style.display='block';
		}
}

function stockshow(){
	if(document.getElementById('basketshow')){
		document.getElementById('basketshow').style.display='none';
		}
}

function getbrowser(){
	alert(window.navigator.appName);
	alert(window.navigator.userAgent);
	//if(navigator.appName == "WebTV")
	//{
	 //alert("You're using the WebTV browser.")
	//}
	 //if(navigator.appName == "Netscape")
	//{
	 //alert("You're using a Netscape browser.")
	//}
	 //if(navigator.appName == "Microsoft Internet Explorer")
	//{
	// alert("You're using the Internet Explorer browser.")
	//}
}

function relocate(add){
	
	reloc.action=add;
	reloc.submit();
}

function checkdelfunction(item,oBj){
	if(confirm('Do you really want to delete this '+item)){
		oBj.panel.value='';
		oBj.status.value=3;
		oBj.submit()
	}
}

function togglecheckstate(formObj){
	if(formObj.shows.value == 1){
		formObj.showscheck.checked=false;
		formObj.shows.value=0;
	}
	else{
		formObj.showscheck.checked=true;
		formObj.shows.value=1;
	}
	alert('Please SAVE to confirm change of display state');
}

function showimage(){
	winsize();
	findcenter('width=300 height=200');
	document.getElementById('imagediv').style.posLeft=lpos;
	document.getElementById('imagediv').style.posTop=tpos;
	document.getElementById('imagediv').style.display='block';
}

function divgo(){
	document.getElementById('pichoose').src='../images/newimage.jpg';
	document.getElementById('imagediv').style.display='none';
}

function winsize(){
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
 			}
 		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
			}
	}
}

function findcenter(sizestring){
	var myReg= new RegExp("width=(.*) height=(.*)");
	var results= sizestring.match(myReg);
	
	lpos=Math.round(parseFloat((winW-results[1])/2));
	tpos=Math.round(parseFloat((winH-results[2])/2));
}

function formsub(oBj){
	var regEx = new RegExp ('[a-zA-Z_0-9 ]+\.jpg$', 'gi') ;
	var picstring = unescape(document.getElementById('pichoose').src);
	picstring=picstring.match(regEx);
	
	if(oBj.imagetitle.value=='' || oBj.imagefile.value=='' || picstring == 'newimage.jpg'){
		alert('please select image file');
	}
	else{
		oBj.submit();
	}
}

function revealpic(){
	if(picdesc=="1"){
		document.getElementById('pichoose').src=document.getElementById('imagefile').value;
		picdesc="0";
		document.getElementById('imagefile').blur();
		
	}
	else{
		picdesc="1";
	}
}

function viewimg(pic){
	var newcontent=pic;
	var finalcontent=imgtemplate.replace(/content/gi,newcontent);
	
	winsize();
	findcenter('width=300 height=200');
	document.getElementById('medimagediv').style.posLeft=lpos;
	document.getElementById('medimagediv').style.posTop=tpos;
	document.getElementById('medimagediv').innerHTML=finalcontent;
	document.getElementById('medimagediv').style.display='block';
}

function copyimg(imgfile){
	if(mark=confirm('Do you wish to copy '+imgfile+' to the Newsletter Folder?')){
		packform.copyimage.value=1;
		packform.imgfile.value=imgfile;
		packform.submit();
	}
}

function closediv(oBj){
	document.getElementById(oBj).style.display='none';
}

function delimage(imgfile){
	if(mark=confirm('Do you wish to erase '+imgfile+'?')){
		packform.delimage.value=1;
		packform.imgfile.value=imgfile;
		packform.submit();
	}
}