// JavaScript Document
//variables section:
var popularQuote = POPULARQ;
var increase_value = INCREASE_V;
var pic_path = 'http://www.printingcenterusa.com/Images/brochures/folds/';
var general_pic_path = 'http://www.printingcenterusa.com/Images/general/';
var baseSite = 'http://www.printingcenterusa.com/';
var arrTabs = new Array('tab1', 'tab2');

function onLoad_brochures_quote()	//use for all quote pages
{
	onLoad_all_quote_pages();
}

//function validateit(the_type)	//called from validate_form.js 
function validateit()	//called from validate_form.js 
{
	return isReady('zipc', 'username', 'emailadd', 'phnum');	//field names you want to validate
}

//array format gotta be like below in order for get_position() to work.
fold_choices = new Array(
new Array(	
new Array("No Fold", 0),
new Array("Half Fold", 1),
new Array("Tri Fold", 3),
new Array("ZFold", 5)
),
new Array(	
new Array("No Fold", 0),
new Array("Half Fold", 1),
new Array("Tri Fold", 3),
new Array("ZFold", 5),
new Array("Parallel", 6),
new Array("Gate Fold", 7)
),
new Array(
new Array("No Fold", 0),
new Array("Half Fold", 1),
new Array("Right Angle Half Fold", 2),
new Array("Tri Fold", 3),
new Array("Right Angle Tri Fold", 4),
new Array("ZFold", 5),
new Array("Custom", 8)
),
new Array(
new Array("No Fold", 0),
new Array("Half Fold", 1),
new Array("Right Angle Half Fold", 2),
new Array("Tri Fold", 3),
new Array("Right Angle Tri Fold", 4),
new Array("ZFold", 5),
new Array("Parallel", 6),
new Array("Gate Fold", 7),
new Array("Custom", 8)
),
new Array(	
new Array("Tri Fold", 3),
new Array("ZFold", 5),
new Array("Parallel", 6),
new Array("Gate Fold", 7),
new Array("Custom", 8)
)
);

color_choices = new Array(
new Array(	
new Array("4 / 4", 0),
new Array("4 / 1", 1),
new Array("4 / 0", 2),
new Array("1 / 1", 3),
new Array("1 / 0", 4)
),
new Array(	
new Array("4 / 4", 0),
new Array("4 / 1", 1),
new Array("4 / 0", 2),
new Array("1 / 1", 3),
new Array("1 / 0", 4)
),
new Array(	
new Array("4 / 4", 0),
new Array("4 / 1", 1),
new Array("4 / 0", 2),
new Array("1 / 1", 3),
new Array("1 / 0", 4)
),
new Array(	
new Array("4 / 4", 0),
new Array("4 / 1", 1),
new Array("1 / 1", 3)
)
);

varnish_choices = new Array(
new Array(	
new Array("Yes", 1)
),
new Array(	
new Array("No", 0),
new Array("Yes", 1)
)
);

zip_choices = new Array(
new Array(	
new Array("Ground", 1),
new Array("3 Day Select", 2),
new Array("2 Day Air", 3),
new Array("Next Day Air", 4)
),
new Array(
new Array("2 Day Air", 3),
new Array("Next Day Air", 4)
)
);

//alert(size_choices[0][2][1])	//output = 2
//alert(size_choices[0][2][0])	//output = "11 x 17"
//alert(size_choices[0][2])	//output = "11 x 17", 2
//alert(size_choices[0]) //output = "8.5 x 11", 0, "8.5 x 14", 1, "11 x 17", 2, "11 x 24.5", 3
//testing

function showpic()	//for custom quote, will show a particular pic if custom is chosen, or generate a file name
{
	pre_pic_path = "http://www.printingcenterusa.com/";
	/*if (item_translator('foldgroup') == "custom")
	{
		document['print_job_pic'].src = pre_pic_path + "Images/general/custom-quote-pic.gif";
	}
	else
	{
		var pic_file_name = item_translator('size')+"_"+item_translator('foldgroup')+"_"+item_translator('color')+".jpg";
		document['print_job_pic'].src = pic_path + pic_file_name;	//for everybody
	}*/
		var color_file_name_outside = "color_boxes_"+item_translator('color')+".gif";
		document['color_boxes'].src = general_pic_path + color_file_name_outside;	//for color boxes
}

