﻿function SetUniqueRadioButton(nameregex, current) {
	re = new RegExp(nameregex);
	for (i = 0; i < document.forms[0].elements.length; i++) {
		elm = document.forms[0].elements[i]
		if (elm.type == 'radio') {
			if (re.test(elm.name)) {
				elm.checked = false;
			}
		}
	}
	current.checked = true;
}

function ToggleRangeInfo(infoDivID, anchorTagID, sashTagID) {

	if (document.getElementById(infoDivID).style.display != 'block') {
		document.getElementById(infoDivID).style.display = 'block';
		document.getElementById(anchorTagID).style.display = 'none';
		document.getElementById(sashTagID).style.display = 'none';
	}
	else {
		document.getElementById(infoDivID).style.display = 'none';
		document.getElementById(anchorTagID).style.display = 'block';
		document.getElementById(sashTagID).style.display = 'block';
	}
}

function ShowSaveQuote() {
	document.getElementById('hiddenSaveQuote').style.display = 'block';
}

function ShowAndHideDivs(divToShow, divToHide) {

	document.getElementById(divToHide).style.display = 'none';
	document.getElementById(divToShow).style.display = 'block';
}

function TryParseInt(str,defaultValue){

	var retValue = defaultValue;
	if(typeof str != 'undefined' && str!=null && str.length>0){
        if (!isNaN(str)){
	        retValue = parseInt(str);
         }
	}
	return retValue;
}

function IncrementQuantity(qtyTextBox, maxQuantity, updateButton){
	//check the quantity is valid
	var qty = TryParseInt(document.getElementById(qtyTextBox).value, 0);
	
	if (qty < maxQuantity || maxQuantity == 0){
		qty++;
		document.getElementById(qtyTextBox).value = qty.toString();
		__doPostBack(updateButton, null);
	}
}

function DecrementQuantity(qtyTextBox, updateButton) {
	//check the quantity is valid
	var qty = TryParseInt(document.getElementById(qtyTextBox).value, 0);
	
	if (qty > 1){
		qty--;
		document.getElementById(qtyTextBox).value = qty.toString();
		__doPostBack(updateButton, null);
	}
}

function ValidateQuantity(obj, args) {
	if (parseInt(args.Value) < 1 || parseInt(args.Value) > 99) {
		args.IsValid = false
		return;
	}
	else {
		args.IsValid = true;
		return;
	}
}
function WarnRemovingGreenhouse() {
	return confirm('This will remove the greenhouse and all integrated accessories. Are you sure?');
}

var timeoutArray = {}; //holds the timeout functions for each button.

function onQtyKeyUp(qtyTextBox, btnUpdate) {
	timeoutString = "updateQuantity('" + btnUpdate + "');";
	timeoutArray[qtyTextBox] = setTimeout(timeoutString, 500);
}

function onQtyKeyDown(qtyTextBox) {
	
	if (timeoutArray[qtyTextBox])
		clearTimeout(timeoutArray[qtyTextBox]);
}

function updateQuantity(btnUpdate) {
	
	__doPostBack(btnUpdate, null);
}

function pageLoad(sender, args) {

	var pageManager = Sys.WebForms.PageRequestManager.getInstance();

	// add a function to execute when an asynchronous postback is initialized
	pageManager.add_initializeRequest(checkAsyncPostback);

}

function checkAsyncPostback(sender, arg) {
	var pageManager = Sys.WebForms.PageRequestManager.getInstance();
	// check if an async postback is already in progress
	if (pageManager.get_isInAsyncPostBack()) {
		// cancel this async postback if one is currently in progress
		arg.set_cancel(true);
	}
}

