
function validate() {
	/* this will do :
	(1) find out if Minimum charge maybe needed to add to the cart, and add it if total amount is less than $50
	(2) find out if any table cell has "red" color background which have more than stock
	*/
	
	document.getElementsByName("submit")[0].disabled = true;	// disable "Add to cart" submit button
	
	elementsForms = document.getElementsByTagName("input");
	var intErrors = 0;
	var intFirstErrors = "";
	var intCount = 0;
	var TotalAmount = 0;
	
	//alert("length:"+elementsForms.length);
	
	//  iterate all Text input to see if there is any cell with "red" color backgroud
	/* for (var intCounter = 0; intCounter < elementsForms.length; intCounter++) { */
	 for (var intCounter = 1; intCounter < 100; intCounter++) {
		if ( elementsForms[intCounter].type == "text" &&  elementsForms[intCounter].className != "stock") {
			if ( elementsForms[intCounter].style.backgroundColor == "red" ) {
				intErrors++;
				if ( intFirstErrors.length == 0 ) {
					intFirstErrors  = intCounter; // records the first error
				}
			} else {
				if ( elementsForms[intCounter].value > 0 ) {
					//alert("element name:"+elementsForms[intCounter].name);
					intCount++;
				}
				//alert(elementsForms[intCounter].name);
				//TotalAmount = TotalAmount + (elementsForms[intCounter]);
			}
		}
		//alert("counter:"+intCounter);
	}
	
	// if there is any error, warn user and set focus to the first error
	if ( intErrors > 0 ) {
		alert("There is " + intErrors + " item(s) that exceeding our stock. \nPlease correct before adding to the cart. \n\n(*You cannot continue until you correct them.)");
		elementsForms[intFirstErrors].focus();
		document.getElementsByName("submit")[0].disabled = false;
		return false;
	} else {
		if ( intCount == 0 ) {
			alert("It seems no items were selected. Please add at least one itme.");
			document.getElementsByName("submit")[0].disabled = false;
			return false;
		}
		
		//alert(intCount);
		//alert(TotalAmount);
		//return false;  // for temporary use
		return true;
	}
	
}

var sUrl = "show_price.php?sku=";
var http = getHTTPObject();//don't worry about this

function show_price(item) {

	var item2 = item + "[sku]";
	var x = document.getElementsByName(item2);
	http.open("GET", sUrl + (x[0].value), true);
	
	http.onreadystatechange = handleHttpResponse; 	// handle what to do with the feedback
	http.send(null);
}


function handleHttpResponse() {
	//if the process is completed, decide to do with the returned data
	if (http.readyState == 4) {
  		sResults = http.responseText; //results is now whatever the feedback from the asp page was
		if ( sResults != "Error" ) {
			alert("Your price : $" + sResults);
		}
	}
}


function getHTTPObject() {
	var xmlhttp;

  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if (window.ActiveXObject) {
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttp = false;
    }
  }  else  {
	  // if running Mozilla or other browsers
    try {
      xmlHttp = new XMLHttpRequest();
    } catch (e) {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
     alert("Error creating the XMLHttpRequest object.");
  else	
    return xmlHttp;		

}

function check_stock(SKU, stock){
	var order = SKU + "[qty]";
	var order = document.getElementsByName(order);
	var order_qty = order[0].value;
	var back_color = "white";

	if ( order_qty > stock ) {
		alert("Our stock is " + stock + " and it's less than you put. \nPlease decrease quantity. ");
		back_color = "red";
	}

	order[0].style.backgroundColor = back_color;
}

function reset_bg_color(text_box) {
	//alert(text_box.name);
	text_box.style.backgroundColor = "white";
}

