// -------------- VALIDATION -------------------------- //

// Validation: If value exists, then must be greater than 0
$.validator.addMethod("gtrZero", function(value, element) {
	return value.length == 0 || (parseFloat(value) > 0);
}, "*");



// -------------- UTILTIES -------------------------- //

// Disable / Enable Submit button
function ToggleButton(btnName) {
	var submitBtn = $(btnName);
	
	// If disabled
  	if(submitBtn.attr('disabled') == true){
		submitBtn.val(submitBtn.data("old_text"));
		submitBtn.removeAttr("disabled").removeClass('ui-state-disabled');
	}else{
		if(submitBtn.data("old_text") == null) {
			submitBtn.data("old_text",submitBtn.val());
		}
		submitBtn.attr("disabled", true).addClass('ui-state-disabled');
		submitBtn.val("Processing...");
	}
}



// Create Array dynamically
function CreateArray(length) {
    var a = new Array(length || 0);

    if (arguments.length > 1) {
        var args = Array.prototype.slice.call(arguments, 1);
        for (var i = 0; i < length; i++) {
            a[i] = CreateArray.apply(this, args);
        }
    }

    return a;
}

// Create price level type object
function PriceLevelType(prop1, prop2, prop3){
	this.price = parseFloat(prop1);
	this.minValue = parseInt(prop2);
	this.maxValue = parseInt(prop3);
}

var priceLevelArray = null;

// Create Price Level
function CreatePriceLevel(){
	var skus = $(".fnSku");
	// priceLevelArray
	priceLevelArray = CreateArray(skus.length);
	skus.each(function(i,val){
		var priceLevel = (val.value).split('/');
		priceLevelArray[i] = new PriceLevelType(priceLevel[0],priceLevel[1],priceLevel[2]);
	});
}

// Get the correct price in the price levels
var correctPriceLevel = function(qty){
	if (priceLevelArray == null) 
		CreatePriceLevel();
	var qty = parseInt($(".qtyForm").val());
	var price = 0;	
	$.each(priceLevelArray, function(i,obj){
		if(i != priceLevelArray.length-1){
			if(qty <= obj.maxValue){
				price = obj.price;
				return false;
			}
		}else{
			price = obj.price;
			return false;
		}
	});
	return price;
}

// -------------- END -------------------------- //