function item_translator(the_item)	//translate the item name to a right part of the file name, use for generate part of a pic file name
{
	//method 1 - eval() is a slower method
	//string_item = "document.Forminfo."+the_item+".options[document.Forminfo."+the_item+".selectedIndex].text";
	//my_item = eval(string_item);
	
	// method 2 - same result as the above method, but faster and safer
	var string_item = document.Forminfo[the_item].options[document.Forminfo[the_item].selectedIndex].text;
	my_item = string_item.toString();
	my_item = my_item.replace(/^\s+/,'').replace(/\s+$/,'');
	
	//alert(my_item);
	switch (my_item)
	{
		case '8.5 x 11':
			return "11_85";
			break;
		case '8.5 x 14':
			return "14_85";
			break;
		case '11 x 17':
			return "17_11";
			break;
		case '11 x 24.5':
			return "245_11";
			break;
		case '4 / 4':		//need to change the pic naming
			//return "color";
			return "color_color";
			break;
		case '4 / 1':	//need to change the pic naming
			//return "black";
			return "color_black";
			break;
		case '4 / 0':	//need to change the pic naming
			//return "blank";
			return "color_blank";
			break;
		case '1 / 1':	//need to change the pic naming
			//return "black";
			return "black_black";
			break;
		case '1 / 0':	//need to change the pic naming
			//return "blank";
			return "black_blank";
			break;
		case 'No Fold':
			return "no";
			break;
		case 'Half Fold':
			return "half";
			break;
		case 'Right Angle Half Fold':
			return "rahalf";
			break;
		case 'Tri Fold':
			return "tri";
			break;
		case 'Right Angle Tri Fold':
			return "ratri";
			break;
		case 'ZFold':
			return "z";
			break;
		case 'Parallel':
			return "parallel";
			break;
		case 'Gate Fold':
			return "gate";
			break;
		case 'Custom':
			return "custom";
			break;
	}
}

function display_summary()	//this function only load at the beginning to display the summary
{
		document.getElementById("summary_size").innerHTML = get_choice("size", "choice");
		document.getElementById("summary_paper").innerHTML = get_choice("paper", "choice");
		document.getElementById("summary_color").innerHTML = get_choice("color", "choice");
		document.getElementById("summary_folding").innerHTML = get_choice("foldgroup", "choice");
		document.getElementById("summary_proof").innerHTML = get_choice("proof", "choice");
		document.getElementById("summary_varnish").innerHTML = get_choice("varnish", "choice");
		document.getElementById("summary_production_speed").innerHTML = get_choice("prospeed", "choice");
		document.getElementById("summary_quantity").innerHTML = get_choice("quant", "value");
		document.getElementById("summary_zip").innerHTML = "(requires zip code)";
		
		document.getElementById("summary_shipping_method").innerHTML = "(requires zip code)";
		document.getElementById("summary_shipping_cost").innerHTML = "(requires zip code)";
		
		document.getElementById("real_total_price").innerHTML = "(requires zip code)";
		document.getElementById("real_shipping_cost").innerHTML = "(requires zip code)";
		
		document.getElementById("summary_promo_saving").innerHTML = "$0.00";

		document.getElementById("summary_total_price").innerHTML = "(requires zip code)";
}

