var filter = new Array();
var productVariants = new Array();
var tooltips = new Array();
var tooltipsNo = -1;
var variantError = new Array;
var boolError = true;

function setBagButtons() {
	var isVariation = checkIsVariation();
	var SelectedFilter = getSelectedFilter();
	$("#divActionsOff").css('display', 'none');
	$("#divActions").css('display', 'block');
	$("#divOutofStock").css('display', 'none');
	$('#unavailableDiv').css('display', 'none');
	$('#addWishList').css('display', 'block');
	//Select Variant
	if (isVariation && productVariants.length > 0) {
		$('#pid').val(productVariants[SelectedVariant].ID);
		if (productVariants[SelectedVariant].price != productVariants[SelectedVariant].salesprice) {
			$('#standardPrice').html(productVariants[SelectedVariant].price);
		}
		$('#salesPrice').html('').html(productVariants[SelectedVariant].salesprice);
		$('#divPriceVariant').css('display', 'block');
		$('#divPriceDefault').css('display', 'none');
		$('#divAvailability').css('display', 'block');
		$('#divAvailability').html(stockMessages.get(productVariants[SelectedVariant].ID));
		$('#divOutofStock').css('display', 'none');
		$('#divActions').css('display', 'block');
		$('#divunderaddtobag').css('display', 'none');
	} else {
		if (productVariants.length > 0) {
			$('#divPriceVairant').css('display', 'none');
		}
		$('#divPriceDefault').css('display', 'block');
		$('#divAvailability').css('display', 'none');
	}
} /* This function is literally the same -- 3 times with only 1 value that's different. */

function checkDefaultSelection(selectedColor) {
	var isVariation = checkIsVariation();
	var SelectedFilter = getSelectedFilter();
	// Set color default selection if none is selected.
	// This applies to a variation master with all variants with no inventory
	var selected_color = false;
	for (x = 0; x < filter.length; x++) {
		if (filter[x].ID == 'color') {
			for (y = 0; y < filter[x].value.length; y++) {
				if (selectedColor != '') {
					if (filter[x].value[y] == selectedColor) {
						filter[x].selected = y;
					}
				} else {
					filter[x].selected = 0;
				}
			}
		}
	}
	// Set style ID default if only one style is available
	var styleID = null;
	var y = false;
	for (x = 0; x < productVariants.length; x++) {
		if (styleID == null) {
			y = true;
			styleID = productVariants[x].styleID;
		} else {
			if (styleID != productVariants[x].styleID) {
				y = false; // multiple styles
				break;
			} else {
				y = true; // only one style
				styleID = productVariants[x].styleID;
			}
		}
	}
	if (y == true) {
		$('#styleLabel').css('visibility', 'visible');
		$('#styleID').html(styleID);
	}
	setBagButtons();
}

function checkDefaultSelectionSize(selectedColor) {
	var isVariation = checkIsVariation();
	var SelectedFilter = getSelectedFilter();
	// Set color default selection if none is selected.
	// This applies to a variation master with all variants with no inventory
	var selected_color = false;
	for (x = 0; x < filter.length; x++) {
		if (filter[x].ID == 'size') {
			for (y = 0; y < filter[x].value.length; y++) {
				if (selectedColor != '') {
					if (filter[x].value[y] == selectedColor) {
						filter[x].selected = y;
					}
				} else {
					filter[x].selected = 0;
				}
			}
		}
	}
	// Set style ID default if only one style is available
	var styleID = null;
	var y = false;
	for (x = 0; x < productVariants.length; x++) {
		if (styleID == null) {
			y = true;
			styleID = productVariants[x].styleID;
		} else {
			if (styleID != productVariants[x].styleID) {
				y = false;
				break;
			} else {
				y = true;
				styleID = productVariants[x].styleID;
			}
		}
	}
	if (y == true) {
		$('#styleLabel').css('visibility', 'visible');
		$('#styleID').html(styleID);
	}
	setBagButtons();
}

