// Detect Browser Settings
var Browser = {
	appName: navigator.appName,
	appVersion: parseInt(navigator.appVersion),
	userAgent: navigator.userAgent, 
	IE:     !!(window.attachEvent && !window.opera),
	IE6:	(navigator.userAgent.toLowerCase().indexOf('msie') != -1 && 
				navigator.userAgent.toLowerCase().indexOf('opera') == -1 && 
				parseInt(navigator.appVersion) == 4 && 
				navigator.userAgent.toLowerCase().indexOf('msie 6.') != -1
			),
    Opera:  !!window.opera,
    WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
    Gecko:  navigator.userAgent.indexOf('Gecko') > -1 && navigator.userAgent.indexOf('KHTML') == -1,
    MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/),
	Height: function() {
		if(Browser.Gecko||Browser.WebKit)
			return window.innerHeight + window.scrollMaxY;
		else if(Browser.IE)
			return document.body.clientHeight-0;
		else if(Browser.Opera)
			return window.innerHeight + window.pageYOffset;
	},
	Width: function() {
		if(Browser.Gecko||Browser.WebKit)
			return window.innerWidth;
		else if(Browser.IE)
			return document.body.clientWidth;
		else if(Browser.Opera)
			return window.innerWidth;
	},
	ScrollY: function() {
		if(Browser.Gecko||Browser.WebKit)
			return window.pageYOffset;
		else if(Browser.IE)
			return screen.height;
		else if(Browser.Opera)
			return window.pageYOffset;
	}	
}

function updateCartInfo(div) {
	var url = './cart.php?a=retrieveCartInfo';
	
	new ajax(url, {onComplete:result});
	function result(request){
		var txt = request.responseText;
		$(div).innerHTML = txt;
	}
}

function closeFloatingDivs() {
	if(document.getElementById('floatingDiv')){
		if(Browser.IE6)
			document.body.removeChild($('iframeie6'));
		document.body.removeChild($('floatingDiv'));
		
	}
	$('body').style.overflow='visible';
}

function moveFloatingDiv() {
	if(document.getElementById('floatingDiv')&&document.getElementById('floatingDivContainer')){
		var divContainer = $('floatingDivContainer');
		divContainer.style.marginTop = Browser.ScrollY()+'px';
	}
}

function resizeFloatingDiv() {
	if(document.getElementById('floatingDiv')&&document.getElementById('floatingDivContainer')){
		var divFloat = $('floatingDiv');
		var divContainer = $('floatingDivContainer');
		
		divFloat.style.width = Browser.Width()+'px';
		divFloat.style.height = Browser.Height()+'px';
		divContainer.style.marginTop = Browser.ScrollY()+'px';
		
		if(Browser.IE6){
			var iframe = $('iframeie6');
			iframe.style.width = Browser.Width()+'px';
			iframe.style.height = Browser.Height()+'px';
		}
	}
}

function displayLargeImage(id){
	// MOUSE SCROLLER HANDLER
	$('body').style.overflow = 'hidden';
	if(Browser.Opera)
		$('body').style.overflowY = 'hidden';
	
	/*if(document.attachEvent)
		document.attachEvent('onmousewheel',moveFloatingDiv);
	else
		window.addEventListener('DOMMouseScroll',moveFloatingDiv,false);*/
	if(Browser.IE6){
		var newframe = document.createElement('IFRAME');
		newframe.setAttribute('src','');
		newframe.setAttribute('id','iframeie6');
		newframe.className = 'iframeie6';
		newframe.style.width = Browser.Width()+'px';
		newframe.style.height = Browser.Height()+'px';
		document.body.appendChild(newframe);
	}
	
	var newdiv = document.createElement('div');
	var divIdName = 'floatingDiv';
	
	newdiv.setAttribute('id',divIdName);
	newdiv.className = divIdName;
	
	newdiv.style.width = Browser.Width()+'px';
	newdiv.style.height = Browser.Height()+'px';
	document.body.appendChild(newdiv);
	
	new ajax('./catalogue.php?a=getImage&p='+id, {onComplete: result})
	function result(request){
		var newdivdiv = document.createElement('div');
		var id = 'floatingDivContainer';
		
		newdivdiv.setAttribute('id',id);
		newdivdiv.className = id;
		newdivdiv.innerHTML = request.responseText;
		
		newdivdiv.style.width = Browser.Width()+'px';
		newdivdiv.style.height = Browser.Height()-100+'px';
		newdivdiv.style.marginTop = '20px';
		
		document.getElementById(divIdName).appendChild(newdivdiv);
		
		var image = $('floatingClose');
		image.onclick = function(){
			closeFloatingDivs();
			$('body').style.overflow = 'visible';
		}
		
	}
	
}

