// Validate form
<!--
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
//-->

// OPEN Browser window

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
//-->


// JavaScript Document

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// Javascript Menu System


// <![CDATA[
	var myMenu;
	window.onload = function() {
		myMenu = new SDMenu("my_menu");
		myMenu.init();
	};
	// ]]>


function SDMenu(id) {
	if (!document.getElementById || !document.getElementsByTagName)
		return false;
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("div");
	this.remember = true;
	this.speed = 3;
	this.markCurrent = true;
	this.oneSmOnly = false;
}
SDMenu.prototype.init = function() {
	var mainInstance = this;
	for (var i = 0; i < this.submenus.length; i++)
		this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
			mainInstance.toggleMenu(this.parentNode);
		};
	if (this.markCurrent) {
		var links = this.menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
			if (links[i].href == document.location.href) {
				links[i].className = "current";
				break;
			}
	}
	if (this.remember) {
		var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
		var match = regex.exec(document.cookie);
		if (match) {
			var states = match[1].split("");
			for (var i = 0; i < states.length; i++)
				this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
		}
	}
};
SDMenu.prototype.toggleMenu = function(submenu) {
	if (submenu.className == "collapsed")
		this.expandMenu(submenu);
	else
		this.collapseMenu(submenu);
};
SDMenu.prototype.expandMenu = function(submenu) {
	var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var links = submenu.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		fullHeight += links[i].offsetHeight;
	var moveBy = Math.round(this.speed * links.length);
	
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < fullHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
			mainInstance.memorize();
		}
	}, 30);
	this.collapseOthers(submenu);
};
SDMenu.prototype.collapseMenu = function(submenu) {
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "collapsed";
			mainInstance.memorize();
		}
	}, 30);
};
SDMenu.prototype.collapseOthers = function(submenu) {
	if (this.oneSmOnly) {
		for (var i = 0; i < this.submenus.length; i++)
			if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};
SDMenu.prototype.expandAll = function() {
	var oldOneSmOnly = this.oneSmOnly;
	this.oneSmOnly = false;
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className == "collapsed")
			this.expandMenu(this.submenus[i]);
	this.oneSmOnly = oldOneSmOnly;
};
SDMenu.prototype.collapseAll = function() {
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className != "collapsed")
			this.collapseMenu(this.submenus[i]);
};
SDMenu.prototype.memorize = function() {
	if (this.remember) {
		var states = new Array();
		for (var i = 0; i < this.submenus.length; i++)
			states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
		var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
	}
};

// Javascript Business Card Calcs

function getArtworkCost() {

//0 Reprint/Inhouse
//25 Supplied
//45 Simple
//75 Medium
//100 Complex
}


// we assign a number and add to get the right option
//colour -colour_front	1
//		colour_both		4

//cello - none			0
//		cello_front		1
//		cello_both		2
//		add the numbers to get the option, remember to minus 1 to get the right array index


//access values in the array with document.write(pricing_array[0] [0]);

function getCost() {
	if(document.bc.quantity.options[0].value == true) {
	return false
	}
	else {
	var qty=document.bc.quantity.options[document.bc.quantity.selectedIndex].value;
	//var pricing_array = new Array([40],[55],[75],[99]);
	var pricing_array = new Array([40,55,75,99,180], [50,60,80,130,230], [55,70,90,145,245], [50,65,85,110,199], [60,75,95,135,240], [65,80,100,155,270])
	var colour = document.bc.colour.value;
	var cello = document.bc.cello.value;
	var quantity = document.bc.quantity.value;
	var option_select = parseInt(colour) + parseInt(cello) - 1;
	new_price = pricing_array[option_select] [document.bc.quantity.value];
	document.bc.cost.value = new_price;
	
	var artwork_price = document.bc.artwork.value;
	document.bc.artwork_cost.value = artwork_price;
	}
	return true
}

