//  Solar Shading / Woven Woods specific javascript..

function leftovers(panel1,panel2,panel3,panel4,panel5,width,seam_width) {
	v = checkValue(width) - (checkValue(panel1) + checkValue(panel2) + checkValue(panel3) + checkValue(panel4) + checkValue(panel5));
	if (v < 0){
		if (seam_width.nodeName == 'INPUT'){
			seam_width.value = Math.floor(checkValue(seam_width.value)+v);
		}else{
			seam_width.value = checkValue(seam_width.value)+v;
		}
		updateLeght(seam_width);
		return;
	}
	v_int = Math.floor(v);	
	v_f = (v-v_int);	
	$('panel_leftover').value = v_int;	
	$('panel_leftover_f').value = v_f;	
}

function checkValue(mnt) {
	if (isNaN(mnt)) { return parseFloat(0); }
	else if (mnt == '') { return parseFloat(0); }
	else
	{
		return parseFloat(mnt);
	}
}

function line_item_validate(myform)
{
	
	//  Check to see if the Return matches the Valance Height
	if ($('newline_return_needed').value == "Yes")
	{
		if ((parseFloat($('newline_valance_height').value) + parseFloat($('newline_valance_height_fraction').value)) !=  (parseFloat($('newline_return_height').value) + parseFloat($('newline_return_height_fraction').value)) && $('newline_return_needed').value == "Yes")
		{
			alert("The valance height you have selected does not match the return height you have selected.");
			return false;
		}
		else
		{
			return true;
		}
	}
	
	// Check to see if the panel widths equal the width
	if (parseInt($('newline_seam_number').value) > 0)
	{
		flag = true;
		//  We have a seamed panel
		if ( parseInt($('newline_seam_number').value) == 1 )
		{ 			
			panel_total = ( checkValue($('newline_panel_1').value) + checkValue($('newline_panel_2').value) );
			flag = (checkValue($('newline_panel_1').value) > 0)
					&& (checkValue($('newline_panel_2').value) > 0);			
		}
		else if (parseInt($('newline_seam_number').value) == 2)
		{
			panel_total = ( checkValue($('newline_panel_1').value) + checkValue($('newline_panel_2').value) + checkValue($('newline_panel_3').value) );		
			flag = (checkValue($('newline_panel_1').value) > 0)
					&& (checkValue($('newline_panel_2').value) > 0)
					&& (checkValue($('newline_panel_3').value) > 0);
		}
		else if (parseInt($('newline_seam_number').value) == 3)
		{
			panel_total = ( checkValue($('newline_panel_1').value) + checkValue($('newline_panel_2').value) + checkValue($('newline_panel_3').value) + checkValue($('newline_panel_4').value) );
			flag = (checkValue($('newline_panel_1').value) > 0)
					&& (checkValue($('newline_panel_2').value) > 0)
					&& (checkValue($('newline_panel_3').value) > 0)
					&& (checkValue($('newline_panel_4').value) > 0);
		}
		else if (parseInt($('newline_seam_number').value) == 4)
		{
			panel_total = ( checkValue($('newline_panel_1').value) + checkValue($('newline_panel_2').value) + checkValue($('newline_panel_3').value) + checkValue($('newline_panel_4').value) + checkValue($('newline_panel_5').value) );			
			flag = (checkValue($('newline_panel_1').value) > 0)
					&& (checkValue($('newline_panel_2').value) > 0)
					&& (checkValue($('newline_panel_3').value) > 0)
					&& (checkValue($('newline_panel_4').value) > 0)
					&& (checkValue($('newline_panel_5').value) > 0);
		}
		
		width_total = (checkValue($('newline_width').value) + checkValue($('newline_width_fraction').value));						
		if (!flag){
			alert('Some of panels have zero width. Please check your sizes and try again.');
			return false;
		}
		
		if ( panel_total != width_total )
		{
			alert('The combined total of your seamed panel widths do not match the total shade width you have specified.  Please check your sizes and try again.');
			return false;
		}
		else
		{
			return true;
		}
	}

}


function updateLeght(seam_width){
	
	if ( parseInt($('newline_seam_number').value) == 1 ){
		$('newline_panel_1').value = checkValue($('i_panel_1').value) + checkValue($('f_panel_1').value);
		$('newline_panel_2').value = checkValue($('i_panel_2').value) + checkValue($('f_panel_2').value);
		leftovers($('newline_panel_1').value, $('newline_panel_2').value,0,0,0,$('total_width').value,seam_width);
	}
	
	if ( parseInt($('newline_seam_number').value) == 2 ){
		$('newline_panel_1').value = checkValue($('i_panel_1').value) + checkValue($('f_panel_1').value);
		$('newline_panel_2').value = checkValue($('i_panel_2').value) + checkValue($('f_panel_2').value);
		$('newline_panel_3').value = checkValue($('i_panel_3').value) + checkValue($('f_panel_3').value);
		leftovers($('newline_panel_1').value, $('newline_panel_2').value,$('newline_panel_3').value,0,0,$('total_width').value,seam_width);
	}
	
	if ( parseInt($('newline_seam_number').value) == 3 ){
		$('newline_panel_1').value = checkValue($('i_panel_1').value) + checkValue($('f_panel_1').value);
		$('newline_panel_2').value = checkValue($('i_panel_2').value) + checkValue($('f_panel_2').value);
		$('newline_panel_3').value = checkValue($('i_panel_3').value) + checkValue($('f_panel_3').value);
		$('newline_panel_4').value = checkValue($('i_panel_4').value) + checkValue($('f_panel_4').value);				
		leftovers($('newline_panel_1').value, $('newline_panel_2').value,$('newline_panel_3').value,$('newline_panel_4').value,0,$('total_width').value,seam_width);
	}
	
	if ( parseInt($('newline_seam_number').value) == 4 ){
		$('newline_panel_1').value = checkValue($('i_panel_1').value) + checkValue($('f_panel_1').value);
		$('newline_panel_2').value = checkValue($('i_panel_2').value) + checkValue($('f_panel_2').value);
		$('newline_panel_3').value = checkValue($('i_panel_3').value) + checkValue($('f_panel_3').value);
		$('newline_panel_4').value = checkValue($('i_panel_4').value) + checkValue($('f_panel_4').value);				
		$('newline_panel_5').value = checkValue($('i_panel_5').value) + checkValue($('f_panel_5').value);						
		leftovers($('newline_panel_1').value, $('newline_panel_2').value,$('newline_panel_3').value,$('newline_panel_4').value,$('newline_panel_5').value,$('total_width').value,seam_width);
	}
}