String.prototype.ucWords = function() {
	var re = /\s/;
	var words = this.split(re);
	re = /(\S)(\S+)/;
	for (i = words.length - 1; i >= 0; i--) {
		re.exec(words[i]);
		words[i] = RegExp.$1.toUpperCase()
		+ RegExp.$2.toLowerCase();
	}
	return words.join(' ');
} 

function changeCategoriesList(object,target,url){
	var selected = object.options[object.selectedIndex].value;
	new ajax (url+selected, {update: $(target), onComplete: populateMenu});
	
	function populateMenu(request) {
		for(x=target.length;x>=0;x--){
			target[x] = null;
		}
		
		var re = new RegExp("(\<select(.*?)\>)(.*?)(\<\/select\>)","i");

		$(target).innerHTML = request.responseText;
		
		if($(target).outerHTML)
			$(target).outerHTML = $(target).outerHTML.replace(re, "$1"+request.responseText+"$4");
	}
}

function checkLimit(object, limit, target){
	if(limit==null)
		limit = 255;
	var str = ''+object.value.length;
	if(object.value.length > limit)
		object.value = object.value.substring(0,limit);
		
	str = object.value.length+' of '+limit+' characters';
	$(target).innerHTML = str;
}

function updateProduct(pid){
	var url = './_admin.product.php?a=update&pid='+pid+'&';
	
	var cid = $('catList');
	var bid = $('brand');
	var title = $('title').value;
	var code = $('code').value;
	var barcode = $('barcode').value;
	var sdescription = $('sdescription').value;
	var wholesale = $('wholesalecost').value;
	var item_cost = $('item_cost').value;
	var special_cost = $('special_cost').value;
	var special = $('special').options[$('special').selectedIndex].value;
	var newlist = $('new').options[$('new').selectedIndex].value;
	
	bid = bid.options[bid.selectedIndex].value;
	cid = cid.options[cid.selectedIndex].value;
	
	url+= 'catid='+cid;
	url+= '&brandid='+bid;
	url+= '&title='+title;
	url+= '&code='+code;
	url+= '&barcode='+barcode;
	url+= '&sdescription='+sdescription;
	url+= '&item_cost='+item_cost;
	url+= '&special_cost='+special_cost;
	url+= '&special='+special;
	url+= '&wholesalecost='+wholesale;
	url+= '&new='+newlist;
	
	new ajax (url, {onComplete: updateFields});
	
	function updateFields(request) {
		var txt = request.responseText;
		
		if(txt == 'success')
		{
			var p = $('prod_title');
			p.innerHTML = '<b>'+title.ucWords()+'</b>';
		}
		else
			alert('Could not update product! If this continues, please contact site administrator!');
	}
}

function newProduct(bool) {
	bool = bool==true?true:false;
	var conf = confirm('Create a new product?'+(bool?' You will lose unsaved changes!':''));
	if(conf){
		var url = './_admin.product.php?a=new';
		new ajax (url, {onComplete: result});
	}
	
	function result(response) {
		if(parseInt(response.responseText)>0)
			window.location = './_admin.catalogue.php?pid='+response.responseText;
		else
			alert('Failure creating new product!');
	}
}

function deleteProduct(pid) {
	if(parseInt(pid)>0){
		var conf = confirm('Delete this product? This cannot be reversed!');
		if(conf){
			var url = './_admin.product.php?a=remove&pid='+pid;
			new ajax (url, {onComplete: result});
		}
	}
	
	function result(response) {
		var txt = response.responseText;
		if(txt=='true'||txt=='false'){
			if(txt=='true'){
				alert('Product successfully removed!');
				window.location = './_admin.catalogue.php';
			}else{
				alert('Product could not be deleted!');
			}
		}
	}
}

function doProductSearch() {
	var data = $('searchData');
	var type = $('searchType');
	
	if(data.value.length>0){
		var url = './catalogue.php?a=search&data='+data.value+'&type='+type.options[type.selectedIndex].value;
		window.location = url;
	}else{
		alert('You need to enter a search string!');
	}
}

