var bAllowMultipleQty;

//Preload Images
document.onload = preloadIms();

function preloadIms() {
	var i = new Image();
	i.src = "images/plus.gif";
	i = new Image();
	i.src = "images/minus.gif";
}

function confirmDelete(sItem)
{
	var answer = confirm('Are you sure you want to delete this ' + sItem + '?')
	return answer;
}

function submitForm(action)
{

	//Stops going to the search form (which is the first form on most pages now, so instead goes to 2nd form)
	if(document.forms[0].formaction) {
		document.forms[0].formaction.value = action;
		document.forms[0].submit();
	} else {
		document.forms[1].formaction.value = action;
		document.forms[1].submit();
	}
}

function setEditItemIndex(i)
{
	//Stops going to the search form (which is the first form on most pages now, so instead goes to 2nd form)
	if(document.forms[0].formaction) {
		document.forms[0].editItemIndex.value = i;
	} else {
		document.forms[1].editItemIndex.value = i;
	}
}

function setId(i)
{
	if(document.forms[0].formaction) {
		document.forms[0].id.value = i;
	} else {
		document.forms[1].id.value = i;
	}
}

function setFormValue(sId, sVal)
{
	if(document.forms[0].formaction) {
		document.forms[0][sId].value = sVal;
	} else {
		document.forms[1][sId].value = sVal;
	}
}

function clearText(sId)
{
	var oText =	document.getElementById(sId);
	if(oText)
	{
		oText.value = "";
		oText.focus();
	}	
}

function popImage(productCode, w, h)
{
	var win = window.open("","","width=" + w + ",height=" + h + ",scrollbars=1,resizable=1,status=1");
	win.document.open();
	win.document.writeln('<html><head><title>' + productCode + '</title></head><body style="margin:0px">');
	win.document.writeln('<div style = "text-align: center; width:100%"><img src="productimages/' + productCode + '.jpg" /></div>');
	win.document.writeln('<br /><div style="bottom:10px;width:100%;text-align:center">');
	win.document.writeln("<input class='basketbutton' type='button' value='Close Window' onclick='window.close()'>");
	win.document.writeln('</div><br />');
	win.document.writeln('</body></html>');
	win.document.close();
}


function popWindow(w, h, url)
{
	var win = window.open(url, "", "width=" + w + ",height=" + h + ",scrollbars=1,resizable=1,status=1");
}


function selectProduct(sNameField, sName, sIdField, sId, sProductLink)
{
	window.opener.document.getElementById(sNameField).innerText = sName;
	window.opener.document.getElementById(sIdField).value = sId;
	window.opener.document.getElementById(sProductLink).innerText = "Change...";
}


function getSelectBoxValue(sSelectId)
{
	var sel = document.getElementById(sSelectId);
	return sel.options[sel.selectedIndex].value;
}


function formDest(dest)
{
	document.forms[0].action = dest;
}

function HideRows(iLowestRow, iHighestRow, sRowPrefix)
{
	var cmd;
	var display;

	if (document.defForm.creditaccount.checked)
	{
		display = 'block';
	}
	else
	{
		display = 'none';
	}

	for (i = 1;i<=iHighestRow ;i++ )
	{
		cmd = sRowPrefix + i + ".style.display = '" + display + "';";
		eval(cmd);
	}
}

function ChangeClass(o,c) {
	if (typeof o == "object") {
		o.className = c;	
	}
}

function popPOS(productCode, w, h) {
	w < 430 ? w = 430 : "";
	h > 500 ? h = 500 : "";
	var win = window.open("emailpos.asp?id=" + productCode + "&w=" + w + "&h=" + h,"","width=" + (w+20) + ",height=" + (h + 150) + ",scrollbars=1,toolbar=1,resizable=1");
}

function InitPos(bShow) {
	bShow ? document.getElementById("Diamond").style.display = "block" : "";
}

function LoadPosHTML(iMode) {
	document.getElementById("PosHTML").value = document.getElementById("PosContent").innerHTML;
	document.getElementById("HTMLFrm").a.value = iMode;
}