function getNCRCost() {
	if(document.ncr_form.quantity.options[0].value == true) {
	return false
	}
	else {
	var qty=document.ncr_form.quantity.options[document.ncr_form.quantity.selectedIndex].value;
	//var pricing_array = new Array([40],[55],[75],[99]);
	var pricing_array = new Array(	[70,75,75,85], [100,110,105,115], [140,175,130,230], [215,255,225,295], [400,560,500,650], [580,830,800,1100], 
									[80,85,85,90], [135,195,135,215], [200,230,185,245], [295,310,315,385], [650,710,590,790], [820,1150,970,1300],
									[105,120,120,150], [140,250,160,290], [235,330,205,365], [405,430,380,500], [800,895,845,1120], [1090,1300,1200,1500])
	var size = document.ncr_form.size.value;  //was colour//
	var sets = document.ncr_form.sets.value;   //was cello//
	var quantity = document.ncr_form.quantity.value;
	var artwork = document.ncr_form.artwork.value;
	
	var page_size = ncr_form.elements["size"].value;
	if(page_size == "0"){
			var ncrSheetSize = "DL";
		} else if (page_size == "6"){
			var ncrSheetSize = "A5";
		} else if (page_size == "12"){
			var ncrSheetSize = "A4";
		} else {
			var ncrSheetSize = "undefined";
	}
	
	var Qty = ncr_form.elements["quantity"].value;
	if(Qty == "0"){
			var ncrQty = "2";
		} else if (Qty == "1"){
			var ncrQty = "5";
		} else if (Qty == "2"){
			var ncrQty = "10";
		} else if (Qty == "3"){
			var ncrQty = "20";
		} else if (Qty == "4"){
			var ncrQty = "50";
		} else if (Qty == "5"){
			var ncrQty = "100";
		} else {
			var ncrQty = "undefined";
	}
	
	var sets = ncr_form.elements["sets"].value;
	if(sets == "0"){
			var ncrSets = "50 sets per book, duplicate";
			var ncrSetsNum = 50;
		} else if (sets == "1"){
			var ncrSets = "100 sets per book, duplicate";
			var ncrSetsNum = 100;
		} else if (sets == "2"){
			var ncrSets = "50 sets per book, triplicate";
			var ncrSetsNum = 50;
		} else if (sets == "3"){
			var ncrSets = "50 sets per book, quadruplicate";
			var ncrSetsNum = 50;
		} else {
			var ncrSets = "undefined";
	}
	
	var cover = ncr_form.elements["cover"].value;
	if(cover == "0"){
			var ncrCover = "softcovers";
			var ncrCoverCost = parseFloat(ncrQty)*0;
		} else if (cover == "red"){
			var ncrCover = "Red Crocodile Covers";
			var ncrCoverCost = parseFloat(ncrQty)*3.50;
		} else if (cover == "blue"){
			var ncrCover = "Blue Crocodile Covers";
			var ncrCoverCost = parseFloat(ncrQty)*3.50;
		} else if (cover == "green"){
			var ncrCover = "Green Crocodile Covers";
			var ncrCoverCost = parseFloat(ncrQty)*3.50;
			} else if (cover == "grey"){
			var ncrCover = "Grey Crocodile Covers";
			var ncrCoverCost = parseFloat(ncrQty)*3.50;
		} else {
			var ncrCover = "undefined";
	}
	
	var lastsheet = ncr_form.elements["last_sheet"].value;
	if(lastsheet == "0"){
			var ncrLastSheet = "do not perf the last sheet";
		} else if (lastsheet == "1"){
			var ncrLastSheet = "perf the last sheet";
		} else {
			var ncrSheetSize = "undefined";
	}
	
	var topSheetColourPrint = ncr_form.elements["top_sheet"].value;
	if(topSheetColourPrint == "black"){
			var ncrColourPrintCost = 0;
		} else if (topSheetColourPrint == "colour"){
			if(ncrSetsNum =="50"){
			var ncrColourPrintCost = 45 + parseFloat(ncrQty)*10;
			} else if(ncrSetsNum =="100"){
			var ncrColourPrintCost = 45 + parseFloat(ncrQty)*17.5;
			} else {
			var ncrColourPrintCost = "undefined";
		}
		} else {
			var ncrSheetSize = "undefined";
	}
	
	var backSheetPrint = ncr_form.elements["top_sheet_back"].value;
	if(backSheetPrint == "none"){
			var ncrBackPrintCost = 0;
		} else if (backSheetPrint == "black"){
			if(ncrSetsNum =="50"){
			var ncrBackPrintCost = 45 + parseFloat(ncrQty)*2;
			} else if(ncrSetsNum =="100"){
			var ncrBackPrintCost = 45 + parseFloat(ncrQty)*3;
			} else {
			var ncrBackPrintCost = "undefined";
		}
		
		} else if (backSheetPrint == "colour"){
			if(ncrSetsNum =="50"){
			var ncrBackPrintCost = 45 + parseFloat(ncrQty)*7.5;
			} else if(ncrSetsNum =="100"){
			var ncrBackPrintCost = 45 + parseFloat(ncrQty)*12;
			} else {
			var ncrBackPrintCost = "undefined";
		}
		
		} else {
			var backSheetPrint = "undefined";
	}
	
	var ncrBindingTape = ncr_form.elements["binding"].value;
	
	var ncrTopSheetColour = ncr_form.elements["top_sheet_colour"].value;
	var ncrSecondSheetColour = ncr_form.elements["second_sheet_colour"].value;
	var ncrThirdSheetColour = ncr_form.elements["third_sheet_colour"].value;
	var ncrFourthSheetColour = ncr_form.elements["fourth_sheet_colour"].value;
	
	var Perforations = ncr_form.elements["perf"].value;
	var ncrPerforations = 0 + (Perforations - 1) * 25;
	
	var Numbering = ncr_form.elements["numb"].value;
	var ncrNumbering = 0 + (Numbering - 1) * 25;
	
	var HoleDrill = ncr_form.elements["drilling"].value;
	var ncrHoleDrill = HoleDrill* 1.5 *parseFloat(ncrQty);
	
	var lb = '<br />';
	
	var ncrBackPages = ncr_form.elements["top_sheet_back"].value;
	var ncrArtwork = ncr_form.elements["artwork"].value;
	
	var option_select = parseInt(size) + parseInt(quantity);
	new_price = pricing_array[option_select] [document.ncr_form.sets.value];
	new_price =  new_price + ncrPerforations + ncrNumbering + ncrCoverCost + ncrHoleDrill + ncrColourPrintCost + ncrBackPrintCost;
	document.ncr_form.cost.value = new_price;
	
	var artwork_price = document.ncr_form.artwork.value;
	document.ncr_form.artwork_cost.value = artwork_price;

	document.getElementById('ncrOptions').innerHTML = 
	'<strong>  NCR sheet size is: </strong>' + ncrSheetSize + lb + 
	'<strong>  The quantity is: </strong>' + ncrQty + lb + 
	'<strong>  The sets are:  </strong>' + lb + ncrSets + lb +
	'<strong>  The binding colour will be:  </strong>' + lb + ncrBindingTape + lb +
	'<strong>  The covers will be:  </strong>' + lb + ncrCover + lb +
	'<strong>  The covers will cost: $</strong>'+ ncrCoverCost + lb +
	'<strong>  When perforating the sheets: </strong>' + lb + ncrLastSheet + lb +
	'<strong>  The perforation cost is: $</strong>' + ncrPerforations + lb +

	'<strong>  The sets are:  </strong>' + ncrSets + lb +
	'<strong>  The top sheet colour is: </strong>' + ncrTopSheetColour + lb +
	'<strong>  The 2nd sheet colour is: </strong>' + ncrSecondSheetColour + lb +
	'<strong>  The 3rd sheet colour is: </strong>' + ncrThirdSheetColour + lb +
	'<strong>  The 4th sheet colour is: </strong>' + ncrFourthSheetColour + lb +
	'<strong>  </strong>' + lb +
	'<strong>  Optional Extras</strong>' + lb +
	'<strong>  The extra numbering cost is $</strong>' + ncrNumbering + lb +
	'<strong>  The extra perforating cost is $</strong>' + ncrNumbering + lb +
	'<strong>  The hole drilling cost is $</strong>' + ncrHoleDrill + lb +
	'<strong>  The numbering cost is $</strong>' + ncrNumbering + lb +
	'<strong>  The front sheet print cost is $</strong>' + ncrColourPrintCost + lb +
	'<strong>  The back of sheet print cost is $</strong>' + ncrBackPrintCost
	;
	return true
	}
	
}

function getOutSourceCost() {
	
	var cost_price = document.outsource.cost_price.value;
	var setup = document.outsource.setup_fee.value;
	var freight = document.outsource.freight.value;
	var margin = document.outsource.margin.value;
	var total_cost = parseFloat(cost_price)+parseFloat(setup)+parseFloat(freight);
	var sell_price = 110*total_cost/(100 - parseFloat(margin));
	var sell_price_exgst = 100*total_cost/(100 - parseFloat(margin));
	document.outsource.sell_exgst.value = Math.round(sell_price_exgst*100)/100;
	var sell_price = Math.round(sell_price*100)/100
	document.outsource.sell.value = sell_price;
	var profit = Math.round((sell_price/1.1-total_cost)*100)/100
	var artwork_price = document.outsource.artwork.value;
	document.outsource.profit.value = profit;
	document.outsource.artwork_cost.value = artwork_price;
	
	return true
}