function doAdminSearch() {
	var data = $('searchData');
	var type = $('searchType');
	
	if(data.value.length>0){
		var url = './_admin.catalogue.php?a=search&data='+data.value+'&type='+type.options[type.selectedIndex].value;
		window.location = url;
	}else{
		alert('You need to enter a search string!');
	}
}

function doAdminUserSearch() {
	var data = $('searchData');
	var type = $('searchType');
	
	if(data.value.length>0){
		var url = './_admin.users.php?a=search&data='+data.value+'&type='+type.options[type.selectedIndex].value;
		window.location = url;
	}else{
		alert('You need to enter a search string!');
	}
}

function removeImage(pid,bool,object){
	if(parseInt(pid)>0){
		var conf = confirm('Do you really want to remove the product '+(bool==true?'thumbnail ':'')+'image?');
		if(conf){
			var url = './_admin.product.php?a=deleteimage&thumb='+bool+'&pid='+pid;
			new ajax (url, {onComplete: result});
			object.disabled=true;
		}
	}
	
	function result(response){
		var def = '../corporate/images/catalogue/no_image'+(bool==true?'_thumbnail':'')+'.jpg';
		var imgCont = document.getElementById((bool==true?'small':'large')+'Image');
		imgCont.src = def;
	}
}

function onLoadCheckSize(){
	var short = $('sdescription');
	var long = $('ldescription');
	
	var targetShort = $('sdesc');
	var targetLong = $('ldesc');
	
	targetShort.innerHTML = short.value.length + ' of 100 characters';
	targetLong.innerHTML = long.value.length + ' of 255 characters';
}

function moveCategory(object,catid){
	if(parseInt(catid)>0){
		var conf = confirm('Move this category?');
		if(conf){
			var target = object.options[object.selectedIndex].value;
			var url = './_admin.ranges.php?a=move&cid='+catid+'&target='+target;
			new ajax(url, {onComplete: result});
		}
	}
	
	function result(response){
		window.location = './_admin.ranges.php';
	}
}

function deleteCategory(catid){
	if(parseInt(catid)>0){
		var conf = confirm('Do you really wish to delete this category? This cannot be reversed!');
		if(conf){
			var url = './_admin.ranges.php?a=deletecat&cid='+catid;
			new ajax(url, {onComplete: result});
		}
	}
	
	function result(response){
		var text = parseInt(response.responseText);
		if(text==1){
			alert('Category could not be removed because there are still products within this category!\nMake sure you assign these products to another category before continuing!');
		}else if(text==2){
			alert('Category could bot be removed because there are child categories attached to this category!\nMake sure you remove or assign these categories to another range or category before continuing!');
		}else if(text==3){
			alert('Could not delete from database!');
		}else if(text==4){
			alert('Category successfully removed!');
			window.location = './_admin.ranges.php';
		}
	}
}

function newCategory(){
	var conf = confirm('Create a new category?');
	if(conf){
		var url = './_admin.ranges.php?a=new';
		new ajax (url, {onComplete: result});
	}
	
	function result(request) {
		if(parseInt(request.responseText)>0)
			window.location = './_admin.ranges.php?a=edit&cid='+parseInt(request.responseText);
	}
}

function newRange(){
	var conf = confirm('Create a new product line?');
	if(conf){
		var url = './_admin.ranges.php?a=newrange';
		new ajax (url, {onComplete: result});
	}
	
	function result(request) {
		if(parseInt(request.responseText)>0)
			window.location = './_admin.ranges.php?a=editrange&rid='+parseInt(request.responseText);
	}
}

function editRange(rid){
	if(parseInt(rid)>0){
		var conf = confirm('Edit range?');
		if(conf){
			var url = './_admin.ranges.php?a=editrange&rid='+parseInt(rid);
			window.location = url;
		}
	}
}

function deleteRange(rid){
	if(parseInt(rid)>0){
		var conf = confirm('Do you really wish to delete this range? This cannot be reversed!');
		if(conf){
			var url = './_admin.ranges.php?a=deleterange&rid='+rid;
			new ajax(url, {onComplete: result});
		}
	}
	
	function result(response){
		var text = parseInt(response.responseText);
		if(text==1){
			alert('Range could not be removed because there are still sub-categories within this category!\nMake sure you assign these sub-categories to another category before continuing!');
		}else if(text==2){
			alert('Could not remove category from database!');
		}else if(text==3){
			alert('Category successfully removed!');
			window.location = './_admin.ranges.php';
		}
	}
}


function editCategory(catid){
	if(parseInt(catid)>0){
		var conf = confirm('Edit category?');
		if(conf){
			var url = './_admin.ranges.php?a=edit&cid='+parseInt(catid);
			window.location = url;
		}
	}
}