function CheckForm(f) {	
var sType, sPattern, iID
	for(i=0;i<f.elements.length;i++) {
		if(f.elements[i].name == "ID") {
			iID = f.elements[i].value
			sType = iID + "_Type"

			switch(document.getElementById(sType).value) {
			
				case "Time":
					sPattern = /^\d{2}\:\d{2}\:\d{2}$/gi;
					if (!sPattern.test(document.getElementById(iID + "_Value").value)) {
						alert("You have not entered the " + document.getElementById(iID + "_Name").value + " Time field correctly, the correct format is HH:MM:SS")
						return false;
					}
					break;
					
				case "Char":
					sPattern = /[^a-z0-9]/gi;
					if (sPattern.test(document.getElementById(iID + "_Value").value)) {
						alert("You have not entered the " + document.getElementById(iID + "_Name").value + " field value in the correct format. Please use only numbers and letters")
						return false;
					}				
					break;
					
				case "Percent":
					sPattern = /^[0-9]+(\.)?[0-9]+$/gi;
					if (!sPattern.test(document.getElementById(iID + "_Value").value)) {
						alert("You have not entered the " + document.getElementById(iID + "_Name").value + " percentage field value in the correct format. Please use only numbers and a '.'")
						return false;
					}								
					break;
			}
		}
	}
}

function CheckNewPassFrm() {
	var e = document.getElementById("un")
	if (e.value == "") {
		alert("You have not entered an email address.");
	} else {
		var spattern = /^[\w|.|\-]*\@[\w|.|\-]*\.[\w|.]+$/gi;
		if(!spattern.test(e.value)) {
			alert("You have not entered a valid email address.")
		} else {
			document.getElementById('SndNewPW').submit()	
		}
	}
}

function CheckChgeFrm() {
	var pw1 = document.getElementById("pw1");
	var pw2 = document.getElementById("pw2");
	if (pw1.value == "" || pw2.value == "") {
		alert("Please enter your password into both boxes.");
	} else {
		if(pw1.value != pw2.value) {
			alert("Passwords do not match. Please try again, making sure that the password is exactly the same in both boxes.");
		} else {
			document.forms['ChgePW'].submit()
		}
	}
}

function ToggleDiv(d) {
	d = document.getElementById(d);
	if (d) {
		if (d.style.display == "none") {
			d.style.display = "block"
		} else {
			d.style.display = "none"
		}
	}
}


//Toggles plus minus image on category browser
function TogglePM(i) {
	i = document.getElementById(i)
	if (i) {
		if (i.src.indexOf("images/plus.gif") == -1) {
			i.src = "images/plus.gif";
		} else {
			i.src = "images/minus.gif";		
		}
	}
}

var im  = new Image();
im.src = "images/minus.gif"


function cleanUpPos(u) {
	window.open(u,'cleanwin','HEIGHT=1,WIDTH=1')
}


function SubmitProducts(s) {
	var s1 = document.getElementById('ProductSelect');
	var s2 = window.opener.document.getElementById(s);
	
	clearGroup(s2)
	
	for (i=0;i<s1.options.length;i++) {
		ol = s2.options.length++
		s2.options[ol].text = s1.options[i].text;
		s2.options[ol].value = s1.options[i].value;
	}
	
	window.close();
}

function clearGroup(s) {
	var i = s.options.length - 1

	while(i >= 0) {
		o = s.options[i];
		s.remove(i);
		i--;
	}	
}

function init(s, buf, bam) {
	var s1 = document.getElementById('ProductSelect');
	var s2 = window.opener.document.getElementById(s);
	
	for (i=0;i<s2.options.length;i++) {
		ol = s1.options.length++
		s1.options[ol].text = s2.options[i].text;
		s1.options[ol].value = s2.options[i].value;
	}
	
	buf ? bAllowMultipleQty = buf : bAllowMultipleQty = bam

}