function checkDefaultSelectionInseam(selectedColor) {
	var isVariation = checkIsVariation();
	var SelectedFilter = getSelectedFilter();
	// Set color default selection if none is selected.
	// This applies to a variation master with all variants with no inventory
	var selected_color = false;
	for (x = 0; x < filter.length; x++) {
		if (filter[x].ID == 'inseam') {
			for (y = 0; y < filter[x].value.length; y++) {
				if (selectedColor != '') {
					if (filter[x].value[y] == selectedColor) {
						filter[x].selected = y;
					}
				} else {
					filter[x].selected = 0;
				}
			}
		}
	}
	// Set style ID default if only one style is available
	var styleID = null;
	var y = false;
	for (x = 0; x < productVariants.length; x++) {
		if (styleID == null) {
			y = true;
			styleID = productVariants[x].styleID;
		} else {
			if (styleID != productVariants[x].styleID) {
				y = false; // multiple styles
				break;
			} else {
				y = true; // only one style
				styleID = productVariants[x].styleID;
			}
		}
	}
	if (y == true) {
		$('#styleLabel').css('visibility', 'visible');
		$('#styleID').html(styleID);
	}
	setBagButtons();
}
// More redundancy

function isShowFilter(n) {
	result = false;
	for (var x = 0; x < allShowFilter.length; x++) {
		allShowFilter[x] == n ? result = true : 0;
	}
	return result;
}

function isShowTooltip(n) {
	result = false;
	for (var x = 0; x < allShowTooltip.length; x++) {
		allShowTooltip[x] == n ? result = true : 0;
	}
	return result;
}

function getSelectedFilter() {
	var SelectedFilter = new Array();
	for (a = 0; a < filter.length; a++) {
		if (filter[a].selected != null) {
			SelectedFilter.push(a);
		}
	}
	return SelectedFilter;
}

function checkIsVariation() {
	return setSelections(); //Return isVariation (true or false)
}

function viewUnavailableMessage() {
	newMessage = filter[0].displayValue[filter[0].selected] + " " + unavailableMessage + " ";
	for (x = 1; x < filter.length; x++) {
		(x > 1) ? newMessage += ',' : 0;
		newMessage += filter[x].displayValue[filter[x].selected];
	}
	$('#unavailableDivInner').html(newMessage);
	$('#unavailableDiv').css('display', 'block');
	$('#divAvailability').css('display', 'block');
	$('#divAvailability').html(outofstockMessage);
	$('#divOutofStock').css('display', 'block');
	$('#divActions').css('display', 'none');
	$('#divunderaddtobag').css('display', 'none');
}

function setSelections() {
	isVariation = false;
	if (filter.length > 0) {
		SelectedVariant = null;
		_isVariation = 0;
		SelectedFilter = getSelectedFilter();
		for (x = 0; x < filter.length; x++) {
			if (filter[x].ID == "color") {
				color = filter[x].value[filter[x].selected];
				shownColor = color;
				selectedColor = color;
			}
			if (filter[x].value.length == 1 && filter[x].value[0] == "O/S") {
				$('#' + filter[x].ID + '_O/S').css('display', 'none');
				$('#pSizes').css('display', 'none');
				$('#imgSizeChart').css('display', 'none');
				$('#one_color').css('display', 'block');
				$('#one_color').html($('#sm_color').html());
				$('#sm_color').css('display', 'none');
			}
		}
		for (a = 0; a < filter.length; a++) {
			for (b = 0; b < filter[a].value.length; b++) {
				filter[a].style[b] = filter[a].StyleOffDefault;
				isActiv = false;
				_isActiv = 0;
				for (c = 0; c < productVariants.length; c++) {
					_isVariation = 0;
					for (d = 0; d < SelectedFilter.length; d++) {
						if (SelectedFilter[d] == a) {
							if (productVariants[c][filter[SelectedFilter[d]].ID] == filter[a].value[b]) {
								_isActiv++;
							}
						} else {
							if (
							productVariants[c][filter[SelectedFilter[d]].ID] == filter[SelectedFilter[d]].value[filter[SelectedFilter[d]].selected] && productVariants[c][filter[a].ID] == filter[a].value[b]) {
								_isActiv++;
							}
						}
					}
					for (d = 0; d < filter.length; d++) {
						if (productVariants[c][filter[d].ID] == filter[d].value[filter[d].selected]) {
							_isVariation++;
						}
					}
					if (filter.length == _isVariation) {
						isVariation = true;
						SelectedVariant = c;
					}
					if (SelectedFilter.length <= _isActiv) {
						filter[a].style[b] = filter[a].StyleDefault;
						isActiv = true;
					} else {
						filter[a].style[b] = filter[a].StyleOffDefault;
						_isActiv = 0;
					}
				}
				if (filter[a].selected == b) {
					if (isActiv) {
						filter[a].style[b] = filter[a].StyleSelected;
					} else {
						filter[a].style[b] = filter[a].StyleOffSelected;
					}
				}
				if ('size' == filter[a].ID && 1 == filter[a].value.length && 'UNS' == filter[a].value[0]) {
					// leave it as StyleDefault
				} else {
					//remove existing class, add new class
					var existingClasses = filter[a].StyleDefault + ' ' + filter[a].StyleOffDefault + ' ' + filter[a].StyleSelected + ' ' + filter[a].StyleOffSelected;
					var thisFilterLink = $('#' + filter[a].ID + '_' + filter[a].value[b]);
					var thisFilterElement = thisFilterLink.parent();
					thisFilterLink.removeClass(existingClasses);
					thisFilterLink.addClass(filter[a].style[b]);
					thisFilterElement.removeClass(existingClasses);
					thisFilterElement.addClass(filter[a].style[b]);
				}
			}
		}
		for (x = 0; x < filter.length; x++) {
			if (filter[x].selected != null) {
				$('#idDiv' + filter[x].ID + 'Name').html (filter[x].displayValue[filter[x].selected]);
				
				if (x == 0) { $('#sm_' + filter[x].ID).html (filter[x].displayValue[filter[x].selected]); }
				else { $('#sm_' + filter[x].ID).html (filter[x].displayValue[filter[x].selected]); }
			} else {
				$('#idDiv' + filter[x].ID + 'Name').html ('');
				$('#sm_' + filter[x].ID).html ('');
			}
		}
	} else {
		shownColor = selectedColor;
		isVariation = true;
	}
	return isVariation;
}