function updateAdminOrder(orderid){
	if(parseInt(orderid)>0){
		var status = $('statusList').options[$('statusList').selectedIndex].value;
		var url = './_admin.order.php?a=update&orderid='+parseInt(orderid)+'&status='+status;
		
		new ajax(url, {onComplete: result});
	}
	
	function result(response){
		txt = response.responseText;
		if(txt=='true'){
			alert('Order details updated!');
			window.location = window.location;
		}else{
			alert('Order details could not be updated!');
		}
	}
}

function deleteAdminOrder(orderid){
	if(parseInt(orderid)>0){
		var conf = confirm('Are you sure you want to delete this order?\nThis cannot be reversed!');
		if(conf){
			var url = './_admin.order.php?a=delete&orderid='+orderid;
			
			new ajax(url, {onComplete: result});
		}
	}
	
	function result(response){
		if(response.responseText=='true'){
			alert('Order successfully removed!');
			var url = './_admin.orders.php';
			window.location = url;
		}else{
			alert('Order could not be removed!');
		}
	}
}

function printOrder(orderid,bool){
	if(parseInt(orderid)>0){
		bool = bool=='true'?true:false;
		var conf = confirm('Do you want to '+(bool==true?'display':'print')+' this order?');
		if(conf){
			var url = './print.order.php?orderid='+orderid;
			r = window.open(url,'_blank','height=700,width=700,menubar=no,status=no,toolbar=no,titlebar=no,resizable=no');
			r.focus();
		}
	}
}

function addCartItem(title,pid,size,colour,qty) {
	if(parseInt(pid)>0){
		var s = $(size).options[$(size).selectedIndex].value;
		var c = $(colour).options[$(colour).selectedIndex].value;
		var q = $(qty).value;
		
		if(parseInt(q) < 0) {
			alert('Could not add '+title+' to shopping cart.\nPlease enter a quantity above zero.');
			return;
		}
			
		
		var url = './cart.php?a=addItem&pid='+pid+'&size='+s+'&cid='+c+'&qty='+q;
		new ajax(url, {onComplete: result});
	}
	
	function result(response){
		if(response.responseText == 'true') {
			alert(title+' added!');
			closeFloatingDivs();
			updateCartInfo('cartInfo');
		}else{
			alert('Could not add '+title+' to shopping cart.\nYou can only order a limited amount of items per product.');
		}
	}
}

function deleteCartItem(object, pid, size, cid){
	if(parseInt(pid)>0){
		var conf = confirm('Do you really wish to remove this item from your shopping cart?');
		if(conf){
			var url = './cart.php?a=removeItem&productid='+pid+'&size='+size+'&colour='+cid;
			
			new ajax(url, {onComplete: result});
		}
	}
	
	function result(response){
		window.location = window.location;
	}
}

function doQtyBox(object,productid,qty,size,bool){
	bool = bool==true?true:false;
	
	if(parseInt(productid)>0&&parseInt(qty)>0){
		if(bool){
			object.innerHTML = qty;
			new ajax('./cart.php?a=edit&productid='+productid+'&qty='+qty+'&size='+size, {onComplete: null});
			new ajax('./cart.php?a=getIP&productid='+productid+'&qty='+qty, {onComplete: updateItemPrice});
		}else{
			if(parseInt(object.innerHTML)==parseInt(qty)){
				var input = "<input type='text' id='"+parseInt(productid)+"qty"+size+"' value='"+parseInt(qty)+"' ";
				input += "style='width:20px;border:1px solid #000;font:10px verdana,sans,helvetica;color:#000;' onfocus='this.select()' ";
				input += "onKeyDown='if (event.keyCode ==13) { doQtyBox(this.parentNode,"+productid+",this.value,\""+size+"\",true); } ' ";
				input += "alt=\"Press enter once you are finished modifying this field\" title=\"Press enter once you are finished modifying this field\" />";
				object.innerHTML = input;
			}
		}
	}
	
	function updateItemPrice(response){
		window.location = './cart.php';
	}
}

function saveCart() {
	var conf = confirm('Do you wish to save this cart?\n\nCookies will be used to store your shopping cart information.\nNo personal information will be used.');
	if(conf){
		new ajax('./cart.php?a=save', {onComplete: result});
	}
	
	function result(response){
		if(response.responseText=='true')
			alert('Cart information successfully saved!\n\nYour cart information can be loaded the next time you login.');
		else
			alert('Could not save cart information.');
	}
			
}