function RemoveProducts() {
	bAllowMultipleQty = top.bAllowMultipleQty;
	var s = document.getElementById('ProductSelect');
	var i = s.options.length - 1

	while(i >= 0) {
		o = s.options[i];
		if (o.selected) {
			s.remove(i);
		}
		i--;
	}
}

function Decrement() {
	bAllowMultipleQty = top.bAllowMultipleQty;
	var s = document.getElementById('ProductSelect');
	var i = s.options.length - 1

	while(i >= 0) {
		o = s.options[i];
		if (o.selected) {
			
			v = getvalue(o.value)
			t = gettext(o.text)
			q = getqty(o.value)			

			q != 1 ? q-- : ""
			o.value = setvalue(v,q);
			o.text = settext(t,q);
			o.selected = true;
		}
		i--;
	}
}


function Increment() {
	bAllowMultipleQty = top.bAllowMultipleQty;
	var s = document.getElementById('ProductSelect');
	var i = s.options.length - 1

	while(i >= 0) {
		o = s.options[i];
		if (o.selected) {
			
			v = getvalue(o.value)
			t = gettext(o.text)
			q = getqty(o.value)			
			q++
			o.value = setvalue(v,q);
			o.text = settext(t,q);
			o.selected = true;
		}
		i--;
	}
}


function SelectProduct(pc,pn) {
	bAllowMultipleQty = top.bAllowMultipleQty;
	var s = window.parent.document.getElementById('ProductSelect');	
	var bFound = false;
	
	if (s.options.length > 0) {
		for(i=0;i<s.options.length;i++) {
			o = s.options[i]
			
			v = getvalue(o.value)
			t = gettext(o.text)
			q = getqty(o.value)
			
			if (getvalue(o.value) == pc) {
				bFound = true
				if (bAllowMultipleQty) {
					q++ //increments the quantity of the item
					o.value = setvalue(v,q)
					o.text = settext(t,q)
					break;
				}
			}
			
		}
		
		if (!bFound) {
			s.options[s.options.length] = new Option(settext(pn,1),setvalue(pc,1));
		}
		
	} else {
		s.options[s.options.length] = new Option(settext(pn,1),setvalue(pc,1));
	}
}

function getvalue(s) { return bAllowMultipleQty ? s.substring(0,s.indexOf(";")) : s }
function gettext(s) { return s.substring(0,s.length-4) }
function getqty(s) { return bAllowMultipleQty ? s.substr(s.indexOf(";")+1,1) : 1 }
function setvalue(v,q) { return bAllowMultipleQty ? v + ";" + q : v }
function settext(t,q) { return bAllowMultipleQty ? t + " x " + q : t }

function selectall(id) {
	s = document.getElementById(id);
	for(i=0;i<s.options.length;i++) {
		o = s.options[i]
		o.selected = true;
	}	
}

function SetType(tid,r) {
	var s = document.getElementById('BuyGroup')
	if (tid == 1) {
		document.getElementById('buyqty').value = 0;
		document.getElementById('buyqty').disabled = true;
		document.getElementById('buyqtytext').style.color = '#999999';
	} else {
		document.getElementById('buyqty').value = '';
		document.getElementById('buyqty').disabled = false;	
		document.getElementById('buyqtytext').style.color = 'black';
	}
	document.getElementById('PurchaseType').value = r.value
	
	clearGroup(s);
	
}

function OpenAd(u,w,h) {
	window.open(u,'Adwin','HEIGHT=' + h + ',WIDTH=' + w);
}

function OpenRegWin() {
	window.open('http://www.sugro.co.uk/shopregister.asp','Win1');
}


function checkAll(formId, checkboxName, controllerCheckbox)
{
	if(!controllerCheckbox)
	{
		return;
	}

	var oForm = document.getElementById(formId);

	if (oForm)
	{
		var oCheckboxes =  oForm.elements[checkboxName];
		if(oCheckboxes)
		{
			for (var i = 0; i < oCheckboxes.length; i++)
			{
				 oCheckboxes[i].checked = controllerCheckbox.checked; 
			}
		}
		
	}
}