function variantClick(thisObject, typ, filterID, filterValueID, filterNo) {
	var swatchGroup = thisObject.parent('ul');
	//ensure previous swatches are unselected visually
	swatchGroup.find('a').removeClass('sizeSwatchSelected');
	for (x = 0; x < filter.length; x++) {
		if (filter[x].ID == filterID) {
			if (filter[x].selected == filterValueID) {
			} else {
				filter[x].selected = filterValueID;
			}
		}
	}
	setSelections();
	//ADD TO BAG Button
	setBagButtons();
	if (getSelectedFilter().length == filter.length) {
		if (!checkIsVariation()) {
			viewUnavailableMessage();
			// Set style ID
			$('#styleLabel').css ('visibility', 'hidden');
			$('#styleID').html ('');
		} else {
			$('#unavailableDiv').css ('display', 'none');
			// Set style ID
			if (productVariants[SelectedVariant].styleID != "null") {
				$('#styleLabel').css ('visibility', 'visible');
				$('#styleID').html (productVariants[SelectedVariant].styleID);
				$('#hdnVariant').val (productVariants[SelectedVariant].ID);
				checkAvailability();
			}
		}
	}
	switch (filter[filterNo].ID) {
	case "color":
		var pid = jQuery('div.sku span#masterID').text().replace ('#', '').replace(/^\s+|\s+$/g,"");
		var pcolor = filter[filterNo].value[filterValueID];
		
		
		/* EDIT CART SWATCHES */
		//Remove the _SWATCH suffix and append the primary image suffix
		var ProductImage_SWAP = thisObject.find ('img').attr ('src').replace ('_1_SWATCH.jpg', '_1.jpg');
		//Find the old preset
		var OldPreset = ProductImage_SWAP.split ('/');
		//Search for LuckyBrandJeans in our URL to get the offset
		for (var i=0; i<OldPreset.length; i++) {
			//Find our string, increment, replace the old preset
			if (OldPreset[i] == 'LuckyBrandJeans') { 
				OldPreset[i+1] = 'prod_large';
				//Finish URL reconstruction
				ProductImage_SWAP = OldPreset.join ('/');
				break;
			}
		}
		$('#qvFrame').attr ('src', ProductImage_SWAP);
		/* QUICK VIEW - Scene 7 PID fix */
		if (pid == "") { pid = qv_ProductID; }
		/* PDP SCENE 7 IMAGE VIEWER */
		setImageSet('LuckyBrandJeans/' + pid + '_' + pcolor + '_IS');
		break;
	}
}