function cartCheckout() {
	var conf = confirm('Do you wish to proceed?\nYour order information will be sent to you via email.');
	var inst = $('instructions').value;
	if(conf){
		window.location = './cart.php?a=confirmcheckout&instructions='+inst;
	}
}

function cartPrint() {
	var url = './cart.php?a=print';
	var r = window.open(url,'_blank','height=700,width=700,menubar=no,status=no,toolbar=no,titlebar=no,resizable=no');
	r.focus();
}

function newUser() {
	var url = './_admin.user.php?a=new';
	new ajax(url, {onComplete: result});
	
	function result(request){
		var txt = request.responseText.toString();
		if(txt.indexOf(':') == -1){
			alert('User could not be created!');
		}else{
			var r = txt.split(':',2);
			alert('User successfully created!\nYou can create a password for the user once you begin editing their details.');
			window.location = './_admin.users.php?uid='+parseInt(r[0]);
		}
	}
}

function deleteUser(uid,back) {
	back = back==true||back=='true'?true:false;
	if(parseInt(uid)>0){
		var url = './_admin.user.php?a=delete&uid='+uid;
		new ajax(url, {onComplete: result});
	}
	
	function result(request){
		if(request.responseText=='true'){
			alert('User deleted!');
			if(back)
				window.location = './_admin.users.php';
		}else{
			alert('User could not be deleted!');
		}
	}
}

function updateUser(uid){
	if(parseInt(uid)>0){
		var url = './_admin.user.php?a=update&uid='+uid;
		
		var password = $('password').value;
		var password2 = $('password2').value;
		
		var username = $('username').value;
		var salutation = $('salutation').value;
		var firstname = $('firstname').value;
		var lastname = $('lastname').value;
		var telephone = $('telephone').value;
		var fax = $('fax').value;
		var mobile = $('mobile').value;	
		var admin = $('admin').options[$('admin').selectedIndex].value;
		var grower = $('grower').options[$('grower').selectedIndex].value;
		var discount = parseFloat($('discount').value);
		
		var billingstreet = $('billingstreet').value;
		var billingsuburb = $('billingsuburb').value;
		var billingpcode = $('billingpcode').value;
		var billingstate = $('billingstate').value;
		var billingcountry = $('billingcountry').value;
		
		var shippingstreet = $('shippingstreet').value;
		var shippingsuburb = $('shippingsuburb').value;
		var shippingpcode = $('shippingpcode').value;
		var shippingstate = $('shippingstate').value;
		var shippingcountry = $('shippingcountry').value;
		
		discount = discount>1?1:discount;
		discount = discount<0?0:discount;
		
		url += '&username='+username;
		url += '&salutation='+salutation;
		url += '&firstname='+firstname;
		url += '&lastname='+lastname;
		url += '&telephone='+telephone;
		url += '&fax='+fax;
		url += '&mobile='+mobile;
		url += '&admin='+admin;
		url += '&grower'+grower;
		url += '&discount='+discount;
		
		url += '&billingstreet='+billingstreet;
		url += '&billingsuburb='+billingsuburb;
		url += '&billingpcode='+billingpcode;
		url += '&billingstate='+billingstate;
		url += '&billingcountry='+billingcountry;
		
		url += '&shippingstreet='+shippingstreet;
		url += '&shippingsuburb='+shippingsuburb;
		url += '&shippingpcode='+shippingpcode;
		url += '&shippingstate='+shippingstate;
		url += '&shippingcountry='+shippingcountry;
		
		if(password.length>5&&password2.length>5&&password==password2){
			url += '&password='+password;
			url += '&password2='+password2;
		}
		
		new ajax(url, {onComplete: result});
	}
	
	function result(request){
		if(request.responseText == 'true'){
			alert('User updated!');
		}else if(request.responseText == '1'){
			alert('User updated!\nYou need to log in again because of your email or password change');
			window.location = './_login.php';
		}else{
      // alert('User was not updated!\nCheck that you have entered correct data into the input fields,'
      //    +'\nand entered email addresses correctly eg. user@some.net');
			alert(request.responseText);
		}
	}
}

