var busy = false;

function rollOff(imgName) {
	imgOff=eval(imgName+"OFF.src");
	document[imgName].src= imgOff;
}

function rollOn(imgName) {
	imgOn=eval(imgName+"ON.src");
	document[imgName].src= imgOn;
}

function AddSears2ShopCart(form) {
	
	if (!(validModel(form) && validPrice(form) && gimmeDate(form))) {
		return false;
	}
	
	if (busy) {
		return false;
	}
	
	busy = true;

	return true;

}

function Add2ShopCart(form) {
	
	if (!(validModel(form) && validPrice(form) && gimmeDate(form))) {
		return false;
	}
	
	if (busy) {
		return false;
	}
	
	busy = true;
	
	// Set the attrValue (the item identity passed to the shopping cart) to the entered price range
	if (form.price.value > 0 && form.price.value <= 300) {
		form.productSku[0].checked = "checked";
	} else if (form.price.value > 300 && form.price.value <= 1000) {
		form.productSku[1].checked = "checked";
	} else if (form.price.value > 1000 && form.price.value <= 2000) {
		form.productSku[2].checked = "checked";
	} else if (form.price.value > 2000 && form.price.value <= 3000) {
		form.productSku[3].checked = "checked";
	}

	return true;

}

function validPrice(form) {

	var success = "";
	// Make sure only integers and one decimal point (.) entered in price field
	var priceLength = form.price.value;
	var periodTimes = 0;
	var test = form.price.value;
	var centPosition = form.price.value.length - 3;
	if (form.price.value == 0 || form.price.value == "") {
		alert("Please enter purchase price like xxxx.xx\nEx: 999.99 or 2500.00");
		form.price.focus();
		return false;
	}
	else {
		for (var i = 0; i < priceLength.length; i++) {
			success = false;
			if (test.charAt(i) == 0) {
				success = true;
			} else if (test.charAt(i) == 1) {
				success = true;
			} else if (test.charAt(i) == 2) {
				success = true;
			} else if (test.charAt(i) == 3) {
				success = true;
			} else if (test.charAt(i) == 4) {
				success = true;
			} else if (test.charAt(i) == 5) {
				success = true;
			} else if (test.charAt(i) == 6) {
				success = true;
			} else if (test.charAt(i) == 7) {
				success = true;
			} else if (test.charAt(i) == 8) {
				success = true;
			} else if (test.charAt(i) == 9) {
				success = true;
			} else if (test.charAt(i) == ".") {
				periodTimes += 1;
				success = true;
			}
			if (success != true) {
				alert("Please make sure your purchase price only contains numbers.");
				return false;
			}
			if (periodTimes > 1) {
				alert("Please check you that your purchase price only\nhas one period between dollars and cents.");
				return false;
			}
		}
		// Add cents to the price string if there are none or have the user finish their entry if only partialy filled like 999.2 or 999.
		if (form.price.value.indexOf(".") == -1) {
			alert("Please enter purchase price like xxxx.xx\nEx: 999.99 or 2500.00");
			return false;
		}
		else if (form.price.value.indexOf(".") != centPosition) {
			alert("Please enter purchase price like xxxx.xx\nEx: 999.99 or 2500.00");
			return false;
		}
	}
	// The purchase price cannot be more than $3000
	if(form.price.value > 3000.00) {
		alert("Sorry, the purchase price of your product is too great\nto cover with our Extended Service Plan.");
		return false;
	}
	
	return success;
}
function validModel(form) {

	// Make sure info is entered for model and serial numbers
	
	if (form.model.value != 0 || form.model.value != "") {
		return true;
	} else {
		alert("Please fill in the necessary model information.");
		form.model.focus();
		return false;
	}
}