function reset_hidden_field()
{
		// ------- reset hidden field ------ //
		this_product = 'brochures';
		document.Forminfo.browser_type.value = detbrowser;
		document.Forminfo.os_type.value = decOS;

		document.Forminfo.quote_number.value = '';

		document.Forminfo.hquant.value = '';
		document.Forminfo.hzipc.value = '';
		document.Forminfo.hshipcost.value = '';
		document.Forminfo.hproofcost.value = '';
		document.Forminfo.hrushcost.value = '';
		document.Forminfo.hfoldcost.value = '';		
		document.Forminfo.hprintcost.value = '';
		document.Forminfo.htotalcost.value = '';

		if(parent.document.location)
		{
			document.Forminfo.page_url.value = parent.document.location;
		}
		document.Forminfo.submit_type.value = "";
		document.Forminfo.quote_saved.value = "not_saved";
		//document.Forminfo.ftype.value = "4 Color Brochure/Flyer/Newsletter";
		
		var currentURL = document.location.href.split('?')[0];

		if (currentURL == baseSite+'brochure-printing.html' || currentURL == baseSite+'affordable-brochure-printing.html' || currentURL == baseSite+'brochure-printing-options.html' || currentURL == baseSite+'business-brochures.html' || currentURL == baseSite+'full-color-brochure-printing.html' || currentURL == baseSite+'marketing-brochures.html')
		{
			document.Forminfo.ftype.value = "Brochure";	
		}
		else if (currentURL == baseSite+'flyer-printing.html' || currentURL == baseSite+'business-promotional-flyers.html' || currentURL == baseSite+'flyer-printing-quotes.html' || currentURL == baseSite+'premium-flyers.html' || currentURL == baseSite+'economical-flyers.html' || currentURL == baseSite+'fast-flyer-printing.html')
		{
			document.Forminfo.ftype.value = "Flyer";
		}
		else
		{
			document.Forminfo.ftype.value = "Newsletter";	
		}

		document.Forminfo.sizepos.value = get_choice("size", "value");
		document.Forminfo.psizec.value = get_choice("size", "choice");		

		document.Forminfo.finkc.value = get_choice("color", "choice");
		document.Forminfo.binkc.value = get_choice("color", "choice");
		
		document.Forminfo.binkpos.value = get_choice("color", "value");	//if only one ink position needs, use binkpos

		document.Forminfo.shipc.value = get_choice("shipgroup", "choice");		
		document.Forminfo.sm.value = get_choice("shipgroup", "value");
		
		document.Forminfo.ptypec.value = get_choice("paper", "choice");
		document.Forminfo.paperpos.value = get_choice("paper", "value");

		document.Forminfo.foldpos.value = get_choice("foldgroup", "value");
		document.Forminfo.foldc.value = get_choice("foldgroup", "choice");

		document.Forminfo.proofpos.value = get_choice("proof", "value");
		document.Forminfo.proofc.value = get_choice("proof", "choice");

		document.Forminfo.rushrate.value = RR[get_choice("prospeed", "value")];
		document.Forminfo.rushc.value = get_choice("prospeed", "choice");
		document.Forminfo.rushpos.value = get_choice("prospeed", "index");

		document.Forminfo.promod.value = "";
		document.Forminfo.promoa.value = "0.00";
		document.Forminfo.promom.value = "0";
		document.Forminfo.promof.value = "";
		document.Forminfo.promot.value = "";
		document.Forminfo.promoda.value = "0";
		document.Forminfo.procode.value = "";

		document.Forminfo.varnpos.value = get_choice("varnish", "value");		
		document.Forminfo.varnc.value = get_choice("varnish", "choice");		
		
		document.Forminfo.tagvar.value = "0";
}

function onChange(choiceType) 
{
	switch (choiceType) 
	{
		case 'psize':
			auto_pull_down_color();
			auto_pulldown_fold_menu();
			size_action();
			color_action();
			fold_action();
			break;
		case 'ppaper':
			auto_pull_down_varnish();
			paper_action();	
			varnish_action();
			break;
		case 'pcolor':
			auto_pulldown_fold_menu();
			color_action();
			fold_action();
			break;
		case 'pfold':
			//auto_pulldown_size_menu();
			fold_action();
			//size_action();
			break;
		case 'pproof':
			proof_action();
			break;
		case 'pvarnish':
			varnish_action();
			break;
		case 'prospeed':
			turnaround_action();
			break;
		case 'quant':
			quantity_action();
			break;
		case 'zipc':
			zip_action();
			auto_pull_down_shipping();
			shipping_action();	//redo shipping action
			break;
		case 'shipgroup':
			shipping_action();
			break;
	}
	calcprintprice();
	showTab('tab2');
}
/*
folding is depends on size and color options: (4/0 cannot have parallel and gate fold)
case1: (8.5x11) + 4/0 					-> 4 folding options
case2: (8.5x11) + non-4/0 			-> 4 folding options
case3: (8.5x14) + 4/0					-> 4 folding options
case4: (8.5x14) + non-4/0			-> 6 folding options
case5: (other sizes) + 4/0			->	7 folding options
case6: (other sizes) + non-4/0	-> 9 folding options
*/
function auto_pulldown_fold_menu()	//generate automatic pull downmenu depend on size and color
{
	
	var Current_size_choice = get_choice("size", "index");	//current size choice
	var Current_fold_choice = get_choice("foldgroup", "index");	//current fold choice
	var Current_color_choice = get_choice("color", "index");	//current fold choice
	
	var Current_choice = 0;	//temp choice
	if (Current_size_choice == 0)	//(8.5x11)
	{
		Current_choice = 0;	//fold_choices[0] means without custom fold
	}
	else if(Current_size_choice == 3)	//11x24.5
	{
		Current_choice = 4;	//restricted to couple of folds
	}
	else if(Current_size_choice == 1 && Current_color_choice == 2)	//(8.5x14) + 4/0
	{
		Current_choice = 0;
	}
	else if(Current_size_choice == 1 && Current_color_choice != 2)	//(8.5x14) + non-4/0
	{
		Current_choice = 1;
	}
	else if(Current_size_choice != 1 && Current_color_choice == 2)	//(other sizes) + 4/0
	{
		Current_choice = 2;
	}
	else
	{
		Current_choice = 3;	//fold_choices[2] means with custom fold
	}
	var New_fold_choice = get_position(fold_choices[Current_choice], get_choice("foldgroup", "choice"));
	
	//auto_pulldown(the pulldown menu name, the array group, the old choice position);
	auto_pulldown(document.Forminfo.foldgroup, fold_choices[Current_choice],New_fold_choice);
}