function updateProfile(){
	var url = './_profile.php?a=update';
	
	var password = $('password').value;
	var password2 = $('password2').value;
	
	var username = $('username').value;
	var salutation = $('salutation').value;
	var firstname = $('firstname').value;
	var lastname = $('lastname').value;
	var telephone = $('telephone').value;
	var fax = $('fax').value;
	var mobile = $('mobile').value;
	
	var billingstreet = $('billingstreet').value;
	var billingsuburb = $('billingsuburb').value;
	var billingpcode = $('billingpcode').value;
	var billingstate = $('billingstate').value;
	var billingcountry = $('billingcountry').value;
	
	var shippingstreet = $('shippingstreet').value;
	var shippingsuburb = $('shippingsuburb').value;
	var shippingpcode = $('shippingpcode').value;
	var shippingstate = $('shippingstate').value;
	var shippingcountry = $('shippingcountry').value;
	
	url += '&username='+username;
	url += '&salutation='+salutation;
	url += '&firstname='+firstname;
	url += '&lastname='+lastname;
	url += '&telephone='+telephone;
	url += '&fax='+fax;
	url += '&mobile='+mobile;
	
	url += '&billingstreet='+billingstreet;
	url += '&billingsuburb='+billingsuburb;
	url += '&billingpcode='+billingpcode;
	url += '&billingstate='+billingstate;
	url += '&billingcountry='+billingcountry;
	
	url += '&shippingstreet='+shippingstreet;
	url += '&shippingsuburb='+shippingsuburb;
	url += '&shippingpcode='+shippingpcode;
	url += '&shippingstate='+shippingstate;
	url += '&shippingcountry='+shippingcountry;
	
	if(password.length>5&&password2.length>5&&password==password2){
		url += '&password='+password;
		url += '&password2='+password2;
	}
	
	new ajax(url, {onComplete: result});
	
	function result(request){
		if(request.responseText == 'true'){
			alert('Profile update!');
		}else if(request.responseText == '1'){
			alert('Profile updated!\nYou need to log in again because of your email and/or password change');
			window.location = './_login.php';
		}else{
			alert('Your profile was not updated!\nCheck that you have entered correct data into the input fields,'
				  +'\nand entered email addresses correctly eg. user@some.net');
		}
	}
}

function generatePass(uid){
	if(parseInt(uid)>0){
		var url = './_admin.user.php?a=genpass&uid='+uid;
		var conf = confirm('Are you sure you want to generate a new password?');
		if(conf)
			new ajax(url, {onComplete: result})
	}
	
	function result(request){
		var txt = request.responseText.toString();
		
		if(txt=='1'){
			alert('Password successfully generated.'
				  +'\nThe new password has been emailed to the users email address.');
		}else if(txt=='2'){
			alert('Your password has been successfully changed.'
				  +'\nYour new password has been emailed to you.\nYou will now be logged out.');
			window.location = './_login.php';
		}else if(txt=='false'){
			alert('A new password could not be generated!');
		}
			
	}
}

function sameBilling() {
	var arrShip = Array($('streetshipping'),$('suburbshipping'),$('pcodeshipping'),$('stateshipping'),$('countryshipping'));
	var arrBill = Array($('street'),$('suburb'),$('pcode'),$('state'),$('country'));
	
	resetValues(arrShip,arrBill);
	
	function resetValues(resetArr,oldArr) {
		for(var i=0;i<resetArr.length;i++){
			resetArr[i].value = oldArr[i].value;
			disableField(resetArr[i]);
		}
		
	}
	
	function disableField(field) {
		if(field.disabled == true)
			field.disabled = false;
		else
			field.disabled = true;
	}
}

function emailOnly(e) {
	var k;
	document.all?k=e.keyCode:k=e.which;
	return ((k>47 && k<58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k==46 || k==45 || k==95 || k==64 || k==0);
}

function nameOnly(e) {
	var k;
	document.all?k=e.keyCode:k=e.which;
	return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k==45 || k==46 || k==0);
}

function phoneNumber(e) {
	var k;
	document.all?k=e.keyCode:k=e.which;
	return ((k>47 && k<58)||k==8||k==43 || k==0);
}

function numberOnly(e) {
	var k;
	document.all?k=e.keyCode:k=e.which;
	return ((k>47 && k<58)||k==8 || k==0);
}

function alphaOnly(e) {
	var k;
	document.all?k=e.keyCode:k=e.which;
	return ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k==0);
}