//----------------------------------------------------------------------
// Note: The JavaScript Date Constructor:  Date(String) is unfortunately 
// inconsistent across browsers, so I am using an alternative approach.
// Take our jQuery Date String and split it into year, month, and day
//----------------------------------------------------------------------
function gimmeDate(form) {

	if (form.whenPurchased.value == '') {
		alert("Please select your purchase date.");
		return false;
	}

	//----------------------------------------------------------------------
	// Days used to validate that the selected date is not to old:  Use 367,
	// to make it valid through the end of the day and in case it's a leap year.
	//----------------------------------------------------------------------
	var allowedDays = 367; 
	var dayInMilliseconds = 1000*60*60*24;
	var success = false;  // Only give success when purchase Date is aproximately 1 year or less from today's date
	
	var purchaseDateString = form.whenPurchased.value;
	var purchaseDateArray  = purchaseDateString.split("-");

	var purchaseDate = new Date();
		purchaseDate.setYear(purchaseDateArray[0]);
		purchaseDate.setMonth(purchaseDateArray[1] - 1);	//0 based
		purchaseDate.setDate(purchaseDateArray[2]);
	
	var currentDate = new Date();
	var currentDateMilliseconds = currentDate.getTime();
	var purchaseDateMilliseconds = purchaseDate.getTime();
		
	if (currentDateMilliseconds - purchaseDateMilliseconds < 0) {
		//Use the current date, not what the customer entered:
		var today = currentDate.getFullYear() + ' - ' + (currentDate.getMonth() + 1) + ' - ' + currentDate.getDate();
		alert("Your purchase date must be before today. \nToday is " + today + ".");
		
	} else {
		// Test: < 1 year old (in milliseconds)
		if ((currentDateMilliseconds - purchaseDateMilliseconds) <= (dayInMilliseconds * allowedDays)) {
			success = true;
			
		} else {
			alert("Sorry, your product was purchased too long ago to be \ncovered by our Extended Service Plans.");
		}
	}
	
	return success;
}

//----------------------------------------------------------------------
// removeCartDetail(id) - Modified for the new UTS layout.
//----------------------------------------------------------------------
function removeCartDetail(id) {

	if (document.checkoutTermsConditionsSubmitForm.quantity[id]) {
		document.checkoutTermsConditionsSubmitForm.quantity[id].value = 0;
		
	} else {
		document.checkoutTermsConditionsSubmitForm.quantity.value = 0;
	}
	
	document.checkoutTermsConditionsSubmitForm.submit();
}


//----------------------------------------------------------------------
// This is the original 'removeCartDetail' with Sears appended to it:
// When it's decided to convert SEARS to use the new interface, 
// this function should be removed.  A lot should be rewritten.
//----------------------------------------------------------------------
function removeCartDetailSears(id) {

	if (document.shoppingCartUpdateForm.quantity[id]) {
		document.shoppingCartUpdateForm.quantity[id].value = 0;
		
	} else {
		document.shoppingCartUpdateForm.quantity.value = 0;
	}
	
	document.shoppingCartUpdateForm.submit();
}


//----------------------------------------------------------------------
// This is no longer used in the new UTS flow... may still need to be
// used by SEARS.  Otherwise get rid of it... 
// It is less than ideal logic that was used to validate 2 forms... 
// not sure how it actually worked... it seemed like it shouldn't work.
//----------------------------------------------------------------------
function nextStep(form, action) {
	form.action = action;
	form.submit();
}

function openPopup(pageName, width, height) {

	// default values
	if(typeof(width)=='undefined')	width = 470;
	if(typeof(height)=='undefined')	height = 300;

	window.open("/UTS/popUpDisplay.do?pageName=" + pageName, null, 'status=0,toolbar=0,scrollbars=0,resizable=1,width=' + width + ',height=' + height);
}

function alertFailure(XMLHttpRequest, textStatus, errorThrown) {

	if (XMLHttpRequest.responseText != '') {
		alert(XMLHttpRequest.responseText);
	} else {
		alert("There was a problem processing your request");
	}
}

function submitAjaxForm(form, callback) {

	submitAjaxForm2(form, callback, alertFailure);

	return false;
}

function submitAjaxForm2(form, callbackSuccess, callbackFailure) {
			
	 $.ajax({
	   type: form.method,
	   url: form.action,
	   timeout: 10000,
	   data: $(form).serialize(),
	   dataType: 'text',
	   success: callbackSuccess,
	   error: callbackFailure
	 });
	return false;
}

