/*
 * General functions
 */

function getElementsByClass(tagName,className)
{
	var tac = document.getElementsByTagName(tagName);
	//Loop through and compare the class to the passed in class name
	//need to be careful as some elements may have more than one class specified
	var matches = Array(); //element holder
	for (var i = 0; i<tac.length; i++)
	{
		if(tac[i].className.length>0)
		{
			//split on space to get an array of class names
			var tmp = tac[i].className.split(' ');
			//work through any class names found
			for(var j=0;j<tmp.length;j++)
			{
				if(tmp[j] == className)
				{
					matches[matches.length] = tac[i];
					break;
				}
			}
		}
	}

	if(matches.length>0)
		return matches;
	else
		return null;
}

function enableElements(tagName,className,enable)
{
	var els = getElementsByClass(tagName,className);
	if(els!=null)
	{
		for(var i=0;i<els.length;i++)
		{
			els[i].disabled = !enable;
		}
		
		return true;
	}
	else
		return false;
}


function doSearch()
{
	var searchTerm = document.getElementById('searchTerm').value;
	searchTerm = searchTerm.replace(/\s/,"");
	if (searchTerm!='')
		window.location = "search.php?search="+searchTerm;
	
	return false;
}

function doQuick()
{
	var quickCode = document.getElementById('quickCode').value;
	quickCode = quickCode.replace(/^\s/,"");
	quickCode = quickCode.replace(/\s$/,"");
	if(quickCode!='')
		window.location = "products.php?quickCode="+quickCode;

	return false;
}

function toggleAccountPassword(check,ID)
{
	var pass = document.getElementById(ID);
	if(check.checked)
	{
		pass.disabled = false;
		pass.focus();
	}
	else
	{
		pass.disabled = true;
	}
	
}

function toggleAccountEdit(el,prefix)
{
	//toggle to edit
	var vis = getElementsByClass('*',prefix+'Visible');
	var hid = getElementsByClass('*',prefix+'Hidden');
	
	for(var i=0;i<vis.length;i++)
	{
		vis[i].className = prefix+'Hidden';
		vis[i].disabled = true; //disable so fields are not submitted unless visible
	}
	
	for(var i=0;i<hid.length;i++)
	{
		hid[i].className = prefix+'Visible';
		//enable elements (except for password which need to be enabled separately
		if(hid[i].name!='customer_password')
			hid[i].disabled = false;
	}
	
	toggleLinkText(el);

	return false; //initiated by a link.  This ensures that the link is not actioned in any other way
}


function toggleLinkText(el)
{
	var txtNode = el.firstChild;
	
	if(txtNode.nodeValue=='Edit')
		txtNode.nodeValue='View only';
	else
		txtNode.nodeValue='Edit';
}

function removeFromBasket(id,el)
{
	//set qty to 0 and click the recalculate button
	document.getElementById(id).value=0;
	el.form.recalculateTotalsBtn.click();
}

function generate(userID,passID,checkID,currentUsername)
{
	var newWindow = window.open("generate.php?userID="+userID+"&passID="+passID+"&currentUser="+currentUsername+"&checkID="+checkID,"GeneratorWindow","toolbar=no,resizable=yes,location=no,scrollbars=yes,width=400,height=300");
	newWindow.focus();
	return false; //so the calling window doesn't actually do anything
}

function updateUseBtn(el)
{
	var form = el.form;
	if (form.useData.value=='Use')
	{
		form.useData.value='Check username';
	}
}

function toggleUsername(el)
{
	el.form.username.disabled = el.checked;
	if(el.form.useData.value!='Use')
		el.form.useData.value='Use';
	
}

function useAuthData(el,origUser,userID,passID,checkID)
{
	//check if the username to be used is the same one that was generated
	//if not, it will have to be revalidated
	var form = el.form;
	
	if(form.username.value!=origUser && !form.ignoreUsername.checked)
	{
		form.submit.click();
	}
	else
	{
		//update the opener window with the chosen values
		if(!form.ignoreUsername.checked)
			window.opener.document.getElementById(userID).value = form.username.value;
			
		window.opener.document.getElementById(passID).value = form.password.value;
		window.opener.document.getElementById(passID).disabled = false;
		//enable password to be updated
		window.opener.document.getElementById(checkID).checked = true;
		
		//focus the opener (in case it has been place a few levels deep)
		window.opener.focus();
		//close the current window
		window.close();
	}
}

function openTerms()
{
	var newWindow = window.open("terms_and_conditions.php","termsWindow","toolbar=no,resizable=yes,location=no,scrollbars=yes,width=500,height=500");
	newWindow.focus();
	return false;
}

//The following two functions are taken from http://www.javascripter.net/faq/arecookie.htm
function ReadCookie(cookieName) 
{
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return "";
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetCookie(cookieName,cookieValue,nDays) 
{
	var today = new Date();
	//var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	//expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue);
                 //+ ";expires="+expire.toGMTString();
}

//the following function is a modified version of a similar function from the website above.
function cookieTest()
{
	//test for enabled cookies
	testValue=Math.floor(1000*Math.random());
	SetCookie('LTSCookieCheck',testValue);
	if (testValue==ReadCookie('LTSCookieCheck'))
		return true;	
	else
		return false;
}