function alphaNumOnly(e) {
	var k;
	document.all?k=e.keyCode:k=e.which;
	return ((k>47 && k<58) || (k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k==0 || k==32);
}

function checkSameShipping() {
	var arrShip = Array($('streetshipping'),$('suburbshipping'),$('pcodeshipping'),$('stateshipping'),$('countryshipping'));
	var arrBill = Array($('street'),$('suburb'),$('pcode'),$('state'),$('country'));
	
	if($('sameshipping').checked==true){
		for(var i=0;i<arrBill.length;i++){
			arrShip[i].value=arrBill[i].value;
		}
	}
}

function newBrand() {
	var url = './_admin.brands.php?a=new';
	new ajax(url, {onComplete: result});
	
	function result(request){
		var txt = request.responseText;
		
		if(txt.length>0)
			window.location = './_admin.brands.php?bid='+txt;
		else
			alert('Could not create a new brand!');
	}
}

function deleteBrand(bid) {
	if(parseInt(bid)>0){
		var url = './_admin.brands.php?a=delete&bid='+bid;
		new ajax(url, {onComplete: result});
	}
	
	function result(request){
		if(request.responseText=='true'){
			alert('Brand deleted!');
			if(back)
				window.location = './_admin.brands.php';
		}else{
			alert('Brand could not be deleted!');
		}
	}
}

function stockControl(obj,pid) {
	var size = obj.parentNode.parentNode.id;
	var ele = document.getElementById(size);
	var img = document.getElementById(size+'.tick');
	var qty = document.getElementById(size+'.quantity');
	var mng = document.getElementById(size+'.manage');
	
	if(parseInt(pid)>0){
		if(obj.checked==true){
			var url = './_admin.product.php?a=addsize&size='+size+'&pid='+pid;
			new ajax(url, {onComplete: activateStock});
		}else{
			var url = './_admin.product.php?a=removesize&size='+size+'&pid='+pid;
			new ajax(url, {onComplete: deactivateStock});
		}
	}
	
	function activateStock(request){
		var txt = request.responseText;
		if(txt=='true'){
			ele.style.fontStyle = 'normal';
			ele.style.backgroundColor = '#fff';
			qty.disabled = false;
			mng.disabled = false;
			mng.style.visibility = 'visible';
			mng.style.width = '100px';
		}else{
			obj.checked=false;
			alert('Could not add stock item');
		}
	}
	
	function deactivateStock(request){
		var txt = request.responseText;
		if(txt=='true'){
			ele.style.fontStyle = 'italic';
			ele.style.backgroundColor = '#ccc';
			img.style.display = 'none';
			qty.disabled = true;
			qty.value = '';
			mng.disabled = true;
			mng.style.visibility = 'hidden';
		}else{
			obj.checked=true;
			alert('Could not remove stock item');
		}
	}
}

function displayStockTick(obj) {
	var size = obj.parentNode.parentNode.id;
	var img = document.getElementById(size+'.tick');
	img.style.display = 'block';
}

function updateStockItem(obj,pid)  {
	var size = obj.parentNode.parentNode.id;
	var qty = document.getElementById(size+'.quantity');
	var img = document.getElementById(size+'.tick');
	if(parseInt(pid)>0){
		var url = './_admin.product.php?a=updatesize&size='+size+'&pid='+pid+'&qty='+qty.value;
		new ajax(url, {onComplete: result});
	}
	
	function result(request){
		var txt = request.responseText;
		if(txt=='true'){
			img.style.display = 'none';
			alert('Stock quantity updated!');
		}else
			alert('Could not update stock item');
	}
}

function requestGrower(obj,uid) {
	if(parseInt(uid)>0){
		var conf = confirm('Request grower status?');
		if(conf){
			var url = './_profile.php?a=reqgrower';
			new ajax (url, {onComplete: result});
		}
	}
	
	function result(request){
		var txt = request.responseText;	
		if(txt=='false')
			alert('Could not process request.');
		else
			obj.parentNode.innerHTML = 'Your request for grower status is pending. Please check back again soon.';
	}
}

function newColour() {
	alert('test');
	var url = './_admin.colours.php?a=new';
	new ajax(url, {onComplete: result});
	
	function result(request){
		var txt = request.responseText;
		
		if(txt.length > 0)
			window.location = './_admin.colours.php?cid='+txt;
		else
			alert('Could not create a new colour!');
	}
}

function deleteColour(cid) {
	if(parseInt(cid) > 0){
		var url = './_admin.colours.php?a=delete&cid='+cid;
		new ajax(url, {onComplete: result});
	}
	
	function result(request){
		alert(request.responseText);
		if(request.responseText == 'true'){
			alert('Colour deleted!');
			window.location = './_admin.colours.php';
		}else
			alert('Colour could not be deleted!');
	}
}

function removeSwatchImage(object, cid) {
	var conf = confirm('Do you really want to remove the swatch image?');
	if(conf){
		var url = './_admin.colours.php?a=deleteImage&cid='+cid;
		new ajax(url, {onComplete: response});
	}
	
	function response(result){
		if(result.reponseText == ''){
			alert('Could not remove swatch image.');
			return;
		}
		object.disabled = true;
		var def = './images/swatches/swatch_test.jpg';
		var img = $('swatchImage');
		img.src = def;
	}
}

function colourManagementWindow(pid, size) {
	if(parseInt(pid) > 0){
		var url = './_admin.product.colours.php?pid='+pid+'&size='+size;
		window.open(url, '_blank', "height=350,width=600,location=no,scrollbars=yes,resizable=no,toolbar=no");
	}
}

function addColourItem(pid, size) {
	var resultText, table, row, c1, c2, c3, c4, c5;
	if(parseInt(pid)>0){
		var url = './_admin.product.colours.php?a=addColour&pid='+pid+'&size='+size;
		new ajax(url, {onComplete: response});
	}
	
	function response(result){
		if(result.responseText.indexOf(':') == -1){
			alert('error!');
		}else{
			var res = result.responseText.split(':');
			
			table = $('colourTable');
			row = table.insertRow(table.rows.length);
			
			c1 = row.insertCell(0);
			c1.innerHTML = table.rows.length-2;
			
			c2 = row.insertCell(1);
			colourSelectMenu(c2, res[0]);
			
			c3 = row.insertCell(2);
			c3.innerHTML = '<img src="images/tick.gif" style="display:block;" id="'+res[0]+'.tick" onclick="updateColourItem(this, '+res[0]+');" />';
			
			c4 = row.insertCell(3);
			c4.innerHTML = '<img src="images/delete.gif" id="'+res[0]+'.remove" onclick="removeColourItem(this, '+res[0]+');" />';
			
			c5 = row.insertCell(4);
			c5.innerHTML = '<img src="'+res[1]+'" />';
		}
	}
}

function updateColourItem(object, lid) {
	if(parseInt(lid) > 0){
		var url = './_admin.product.colours.php?a=updateColour&lid='+lid+'&cid='+$(lid+'.menu').options[$(lid+'.menu').selectedIndex].value;
		new ajax(url, {onComplete: response});
	}
	
	function response(result){
		if(result.responseText == 'true'){
			updateTick(lid);
			var cell = $(lid+'.menu').parentNode;
			cell.innerHTML = $(lid+'.menu').options[$(lid+'.menu').selectedIndex].text;
		}
	}
}

function colourSelectMenu(object, lid) {
	if(parseInt(lid) > 0){
		var url = './_admin.product.colours.php?a=colourMenu&lid='+lid;
		new ajax(url, {onComplete: response});
	}
	
	function response(result){
		object.innerHTML = result.responseText;
	}
}

function updateTick(lid) {
	var tick = $(lid+'.tick');
	tick.style.display = 'none';
}

function removeColourItem(object, lid) {
	if(parseInt(lid) > 0){
		var url = './_admin.product.colours.php?a=removeColour&lid='+lid;
		new ajax(url, {onComplete: response});
	}
	
	function response(result){
		if(result.responseText == 'true'){
			var table = object.parentNode.parentNode.parentNode;
			var row = object.parentNode.parentNode;
			table.removeChild(row);
		}
	}
}

function updateColourList(pid, obj, target) {
	if(parseInt(pid) )
	{
		var size = obj.options[obj.selectedIndex].value;
		var url = './catalogue.php?a=getColourList&pid='+pid+'&size='+size+'&id='+target;
		new ajax(url, {onComplete: replaceList});
		
		updateSwatches(pid, obj);
	}
	
	function replaceList(result) {
		var html = result.responseText;
		$(target).innerHTML = html;
	}
	
	function response(result) {
		var txt = result.responseText;
		$(target).innerHTML = txt;
	}
}

function updateSwatches(pid, obj)
{
	var target = 'sizeSwatches.'+pid;
	if(parseInt(pid) )
	{
		var size = obj.options[obj.selectedIndex].value;
		var url = './catalogue.php?a=getSwatches&p='+pid+'&size='+size;
		new ajax(url, {onComplete: response});
	}
	
	function response(result)
	{
		var txt = result.responseText;
		$(target).innerHTML = txt;
	}
}