function size_action()
{
	var choiceValue = get_choice("size", "value");
	var choiceName = get_choice("size", "choice");
	document.getElementById("summary_size").innerHTML = choiceName;
	
	chgpapsize(parseInt(choiceValue), choiceName);
}

function paper_action()
{
	var choiceValue = get_choice("paper", "value");
	var choiceName = get_choice("paper", "choice");
	document.getElementById("summary_paper").innerHTML = choiceName;

	chgpaptype(parseInt(choiceValue), choiceName);
}

function auto_pull_down_varnish()
{
	var Current_paper_choice = get_choice("paper", "index");	//current paper choice
	var Current_paper_name = get_choice("paper", "choice");	//current paper name
	var Current_varnish_choice = get_choice("varnish", "index");	//current varnish choice
	
	var isMatteStock = word_search(Current_paper_name, 'matte');
	
	var choiceIndex = 0;	//temp choice
	if (isMatteStock)	//matte stock
	{
		choiceIndex = 0;	//must varnish
	}
	else
	{
		choiceIndex = 1;
	}
	
	var New_varnish_choice = get_position(varnish_choices[choiceIndex], get_choice("varnish", "choice"));
	auto_pulldown(document.Forminfo.varnish, varnish_choices[choiceIndex], New_varnish_choice);
}

function color_action()
{
	var choiceValue = get_choice("color", "value");
	var choiceName = get_choice("color", "choice");
	document.getElementById("summary_color").innerHTML = choiceName;

	chgink(parseInt(choiceValue), choiceName);
}

function auto_pull_down_color()
{
	var choiceIndex = get_choice("size", "index");
	var New_color_choice = get_position(color_choices[choiceIndex], get_choice("color", "choice"));
	
	// to generate a respective inside color menu choice
	auto_pulldown(document.Forminfo.color, color_choices[choiceIndex], New_color_choice);
}


function fold_action()
{
	var choiceValue = get_choice("foldgroup", "value");
	var choiceName = get_choice("foldgroup", "choice");
	document.getElementById("summary_folding").innerHTML = choiceName;

	chgfold(parseInt(choiceValue), choiceName);
}

function proof_action()
{
	var choiceValue = get_choice("proof", "value");
	var choiceName = get_choice("proof", "choice");
	document.getElementById("summary_proof").innerHTML = choiceName;

	chgproof(parseInt(choiceValue), choiceName);
}

function varnish_action()
{
	var choiceValue = get_choice("varnish", "value");
	var choiceName = get_choice("varnish", "choice");
	document.getElementById("summary_varnish").innerHTML = choiceName;

	chgvarnpos(parseInt(choiceValue), choiceName);
}

function turnaround_action()
{
	var choiceValue = get_choice("prospeed", "value");
	var choiceName = get_choice("prospeed", "choice");
	var choiceIndex = get_choice("prospeed", "index");
	document.getElementById("summary_production_speed").innerHTML = choiceName;

	chgrushrate(parseInt(choiceValue), choiceName, choiceIndex);
}

function quantity_action()
{
	//calcprintprice();
	var choiceValue = get_choice("quant", "value");
	document.getElementById("summary_quantity").innerHTML = choiceValue;
}

function auto_pull_down_shipping()
{
	var choiceValue = get_choice("zipc", "value");
	var short_zip = choiceValue.substring(0,3);
	var the_choice = 0;
	
	if (short_zip == "967" || short_zip == "968" || short_zip == "995" || short_zip == "996" || short_zip == "997" || short_zip == "998" || short_zip == "999")
	{
		the_choice = "1";
	}
	var New_shipping_choice = get_position(zip_choices[the_choice], get_choice("shipgroup", "choice"));
	
	auto_pulldown(document.Forminfo.shipgroup, zip_choices[the_choice], New_shipping_choice);
}
function zip_action()
{
	//calcprintprice();
	var choiceValue = get_choice("zipc", "value");
	document.getElementById("summary_zip").innerHTML = choiceValue;
	getCityState();	//located at quote_common.js
}

function shipping_action()
{
	var choiceValue = get_choice("shipgroup", "value");
	var choiceName = get_choice("shipgroup", "choice");
	document.getElementById("summary_shipping_method").innerHTML = choiceName;

	fixsm(parseInt(choiceValue), choiceName);
}