function variantOver(thisObject, typ, filterID, filterValueID, filterNo, tooltipsNo) {
	if (filter[filterNo].style[filterValueID] == filter[filterNo].StyleOffDefault
	|| filter[filterNo].style[filterValueID] == filter[filterNo].StyleOffSelected) {
		$('#' + filter[filterNo].ID + "_" + filter[filterNo].value[filterValueID]).attr ('class', filter[filterNo].StyleOffOver);
	}
	else { $('#' + filter[filterNo].ID + "_" + filter[filterNo].value[filterValueID]).attr ('class', filter[filterNo].StyleOver); }
	lastSelected = filter[filterNo].selected;
	if (getSelectedFilter().length == filter.length) {
		if (!checkIsVariation()) {
			viewUnavailableMessage();
		} else { $('#unavailableDiv').css ('display', 'none'); }
	}
	filter[filterNo].selected = lastSelected;
}

function variantOut(thisObject, typ, filterID, filterValueID, filterNo) {
	setSelections();
	if (filter[filterNo].style[filterValueID] == undefined) document.getElementById(filter[filterNo].ID + "_" + filter[filterNo].value[filterValueID]).className = filter[filterNo].StyleDefault;
	else document.getElementById(filter[filterNo].ID + "_" + filter[filterNo].value[filterValueID]).className = filter[filterNo].style[filterValueID];
	if (getSelectedFilter().length == filter.length) {
		if (!checkIsVariation()) {
			viewUnavailableMessage();
		} else {
			document.getElementById("unavailableDiv").style.display = "none";
		}
	}
}

