var errorboxid = -1;

			function toggleJavascriptError(msg, id, section)
			{
					/*
					Clear other messages
					*/
					var flag = true;
					var i=0;
					while(flag)
					{
						if(document.getElementById('msg'+i))
							document.getElementById('msg'+i).style.display = 'none';
						else
							flag = false;
							
						i++;
					}
					
					if(document.getElementById(id+'box'))
						document.getElementById(id+'box').style.border = '1px solid #ca5d5d';
					
					if(document.getElementById('msg'+section))
					{
						document.getElementById('msg'+section).style.display = 'block';
						document.getElementById('msg'+section).innerHTML = msg;
					}
					
					if(section != null)
						tabs.showSection(section);
					
					/*
					This seems to mess up the tabbing content alignment,
					so lets just leave it out.
					*/
					//document.getElementById(id).focus();

					if(errorboxid != -1 && errorboxid != id)
					{
						if(document.getElementById(errorboxid+'box'))
							document.getElementById(errorboxid+'box').style.border = '1px solid #cccccc';
					}
						
					errorboxid = id;
					
					return false;
			}
			
			function isEmpty(str)
			{
				return (str == null) || (str.length == 0);
			}
			
			function isEmail(str)
			{
				if(isEmpty(str)) return false;
			
				var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
			
				return re.test(str);
			}

			function isAlphaNumeric(str)
			{
				var re = /[^a-zA-Z0-9]/g
				
				if (re.test(str) == true)
				{
					return false;
				}
				else
				{
					return true;
				}
			}
			
			function isNumeric(str)
			{
				var re = /[^\. 0-9]/g
				
				if (re.test(str)) return false;
				
				return true;
			}

			function isAlpha(str)
			{
				var re = /[^a-zA-Z]/g
				
				if (re.test(str)) return false;
			
				return true;
			}
			
			function isImage(str)
			{
				if (!/(\.(jpg|jpeg|png))$/i.test(str)) return false;
				
				return true;
			}

			 function filterString(str) {
				  re = /\$|\_|,|@|#|~|`|\%|\*|\^|\&|\(|\)|\+|\=|\[|\-|\]|\[|\}|\{|\;|\:|\'|\"|\<|\>|\s|\?|\||\\|\!|\$|\./g;
				  // remove special characters like "$" and "," etc...
				  return str.replace(re, "");
			 }

			function suggestURL()
			{
				var title = document.aeform.title.value;
				var url = document.aeform.url_title.value;

				if(title != "" && url == "" && title != "Home")
				{
					string = filterString(title);
					document.aeform.url_title.value = string.toLowerCase();
				}
			}

			function validatePage()
			{
				var status = true;
				var msg = '';
				var id = '';
				var section = 0;

				var title = document.getElementById('title').value;
				var url_title = document.getElementById('url_title').value;

				if(isEmpty(title))
				{
					msg = 'Please enter in a title for this page.';
					id = 'title';
					section = 1;

					status = false;
				}
				else if(isEmpty(url_title))
				{
					msg = 'Please enter in a URL for this page.';
					id = 'url_title';
					section = 1;

					status = false;
				}
				else if(isAlphaNumeric(url_title) == false)
				{
					msg = 'The page URL must contain only alpha numerical data.';
					id = 'url_title';
					section = 1;

					status = false;
				}


				if(status == false)
				{			
					var temp = toggleJavascriptError(msg, id, section);
					
					return false;
				}
				else
				{
					return true;
				}
			}
			
			function validatePanel()
			{
				var title = document.getElementById('title').value;
				
				var status = true;
				var msg = '';
				var id = '';
				var section = 0;

				if(isEmpty(title))
				{
					msg = 'Please enter in a title for this panel.';
					id = 'title';
					section = 1;

					status = false;
				}
				else if(!isAlphaNumeric(title))
				{
					msg = 'The panel title must contain only alpha numerical data.';
					id = 'title';
					section = 1;

					status = false;
				}

				if(status == false)
				{			
					var temp = toggleJavascriptError(msg, id, section);
					
					return false;
				}
				else
				{
					return true;
				}
			}
			
			function validateDVCMSUser(form)
			{
				var username = form.username.value;
				var password = form.password.value;
				var fullname = form.name.value;
				var email = form.email.value;

				var errors = "Please correct the following problems:\n";
				var status = true;

				var anyPrivileges = false;
				var pCount = 0;
				
				var ins = document.getElementsByTagName('input')
				var i;
				for(i=0;i<ins.length;i++)
				{
					if(ins[i].type=="checkbox")
					{
						if(ins[i].checked == true)
							anyPrivileges = true;
						
						pCount++;
					}
				}
				
				if(pCount == 0) anyPrivileges = true;

				if(isEmpty(username))
				{
					errors += "  - You must enter in a username.\n";
					status = false;
				}
				else
				{
					if(!isAlpha(username))
					{
						errors += "  - Username must contain only alphabetical characters.\n";
						status = false;
					}
				}

				if(isEmpty(password))
				{
					errors += "  - You must enter in a password.\n";
					status = false;
				}

				if(isEmpty(fullname))
				{
					errors += "  - You must enter in a name.\n";
					status = false;
				}
				
				if(isEmpty(email))
				{
					errors += "  - You must enter in a email address.\n";
					status = false;
				}
				else
				{
					if(!isEmail(email))
					{
						errors += "  - The email address must be a valid email.\n";
						status = false;
					}
				}
				
				if(!anyPrivileges)
				{
					errors += "  - You must select at least one privilege for the user.\n";
					status = false;
				}

				if(status == false)
				{
					alert(errors);
				}

				return status;
			}
			
			function validateCustomer(form)
			{
				var username = form.username.value;
				var password = form.password.value;
				if(form.company)
					var company = form.company.value;
				else
					var company = "Guest"

				if(form.ABN)
					var ABN = form.ABN.value;
				else
					var ABN = "";
				var email = form.email.value;
				var phone = form.phone.value;
				var firstname = form.firstname.value;
				var lastname = form.lastname.value;
				var address = form.address.value;
				var suburb = form.suburb.value;
				var city = form.city.value;
				var postcode = form.postcode.value;
				var country = form.country.value;
				var state_specific = form.state_specific.value;
				
				var errors = "Please correct the following problems:\n";
				var status = true;

				if(isEmpty(username))
				{
					errors += "  - You must enter in a username.\n";
					status = false;
				}
				else
				{
					if(!isAlphaNumeric(username))
					{
						errors += "  - Username must contain only alphabetical characters.\n";
						status = false;
					}
				}

				if(isEmpty(password))
				{
					errors += "  - You must enter in a password.\n";
					status = false;
				}
				

				if(isEmpty(firstname))
				{
					errors += "  - You must enter in a first name.\n";
					status = false;
				}

				if(isEmpty(lastname))
				{
					errors += "  - You must enter in a last name.\n";
					status = false;
				}

				if(isEmpty(company))
				{
					errors += "  - You must enter in a company name.\n";
					status = false;
				}

				if(!isEmpty(ABN) && !isNumeric(ABN))
				{
					errors += "  - The ABN number must contain only numeric values.\n";
					status = false;
				}
				
				if(isEmpty(phone))
				{
					errors += "  - You must enter in a phone number.\n";
					status = false;
				}
				else
				{
					if(!isNumeric(phone))
					{
						errors += "  - The phone number must contain only numeric values.\n";
						status = false;
					}
				}

				if(isEmpty(email))
				{
					errors += "  - You must enter in the email address.\n";
					status = false;
				}
				else
				{
					if(!isEmail(email))
					{
						errors += "  - The email address must be a valid email format.\n";
						status = false;
					}
				}
				
				if(isEmpty(address))
				{
					errors += "  - You must enter in a street address.\n";
					status = false;
				}

				if(isEmpty(suburb))
				{
					errors += "  - You must enter in a suburb.\n";
					status = false;
				}

				if(isEmpty(postcode))
				{
					errors += "  - You must enter in a postcode.\n";
					status = false;
				}
				else
				{
					if(!isNumeric(postcode))
					{
						errors += "  - The postcode must contain only numeric values.\n";
						status = false;
					}
				}
				
				if(isEmpty(city))
				{
					errors += "  - You must enter in a city.\n";
					status = false;
				}

				if(country != "Australia" && isEmpty(state_specific))
				{
					errors += "  - You must enter in a state.\n";
					status = false;
				}

				if(status == false)
				{
					alert(errors);
				}

				return status;
			}
			
			function validateCategory(form)
			{
				name = form.category.value;
				image = form.image.value;
				
				if(isEmpty(name))
				{
					alert("Please enter in a category name.");
					
					return false;
				}
				else if(!isEmpty(image) && !isImage(image))
				{
					alert("The image must be either a JPG or PNG file.\n");
					
					return false;
				}
				
				return true;
			
			}

			function returnSelectedMultiple(field)
			{

				len = field.length
				i = 0
				chosen = new Array();
				j = 0;

				for (i = 0; i < len; i++)
				{
					if (field[i].selected)
					{
						chosen[j] = field[i].value;
						j++;
					}
				}

				return chosen
			}

			// Removes leading whitespaces
			function LTrim(value)
			{
				var re = /\s*((\S+\s*)*)/;
				return value.replace(re, "$1");
			}

			// Removes ending whitespaces
			function RTrim(value)
			{
				var re = /((\s*\S+)*)\s*/;
				return value.replace(re, "$1");
			}

			function trim(value)
			{
				return LTrim(RTrim(value));
			}
			
			function capWords(str)
			{ 
			 	var words = str.split(" ");

				for (var i=0 ; i < words.length ; i++)
				{ 
					words[i] = words[i].toLowerCase();
					var testwd = words[i]; 
					var firLet = testwd.substr(0,1); 
					var rest = testwd.substr(1, testwd.length -1) 
					words[i] = firLet.toUpperCase() + rest 
			   	} 
			   
				return words.join(" "); 
			} 

			function returnCheckboxValues(field)
			{

				len = field.length
				i = 0
				chosen = new Array();
				j = 0;

				for (i = 0; i < len; i++)
				{
					if (field[i].checked)
					{
						chosen[j] = field[i].value;
						j++;
					}
				}

				return chosen
			} 

			function validateProduct()
			{
				var status = true;
				var msg = '';
				var id = '';
				var section = 0;

				var code = document.getElementById('code').value;
				var name = document.getElementById('name').value;
				
				if(document.getElementById('specialPrice'))
				{
					var specialPrice = document.getElementById('specialPrice').value;
					specialPrice = specialPrice.replace("$","");
				}
				
				var maxQty = document.getElementById('maxQty').value;
				var weight = document.getElementById('weight').value;
				weight = weight.replace("$","");
				var image = document.getElementById('image').value;
				var colourlist = "";
				if(document.getElementById('colourlist'))
					colourlist = document.getElementById('colourlist');

				var sizes = document.getElementById('sizes');
				var categories = document.getElementById('categories');

				catArr = returnSelectedMultiple(categories);

				if(isEmpty(code))
				{
					msg = 'Please enter in a product code.';
					id = 'code';
					section = 0;

					status = false;
				}
				else if(isEmpty(name))
				{
					msg = 'Please enter in a name for this product.';
					id = 'name';
					section = 0;

					status = false;
				}
				else if(!isEmpty(specialPrice) && !isNumeric(specialPrice))
				{
					msg = 'The special price must contain only numerical data.';
					id = 'specialPrice';
					section = 0;

					status = false;
				}
				else if(!isEmpty(weight) && !isNumeric(weight))
				{
					msg = 'The weight must contain only numerical data.';
					id = 'weight';
					section = 0;

					status = false;
				}
				else if(!isEmpty(maxQty) && !isNumeric(maxQty))
				{
					msg = 'Max Quantity must contain only numerical data.';
					id = 'maxQty';
					section = 0;

					status = false;
				}
				else if(!isEmpty(image) && !isImage(image))
				{
					msg = 'The image must be either a JPG or PNG file.';
					id = 'name';
					section = 0;

					status = false;
				}
				else if(catArr.length == 0)
				{
					msg = 'You must select at least one category for this product.';
					id = 'categories';
					section = 1;

					status = false;
				}
				else if(colourlist.length == 0 || colourlist == "")
				{
					msg = 'You must select or enter at least one fabric/colour for this product.';
					id = 'colours';
					section = 2;

					status = false;
				}
				else if(sizeArr.length == 0)
				{
					msg = 'You must select at least one size for this product.';
					id = 'size';
					section = 3;

					status = false;
				}
		
				if(status == false)
				{
					var temp = toggleJavascriptError(msg, id, section);
					
					return false;
				}
				else
				{
					return true;
				}
			}

			function checkExternalLink()
			{
				var elink = document.addeditbanner.externalurl.value;
				var ilink = document.addeditbanner.internalurl.value;

				if(ilink == "")
				{
					document.addeditbanner.externalurl.disabled = false;
				}
				else
				{
					document.addeditbanner.externalurl.disabled = true;
					document.addeditbanner.externalurl.value = "http://";
				}
			}
			
			function checkInternalExternalLinks()
			{
				var nolink = document.addeditbanner.nolink.checked;
				
				if(nolink)
				{
					document.addeditbanner.internalurl.value = "";
					document.addeditbanner.externalurl.value = "http://";
					
					document.addeditbanner.externalurl.disabled = true;
					document.addeditbanner.internalurl.disabled = true;
				}
				else
				{
					document.addeditbanner.externalurl.disabled = false;
					document.addeditbanner.internalurl.disabled = false;				
				}
			}
			
			function validateBanner()
			{
				var image = document.addeditbanner.image.value;
				
				if(isEmpty(image))
				{
					alert("You must select an image for this banner.\n");

					return false;				
				}
				else
				{
					if (isImage(image))
					{
						return true;
					}
					else
					{
						alert("The image must be either a JPG or PNG file.\nPlease fix this problem before adding the banner.");

						return false;
					}
				}
			}

	function confirmDel()
	{
		return confirm("Are you sure you want to delete this item?");
	}

	function confirmUserDelete()
	{
		return confirm("Are you sure you want to delete this user?");
	}

	function confirmSubAccountDelete()
	{
		return confirm("Are you sure you want to delete this Sub-Account?\nThis can not be undone.");
	}

	function confirmCustomerDelete()
	{
		return confirm("Are you sure you want to delete this Customer?\nAll employee accounts belonging to this customer will also be deleted.");
	}

	function confirmCancel()
	{
		return confirm("Are you sure you want to cancel? All changes will be lost.");
	}

	function alertNoDel()
	{
		alert("You can not delete this item.");
		return false;
	}

	function alertNoEdit()
	{
		alert("You can not edit this item.");
		return false;
	}
	
	function selectAllPrivileges(field)
	{
		var flag = true;
		
		if(field[0].checked != true)
			flag = false;
		
		for (i=0; i < field.length; i++)
		{
			field[i].checked = flag;
		}
	}
	

	function autogenerateCustomerPwd()
	{	
		var keylist="abcdefghijklmnopqrstuvwxyz123456789"
		var temp=''

		for (i=0;i<7;i++)
			temp+=keylist.charAt(Math.floor(Math.random()*keylist.length))
		

		document.custform.password.value = temp;
	}
	
	function toggleSpecificState(specCountry)
	{
		country = document.custform.country.value;
		
		if(specCountry != "")
			country = specCountry;

		if(country == "Australia")
		{
			document.getElementById('state_specific').style.display = 'none';
			document.getElementById('state').style.display = 'block';
		}
		else
		{
			document.getElementById('state_specific').style.display = 'block';
			document.getElementById('state').style.display = 'none';
		}
	}
	
	function selectProduct(prodid)
	{
		var productid = document.getElementById(prodid).value;

		if(productid == "true")
		{
			document.getElementById(prodid).value = "false";
			document.getElementById('tick'+prodid).style.display = 'none';
			document.getElementById('td1'+prodid).style.background = 'transparent';
			document.getElementById('td2'+prodid).style.background = 'transparent';
			document.getElementById('td3'+prodid).style.background = 'transparent';
		}
		else
		{
			document.getElementById(prodid).value = "true";
			document.getElementById('tick'+prodid).style.display = 'block';
			document.getElementById('td1'+prodid).style.background = '#e6edf5';
			document.getElementById('td2'+prodid).style.background = '#e6edf5';
			document.getElementById('td3'+prodid).style.background = '#e6edf5';
		}

	}
	
	function textCounter(field,cntfield,maxlimit)
	{
		if (field.value.length > maxlimit)
			field.value = field.value.substring(0, maxlimit);
		else
			cntfield.value = maxlimit - field.value.length;
	}