
// check form //
function checkform(args,formname) {

var ok = true;
var argArray = args.split(",");

	for (i=0;i<argArray.length; i++) {
		
		if (document.getElementById(argArray[i]).value == "") {
			
			alert("(*) Please enter all required information");
			ok = false;
			break;
		}
		
	}
	
	if (ok) {
		formname.submit();
	}

}

//* GET THE SCREEN SIZE AND MOVE THE SITE DIV TO CENTER WHENEVER THE SCREEN IS RESIZED*/

function getWindowSize() {
	
	try {

	var winW = 0;

	if (parseInt(navigator.appVersion)>3) {
 		if (navigator.appName=="Netscape") {
  			winW = window.innerWidth;
 		}
 		if (navigator.appName.indexOf("Microsoft")!=-1) {
  			winW = document.body.offsetWidth;
 		}
	}

	var strLeft = (winW-710)/2;

	document.getElementById("site").style.left = strLeft+"px";
	
	} catch (ex) {
		// do nothing
	}
	
}
// This moves the site div when window resizes
window.onresize = getWindowSize;


// add to basket

function addToBasket(id, qty) {
	
	var newitem = id + "^" + qty;
	var newlist;
	
	var currentlist = getCookie("basketListOfIds");
	var currentcount = getCookie("basketNumOfBottles");
	if (currentlist == "") {
		newlist = newitem;
	} else {
		newlist = currentlist + "," + newitem;
	}
	
	
	
	
	var newcount = parseInt(currentcount)+parseInt(qty);
	
	setCookie("basketListOfIds", newlist, "#May 10, 2012#")
	setCookie("basketNumOfBottles", newcount, "#May 10, 2012#")
	
	document.getElementById("bottleNum").innerHTML = "You have<br/>"+newcount+" bottles<br/>in your basket";
	
}

function checkAndAdd(itemid,avail){
	
	val = document.getElementById('qty'+itemid).value;
	
	if (isNaN(val) || val == ""){ // Check its a number
		alert("Sorry the quantity you've entered is not valid\nPlease check it and try again");
		document.getElementById('qty'+itemid).focus();
	} else { // Its a number 
		if (val > avail) { // The amount entered is too high
			alert("Sorry we do not have the quantity you have entered in stock\nPlease lower your amount and re-add");
			document.getElementById('qty'+itemid).focus();
		} else { // We're good to go
			addToBasket(itemid, val);
			document.getElementById('qty'+itemid).value = "";
			document.getElementById('add_'+itemid).value = "Added";
			document.getElementById('add_'+itemid).disabled = "disabled";
			document.getElementById('add_'+itemid).style.width = "45px";
		}
	}
}

function checkUpdates(){
	oform=document.getElementById('orderlist');
	errs=false;
	for(i=0;i<oform.length;i++){
		ename=oform[i].name.substr(0,4);		
		if(ename=='item' && isNaN(oform[i].value)) { //they've entered a string
			alert("Sorry the quantity you've entered is not valid\nPlease check it and try again");
			oform[i].focus();
			errs=true;
			break;
		} else if (ename=='item') { // is amount too high?
			wid='avail'+oform[i].name.substring(4);
			//alert(oform[i].value > parseFloat(document.getElementById(wid).value));
			if(oform[i].value > parseFloat(document.getElementById(wid).value)){
				alert("Sorry we do not have the quantity you have entered in stock\nPlease lower your amount and re-add");
				oform[i].focus();
				errs=true;
				break;
			}
		}
	}
	if(!errs){
		oform.submit();
	}
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function wineSearch(){
	
	val = document.getElementById('wine_search2').value;

	window.location = "search_wines.asp?search=" + val;
	
}