function writeVariation(_filter, _tooltip, divOverTitle, divOverTitleStyle, divOverContentStyle, StyleDefault, StyleOver, StyleSelected, StyleOffDefault, StyleOffOver, StyleOffSelected) {
	//
	// variable 'filter' contains all variants info
	//
	for (x = 0; x < filter.length; x++) {
		if (filter[x].ID == _filter) filterNo = x;
	}
	for (x = 0; x < tooltips.length; x++) {
		if (tooltips[x].ID == _tooltip) tooltipsNo = x;
	}
	filter[filterNo].StyleDefault = StyleDefault;
	filter[filterNo].StyleOver = StyleOver;
	filter[filterNo].StyleSelected = StyleSelected;
	filter[filterNo].StyleOffDefault = StyleOffDefault;
	filter[filterNo].StyleOffOver = StyleOffOver;
	filter[filterNo].StyleOffSelected = StyleOffSelected;
	var ShowTyp = "div";
	var toImg = new Array("color");
	var newOutput = "";
	for (x = 0; x < toImg.length; x++) {
		if (filter[filterNo].ID == toImg[x]) ShowTyp = "img";
	}
	var overTitle = "<div style=\"display:none;\" class=\"" + divOverTitleStyle + "\">" + divOverTitle + "&nbsp;<span id=\"idDiv" + filter[filterNo].ID + "Name\" class=\"" + divOverContentStyle + "\"  ></span></div>\n";
	//
	// special treatment for size "UNS"
	//
	if ('size' == filter[filterNo].ID && 1 == filter[filterNo].value.length && 'UNS' == filter[filterNo].value[0]) {
		newOutput += "<li class='lnkVariant'><div id='" + filter[filterNo].ID + "_" + filter[filterNo].value[0] + "'>One Size</div></li>";
		//
		// everything else
		//
	} else {
		for (x = 0; x < filter[filterNo].value.length; x++) {
			var filtValue = filter[filterNo].value[x];
			filtValue = filtValue.replace(/\\\//g,'\/');
			newOutput += "<li class=\"lnkVariant " + StyleDefault + "\">";
			newOutput += "<input type=\"hidden\" class=\"varThisFilter\" value=\"" + filtValue + "," + ShowTyp + "," + filter[filterNo].ID + "," + x + "," + filterNo + "," + tooltipsNo + "\" />"
			if (ShowTyp == "img") {
				newOutput += "<img src=\"" + imageURL.replace(codeImageCode, filtValue) + "\" class=\"" + StyleDefault + "\"";
			} else {
				newOutput += "<a class=\"" + StyleDefault + "\"";
			}
			newOutput += " id=\"" + filter[filterNo].ID + "_" + filtValue + "\" alt=\"" + filter[filterNo].displayValue[x] + "\">";
			if (ShowTyp != "img") {
				//adjust value here because changing data values causes problems downstream	  
				if (filter[filterNo].ID == "inseam") {
					var rawFilter = $.trim(filter[filterNo].displayValue[x]);
					var filterDisplay = rawFilter.split('=');
					newOutput += (filterDisplay[0].charAt(0)) + "/" + (filterDisplay[1]) + "</a>";
				} else {
					newOutput += filter[filterNo].displayValue[x] + "</a>";
				}
			}
			newOutput += "</li>";
		}
	}
	//
	// if there is only one option, select it
	//
	if (1 == filter[filterNo].value.length) {
		filter[filterNo].selected = 0;
	}
	newOutput = overTitle + newOutput;
	return newOutput;
}

function createFilter(ID, name, value, displayValue) {
	if (isShowFilter(ID)) {
		var count = filter.length;
		value = value.replace(/\//g, '\\/');
		filter[count] = new Object();
		filter[count].ID = ID;
		filter[count].name = name;
		filter[count].value = value.split(",");
		filter[count].value.pop();
		filter[count].displayValue = displayValue.split(",");
		filter[count].displayValue.pop();
		filter[count].selected = null;
		filter[count].style = new Array(filter[count].value.length);
		if (filter[count].value.length == 1 && filter[count].value[0] == "O/S") {
			filter[count].selected = 0;
		}
	} else if (isShowTooltip(ID)) {
		var count = tooltips.length;
		tooltips[count] = new Object();
		tooltips[count].ID = ID;
		tooltips[count].name = name;
		tooltips[count].value = value.split(",");
		tooltips[count].value.pop();
		tooltips[count].displayValue = displayValue.split(",");
		tooltips[count].displayValue.pop();
		tooltips[count].selected = null;
		tooltips[count].style = new Array(tooltips[count].value.length);
	}
}

function createProductVariants(ID, styleID, Filter, FilterValue, price, salesprice, isDefault) {
	var count = productVariants.length;
	var _Filter = Filter.split(",");
	_Filter.pop();
	var _FilterValue = FilterValue.split(",");
	_FilterValue.pop();
	Filter = new Array();
	FilterValue = new Array();
	for (x = 0; x < _Filter.length; x++) {
		if (isShowFilter(_Filter[x])) {
			Filter.push(_Filter[x]);
			FilterValue.push(_FilterValue[x]);
		}
	}
	productVariants[count] = new Object();
	productVariants[count].ID = ID;
	productVariants[count].styleID = styleID;
	productVariants[count].price = price;
	productVariants[count].salesprice = salesprice;
	for (x = 0; x < Filter.length; x++) {
		productVariants[count][Filter[x]] = FilterValue[x];
	}
	if (isDefault) {
		for (x = 0; x < Filter.length; x++) {
			if (filter[x].ID == 'color') {
				for (y = 0; y < filter[x].value.length; y++) {
					if (filter[x].value[y] == FilterValue[x]) filter[x].selected = y;
				}
			}
		}
	}
}

function AddtoCartValidation() {
	var underbag = $('#divunderaddtobag');
	var quantity = $('#Quantity').val();
	// Single Products
	if (productVariants.length == 0 && filter.length == 0) {
		boolError = false;
		$('#divunderaddtobag').css('display', 'block');
	} else {
		// Variation Products
		var isVariation = checkIsVariation();
		var SelectedFilter = getSelectedFilter();
		var found;
		variantError = [];
		if (SelectedFilter.length == filter.length) {
			if (isVariation && productVariants.length > 0) {
				boolError = false;
				$('#divunderaddtobag').css('display', 'block');
				// Override quntity to 1 if set to 0 , blank or non-numeric
				if (quantity <= 0 || empty(quantity) || isNaN(quantity)) {
					$('#Quantity').attr('value', 1);
				} else {
					// Check for maximum quantity
					if (parseInt(quantity) > maxQuantityNo) {
						boolError = true;
						underbag.html(maxQtyMessage);
					}
				}
			} else {
				viewUnavailableMessage();
			}
		} else {
			for (x = 0; x < filter.length; x++) {
				found = false;
				for (y = 0; y < SelectedFilter.length; y++) {
					if (SelectedFilter[y] == x) {
						found = true;
					}
				}
				if (found == false) {
					variantError.push(filter[x].ID);
				}
			}
			if (variantError.length > 1) {
				underbag.html(selectSizeInseamMessage);
				boolError = true;
			} else {
				switch (variantError[0]) {
				case "size":
					underbag.html(selectSizeMessage)
					boolError = true;
					break;
				case "inseam":
					underbag.html(selectInseamMessage);
					boolError = true;
					break;
				}
			}
		}
	}
}

function EditCartValidation() {
	var underbag = $('#divunderaddtobag');
	var quantity = $('#Quantity').val();
	// Single Products
	if (productVariants.length == 0 && filter.length == 0) {
		boolError = false;
		document.getElementById('divunderaddtobag').style.display = 'block';
	} else {
		// Variation Products
		var isVariation = checkIsVariation();
		var SelectedFilter = getSelectedFilter();
		var found;
		variantError = [];
		if (SelectedFilter.length == filter.length) {
			if (isVariation && productVariants.length > 0) {
				boolError = false;
				document.getElementById('divunderaddtobag').style.display = 'block';
				// Override quantity to 0 if set to 0 , blank or non-numeric
				if (quantity < 0 || empty(quantity) || isNaN(quantity)) {
					document.getElementById('Quantity').value = 0;
				} else {
					// Check for maximum quantity
					if (parseInt(quantity) > maxQuantityNo) {
						boolError = true;
						underbag.update(maxQtyMessage);
					}
				}
			} else {
				viewUnavailableMessage();
			}
		} else {
			for (x = 0; x < filter.length; x++) {
				found = false;
				for (y = 0; y < SelectedFilter.length; y++) {
					if (SelectedFilter[y] == x) {
						found = true;
					}
				}
				if (found == false) {
					variantError.push(filter[x].ID);
				}
			}
			if (variantError.length > 1) {
				underbag.update(selectSizeInseamMessage);
				boolError = true;
			} else {
				switch (variantError[0]) {
				case "size":
					underbag.update(selectSizeMessage)
					boolError = true;
					break;
				case "inseam":
					underbag.update(selectInseamMessage);
					boolError = true;
					break;
				}
			}
		}
	}
}
//
// Given color, inseam & size, this function selects the variant as
// if a user clicks and selects the variant. It's used in the checkout
// page where user can re-edit the color, insean &size.
//
// color  : 460
// inseam : [Short|Regular|X-Long|...]
// size   : [28|29|30|31|32|...]
// debug  : [true|false]
//

function selectVariant(color, inseam, size, debug) {
	if (debug) {
		alert('color=' + color + "\n" + 'inseam=' + inseam + "\n" + 'size=' + size);
	}
	var valueInfo = {
		'color': color,
		'inseam': inseam,
		'size': size
	};
	for (var i = 0; i < filter.length; i++) {
		var variantAttribute = filter[i];
		for (var j = 0; j < variantAttribute.value.length; j++) {
			if (valueInfo[variantAttribute.ID] == variantAttribute.value[j]) {
				if (debug) {
					alert("variantClick(this,'div','" + variantAttribute.ID + "'," + j + "," + i + ");");
				}
				variantClick(this, 'div', variantAttribute.ID, j, i);
			}
		}
	}
}
var xmlHttp;

function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}
//this function will be called on onchange of quantity textbox

function checkAvailability() {
	var content;
	if ((document.getElementById('Quantity').value != "") && (document.getElementById("hdnVariant").value != "")) {
		var quantity = document.getElementById('Quantity').value;
		document.getElementById('divAvailability').innerHTML = "";
		document.getElementById('divAvailability').style.display = 'block';
		document.getElementById('divAvailability').style.visible = 'visible';
		content = prodAvailURL + "?variationID=" + document.getElementById("hdnVariant").value + "&quantity=" + quantity;
		createXMLHttpRequest();
		xmlHttp.onreadystatechange = handleStateChangeAvailability;
		xmlHttp.open('GET', content, true);
		xmlHttp.send(null);
	} //not empty
} //checkAvailability

function handleStateChangeAvailability() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			document.getElementById('divAvailability').innerHTML = xmlHttp.responseText;
		}
	}
}
