// JavaScript Document
var priceDisplayed = false;

$(document).ready(function() {

$("#showTIP a[title]").tooltip({ 
 
        // use div.tooltip as our tooltip 
        tip: '.tooltip', 
 
        // use fade effect instead of the default 
        effect: 'fade', 
 
        // make fadeOutSpeed similar to browser's default 
        fadeOutSpeed: 100, 
 
        // the time before tooltip is shown 
        predelay: 400, 
 
        // tweak the position 
        position: "bottom right",         
        offset: [0, 25] 
    });});


function changeCur(myModel) 
{
	if ($('#curIMG').attr('src') == '/images/2010/cdn.png') {
		$('#curIMG').attr('src', '/images/2010/usd.png');
		$.get('/include/setConfig.php', { curchange: 'USD'});	
	} else {
		$('#curIMG').attr('src', '/images/2010/cdn.png');
		$.get('/include/setConfig.php', { curchange: 'CAD'});
	}
	if (priceDisplayed) {
		getPrice(myModel);
	}
	return false;
}

function changeUnits(myModel) 
{
	if ($('#unitsIMG').attr('src') == '/images/2010/metric.png') {
		$('#unitsIMG').attr('src', '/images/2010/imperial.png')
		$.get('/include/setConfig.php', { unitchange: 'I'});
	} else {
		$('#unitsIMG').attr('src', '/images/2010/metric.png');
		$.get('/include/setConfig.php', { unitchange: 'M'});
	}
	setVoltage($('#voltage').val(), myModel);
	return false;
}

function setHP(myVar, myModel)
{
	
	$.post('/include/getPhase.php', { id: myVar, model: myModel},
		  function(data){
			$('#phase').html(data);
	  });
	  	$('#voltage').html('<option value="0" selected>Choose </option>');	
		$('#productoverview').show();
		$('#productdata').hide();
	  priceDisplayed = false;
	  return false;
}

function setPhase(myVar, myModel)
{
	
	$.post('/include/getVoltage.php', { id: myVar, hp: $('#hp').val(), model: myModel},
		  function(data){
			$('#voltage').html(data);
	  });
	  	$('#productoverview').show();
		$('#productdata').hide();
	 priceDisplayed = false;
	  return false;
}

function setVoltage(myVar, myModel)
{
	//cable choice?
	$('#price').hide()
	$('#order').hide()
	$('#quote').hide()
	$('#rowcableType').html('&nbsp;');
	$('#finalData').html('&nbsp;');
	
	
	$.post('/include/getDisplayData.php', { volts: myVar, phase: $('#phase').val(), hp: $('#hp').val(), model: myModel},
		  function(data){
			$('#displayData').html(data.data);
			$('#titleAdditions').html(data.title);
			$('#productoverview').hide();
			$('#productdata').show();
	  }, "json"); 

	$.post('/include/getCable.php', { volts: myVar, phase: $('#phase').val(), hp: $('#hp').val(), model: myModel },
		  function(data){
			$('#cable').html(data);
	  });
	priceDisplayed = false;
	return false;
}

function setLength(myVar, myModel)
{
	//cable choice?
	$('#price').hide()
	$('#order').hide()
	$('#quote').hide()
	$('#finalData').html('&nbsp;');
	$.post('/include/getCableChoice.php', { len: myVar, volts: $('#voltage').val(), phase: $('#phase').val(), hp: $('#hp').val(), model: myModel },
		  function(data){
			$('#rowcableType').html(data);
			if ($('#cablechoices').val() == 1) {
				getPrice(myModel);
			}
	  });
	priceDisplayed = false;
	  return false;
}

function getPrice(myModel)
{
	$.post('/include/getPrice.php', { hardusage: $('#cableType').val(), len: $('#cable').val(), 
				volts: $('#voltage').val(), phase: $('#phase').val(), hp: $('#hp').val(), model: myModel },
		  function(data){
				
				$('#mv-sku').val(data.mvSKU)
				$('#mv-sku-extensions').val(data.ext)
				if (data.showPrice) {
					$('#price').show()
					$('#order').show()
					$('#priceData').html(data.price)
				} else {
					$('#quote').show()
				}
				//$('#finalData').html('<p>Description: ' + data.desc +'</p><p>Part Number: ' + data.SKU + '</p>');
				$('#finalData').html(data.SKU);
				
	  }, "json");
	  priceDisplayed = true;
	  return false;
}


function updatePrice(which, mySKU, myValue)
{
	var basePrice = parseFloat($("#base-"+mySKU).val());
	var newPrice = basePrice + (which.selectedIndex * parseFloat(myValue));
	
	var newLength = parseFloat($("#"+mySKU+"-min").val()) + (parseFloat($("#"+mySKU+"-increment").val()) * which.selectedIndex);
	var maxLength = parseFloat($("#"+mySKU+"-maxship").val());
	
	var MVsku = which.name.replace("-extensions", "");
	
	if (newLength > maxLength) {
		$("#mv-sku-"+MVsku).attr('disabled', 'disabled');
		alert ('Please contact the office to order this product.');
	} else {
		$("#mv-sku-"+MVsku).removeAttr('disabled');
	}
	
	$('#'+mySKU).html(addCommas(newPrice.toFixed(2)))
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
