
var objAJAX				= null;			// the AJAX object
var sp_price			=	0;
var dp_price			=	0;
var pp_price			=	0;
var fp_price			=	0;
var cb_price			=	0;

function initializeAJAX(url,onReady)
{
	delete objAJAX;
	if( window.XMLHttpRequest )
	{
		objAJAX	= new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		objAJAX	= new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert('Error: You cannot run the auto complete because your browser does not support AJAX');
		return;
	}
	objAJAX.onreadystatechange	= onReady;
	objAJAX.open('GET',url,true);
	objAJAX.send(null);
}


function focusFirstElement()
{
	var elem	= document.getElementById("seller_name");
	if( elem )
		elem.focus();
}

function controlDiv( divNum )
{
	div	= document.getElementById('div_'+divNum);
	if( div )
	{
		if( div.style.display == 'none' )
		{
			div.style.display	= 'inline';
		} else {
			div.style.display	= 'none';
		}
	}
	return true;
}



function specifySpPrice(url_root)
{
	var elem	= document.getElementById('single_paint_option');
	if( elem )
	{
		var value	= elem.value;
		if( value != '' )
		{
			val_ary	=	value.split(',');
			sp_price = parseFloat(val_ary[1]);
			initializeAJAX(url_root+'/content/customize.php?spid='+val_ary[0],displaySpTextPrice);
		}
	}
}


function displaySpTextPrice()
{
	if( (objAJAX.readyState==4 || objAJAX.readyState=='complete') && objAJAX.responseText.length > 0 )
	{
		document.getElementById("specify_sp_price_div").innerHTML=objAJAX.responseText 
	}	
}


function specifyDpPrice(url_root)
{
	var elem	= document.getElementById('double_paint_option');
	if( elem )
	{
		var value	= elem.value;
		if( value != '' )
		{
			val_ary	=	value.split(',');
			dp_price = parseFloat(val_ary[1]);
			initializeAJAX(url_root+'/content/customize.php?dpid='+val_ary[0],displayDpTextPrice);
		}
	}
}


function displayDpTextPrice()
{
	if( (objAJAX.readyState==4 || objAJAX.readyState=='complete') && objAJAX.responseText.length > 0 )
	{
		document.getElementById("specify_dp_price_div").innerHTML=objAJAX.responseText 
	}	
}

function specifyPpPrice(url_root)
{
	var elem	= document.getElementById('premium_paint_option');
	if( elem )
	{
		var value	= elem.value;
		if( value != '' )
		{
			val_ary	=	value.split(',');
			pp_price = parseFloat(val_ary[1]);
			initializeAJAX(url_root+'/content/customize.php?ppid='+val_ary[0],displayPpTextPrice);
		}
	}
}


function displayPpTextPrice()
{
	if( (objAJAX.readyState==4 || objAJAX.readyState=='complete') && objAJAX.responseText.length > 0 )
	{
		document.getElementById("specify_pp_price_div").innerHTML=objAJAX.responseText 
	}	
}


function specifyFpPrice(url_root)
{
	var elem	= document.getElementById('frame_paint_option');
	if( elem )
	{
		var value	= elem.value;
		if( value != '' )
		{
			val_ary	=	value.split(',');
			fp_price = parseFloat(val_ary[1]);
			initializeAJAX(url_root+'/content/customize.php?fpid='+val_ary[0],displayFpTextPrice);
		}
	}
}


function displayFpTextPrice()
{
	if( (objAJAX.readyState==4 || objAJAX.readyState=='complete') && objAJAX.responseText.length > 0 )
	{
		document.getElementById("specify_fp_price_div").innerHTML=objAJAX.responseText 
	}	
}

function specifyCbPrice(url_root)
{
	var elem	= document.getElementById('cooler_bag_option');
	if( elem )
	{
		var value	= elem.value;
		if( value != '' )
		{
			val_ary	=	value.split(',');
			cb_price = parseFloat(val_ary[1]);
			initializeAJAX(url_root+'/content/customize.php?cbid='+val_ary[0],displayCbTextPrice);
		}
	}
}


function displayCbTextPrice()
{
	if( (objAJAX.readyState==4 || objAJAX.readyState=='complete') && objAJAX.responseText.length > 0 )
	{
		document.getElementById("specify_cb_price_div").innerHTML=objAJAX.responseText 
	}	
}

function singleDoubleDiv(show, hide)
{
	document.getElementById(show).style.display="block";
	document.getElementById(hide).style.display="none";
}


function checkData()
{
//	var val = document.getElementById('single_double_paint').value;
	for(var i = 0; i < document.add_to_cart.single_double_paint.length; i++)
	{
		if(document.add_to_cart.single_double_paint[i].checked)
		{
			var selected_index = i;
		}
	}
	
	//single paint checks
	if(document.add_to_cart.single_double_paint[selected_index].value == 'single_paint')
	{
		val = document.add_to_cart.single_paint_option.value;
		val_ary	=	val.split(',');
		if(val_ary[0] == 0)
		{
			document.add_to_cart.single_paint_option.focus();
			alert('Single Paint color is a required field!')
			return false;
		}
		
		if(val_ary[0] == 3)
		{
			if(document.add_to_cart.specify_sp_text.value == '')
			{
				document.add_to_cart.specify_sp_text.focus();
				document.add_to_cart.specify_sp_text.select();
				alert('You need to specify a color!')
				return false;
			}
		}
		
	}
	
	//DOUBLE PAINT CHECKS
	if(document.add_to_cart.single_double_paint[selected_index].value == 'double_paint')
	{
		val = document.add_to_cart.double_paint_option.value;
		val_ary	=	val.split(',');
		if(val_ary[0] == 0)
		{
			document.add_to_cart.double_paint_option.focus();
			alert('Two Tone Paint color is a required field!')
			return false;
		}
		
		if(val_ary[0] == 4)
		{
			if(document.add_to_cart.specify_dp_text1.value == '')
			{
				document.add_to_cart.specify_dp_text1.focus();
				document.add_to_cart.specify_dp_text1.select();
				alert('You need to specify a color!')
				return false;
			}
			
			if(document.add_to_cart.specify_dp_text2.value == '')
			{
				document.add_to_cart.specify_dp_text2.focus();
				document.add_to_cart.specify_dp_text2.select();
				alert('You need to specify a color!')
				return false;
			}
		}
	}
	
	//Premium paint check
	if(document.add_to_cart.frame_paint_option.value == 0)
	{
		document.add_to_cart.frame_paint_option.focus();
		alert('Frame color is a required field!')
		return false;
	}

	return true;
	
}

function CalculateTotalPrice(total)
{
	//	var val = document.getElementById('single_double_paint').value;
	for(var i = 0; i < document.add_to_cart.single_double_paint.length; i++)
	{
		if(document.add_to_cart.single_double_paint[i].checked)
		{
			var selected_index = i;
		}
	}
	
	if(document.add_to_cart.single_double_paint[selected_index].value == 'single_paint')
	{
		total += sp_price ;		
	}
	
	if(document.add_to_cart.single_double_paint[selected_index].value == 'double_paint')
	{
		total += dp_price ;	
	}
				
	total += pp_price ;	
	total += fp_price ;	
	total += cb_price ;	
	
	total = Math.round(total*100)/100;
	document.add_to_cart.subtotal.value = total.toFixed(2);
}
