// JavaScript Document

function dollarformat(num){
	if(num.length>6){
		rem=num.length-6;
		numform=num.substr(0,rem)+','+num.substr(rem);
	}else{
		numform=num;
	}
	return numform;
}
function sort_it_up() { 
	document.sort_form.submit();
}

function update_option(what_pic,opt_sku,opt_image_small,opt_image_large){
	//alert(opt_image_small);
	//alert(opt_image_small);
	
	if(opt_image_small != '' && opt_image_small != 'x'){
		document.images["small_pic" + what_pic].src =imagepath + opt_image_small;
	}
	
	//squ
	if(opt_sku != 'x'){
		sku_name = 'sku' + what_pic;	
		document.getElementById(sku_name).innerHTML = opt_sku;
	}
}

function format_price(y,num1){
	//num1=0;
	//can_price=0;
	base_cost = document['form_'+y]['current_price'+y].value; //this is the product price with no options
	can_price = 0;
	
	//add all the options on the page togethet + the product price
	var num2 = 0;
	//alert(num1);
	for (var i=0; i < document['form_'+y].length; i++){
		if(document['form_'+y].elements[i].name.search(/option_/) != '-1'){
			f = document['form_'+y].elements[i].value.split( "," );
			num2 = Number(num2) + Number(f[2]);
			//alert(num2);
		}
	}
	//alert();
	the_sum = Number(base_cost)+num2;//Number(base_cost) + Number(num2)
	//alert(the_sum);
	num = parseFloat(the_sum);//opt price
	price_name = 'price' + y;
	
	if(num==0){
		//num == '0' && cents == '00'
		//
		document.getElementById(price_name).innerHTML = 'Please Select an option to see Price';
	} else {
		num_c = 0;
		num_c =0;
		
		listprice=document['form_'+y]['list'+y].value;
		listprice=parseFloat(listprice).toFixed(2);
		listprice=Number(listprice)+Number(num2);
		listout="";
		if(Number(listprice)>Number(num)){
			dif=(Number(listprice)-Number(num)).toFixed(2);
			listout='<span style="text-decoration:line-through">List: $'+dollarformat(listprice.toFixed(2))+'</span><br>';
			listout=listout+"<b>You save : $"+dollarformat(dif)+"</b><br>";
		}
		
		<!---if(isNaN(num_c))
		num_c = "0";
		sign = (num_c == (num_c = Math.abs(num_c)));
		num_c = Math.floor(num_c*100+0.50000000001);
		cents_c = num_c%100;
		num_c = Math.floor(num_c/100).toString();
		if(cents_c<10)
		cents_c = "0" + cents_c;
		for (var i = 0; i < Math.floor((num_c.length-(1+i))/3); i++)
		num_c = num_c.substring(0,num_c.length-(4*i+3))+','+num_c.substring(num_c.length-(4*i+3));
		can_pri=0;
		//if(num_c == 0 && cents_c == 0){
		//	can_pri = '<br>This item is not available in Canada';
		//}else{
		//	can_pri = '<br>CAN $' + num_c + '.' + cents_c + '+ shipping & handling';
		//}
		//document.getElementById(price_name).innerHTML = 'Price: ' + num + '.' + cents;
		//alert(num)
		document.getElementById(price_name).innerHTML = listout+'Price: $' + dollarformat(num.toFixed(2)) <!---num + '.' + cents+ can_pri--->;


	}
	
}

 function update_price(x,y){
	//add it up or just update the price displayed based on if the option is default or not
	//alert(x); 
	//alert(y);
	
	var x_arr = x.split( "," );
	
	//define the vars we will be using
	var option_id = x_arr[0];
	var option_choice = x_arr[1];
	var option_price = x_arr[2];
	var weight = x_arr[3];
	var sku = x_arr[4];
	var quantity = x_arr[5];
	var small_image = x_arr[6];
	var large_image = x_arr[7];
	var op_type = x_arr[8];
	
	//var quant_procedure_backorder = <!--- #allow_zero_order# --->;
	var quant_procedure_backorder = false;
	if(quantity == 0 && quant_procedure_backorder == false && option_id != 'option_error' && option_id != 'no_option_selected'){
		price_name = 'price' + y;
		butt_name = 'butts' + y;
		document.getElementById(price_name).innerHTML = backorder_message;
		document.getElementById(butt_name).style.display = 'none'; 
	} else {
		butt_name = 'butts' + y;
		document.getElementById(butt_name).style.display = ''; 
		if(option_id == 'option_error' || option_id == 'no_option_selected'){
			format_price(y,0)
			
			if(quantity != 'x'){
				quant_name = 'quantity' + y;	
				document.getElementById(quant_name).innerHTML = quantity;
			}
			
			//pic and sku
			if(op_type == 'c'){update_option(y,sku,large_image);};
		}else if(op_type == 'd'){
			//default option, only update price
			format_price(y,option_price);
			update_option(y,sku,small_image,large_image);
			
		}
		else if(op_type == 'c'){
			//custome option, update price sku image quantity
			format_price(y,option_price)
			
			//now we need to update the quantity (if displayed)	
			/*if(quantity != 'x'){
				quant_name = 'quantity' + y;	
				document.getElementById(quant_name).innerHTML = quantity;
			}*/
			
			//now we will call a function on the product_line or detail page that will, update the image and the sku
			update_option(y,sku,small_image,large_image);
		}
	}
}
function openWin( windowURL, windowName, windowFeatures ) { 
	return window.open( windowURL, windowName, windowFeatures ); 
}

function checklen(numlen){
	if(this.value.length<=numlen){
		return true;
	}else{
		return false;
	}
}
