var searchBoxReceivedFocus = false;

function setSearchFocus(inputObj){
	if(!searchBoxReceivedFocus) inputObj.value = "";
	searchBoxReceivedFocus = true;
	inputObj.className = "active";
}


function initialise(){
	setProductGrids();
	setCollapsibleContent();
}


function setProductGrids(){
	
	gridContainers = getElementsByClassName("productgridcontainer");

	for(var i = 0; i < gridContainers.length; i++){
		
		gridContainers[i].style.position = "relative";
	}
	
	productGrids = getElementsByClassName("productgrid");
	for(i = 0; i < productGrids.length; i++){
		productGrids[i].style.top = "0px";
		productGrids[i].style.left = "0px";
	}
	
	for(var i = 0; i < gridContainers.length; i++){
		
		containerobj = gridContainers[i];
		id = containerobj.id.split("_");
		tabcontainerobj = document.getElementById("tabcontainer_"+id[id.length-1]);
		
		HideAllProductGrids(containerobj.id);
		var x = 0;	
		for(var j = 0; j < containerobj.childNodes.length; j++){
			obj = containerobj.childNodes[j];
			if(obj.className == "productgrid"){
				if(tabcontainerobj.childNodes[x].className == "selectedtab")
					obj.style.display = "block";
				x++;
			} 
	
		}

	}

	
}

function ResetTabs(tabcontainerid){
	tabcontainer = document.getElementById(tabcontainerid);
	for(var i = 0; i < tabcontainer.childNodes.length; i++){
		obj = tabcontainer.childNodes[i];
		if(obj.className == "selectedtab")
			obj.className = "tab";
	}
}

function HideAllProductGrids(containerid){
	container = document.getElementById(containerid);
	
	for(var i = 0; i < container.childNodes.length; i++){
		obj = container.childNodes[i];
		if(obj.className == "productgrid"){
			obj.style.display = "none";
		} 
	
	}
	

}

function ShowProductGrid(tabid, tabcontainerid, containerid, gridid){
	ResetTabs(tabcontainerid);
	tab = document.getElementById(tabid);
	tab.className = "selectedtab";
	HideAllProductGrids(containerid);
	obj = document.getElementById(gridid);
	obj.style.display = 'block';
}



function getElementsByClassName(className){ 
	var arr = new Array(); 
	var elems = document.getElementsByTagName("div");
	for(var i = 0; i < elems.length; i++){
		var elem = elems[i];
		if(elem.className == className) arr[arr.length] = elem;
	}
	return arr;
}

function RemoveKeyword(keyword){

	//if(!confirm("Are you sure you want to remove the keyword\n"+keyword+" from this item?")) return;
	
	document.getElementById("keywordvalue").value = keyword;
	document.getElementById("keywordaction").value = "removekeyword";
	document.getElementById("frm_keywordmanagement").submit();

}

function AddKeyword(keyword){

	//if(!confirm("Are you sure you want to add the keyword\n"+keyword+" to this item?")) return;

	document.getElementById("keywordvalue").value = keyword;
	document.getElementById("keywordaction").value = "addkeyword";
	document.getElementById("frm_keywordmanagement").submit();
}

function RemoveItem(productid){

	if(!confirm("Are you sure you want to remove product "+productid+" from the database?")) return;

	document.getElementById("itemaction").value = "removeitem";
	document.getElementById("productid").value = productid;
	document.getElementById("adminform").submit();
}


function setRemoveItem(productid){
	checkboxname = "cb"+productid;

}

function setProductIdList(){
	var inputs = document.getElementsByTagName("input");
	var items = new Array();
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].className == "itemcheckbox"){
			if(inputs[i].checked) items.push(inputs[i].value);
		}
	}
	document.getElementById("itemstoremove").value = items.join(",");
	
}

function setAllItems(){
	var elem = document.getElementById("setallitems");
	var inputs = document.getElementsByTagName("input");
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].className == "itemcheckbox"){
			if(elem.checked) inputs[i].checked = true;
			else inputs[i].checked = false;
		}
	}
	setProductIdList();
}

function submitRemoveItems(){
	if(confirm("Remove all selected items - Are you sure?")) return true;
	else return false;
}

function setCollapsibleContent(){
	var elems = getElementsByClassName("collapsible");
	for(var i = 0; i < elems.length; i++){
		elems[i].childNodes[0].id = "collapsible_"+(i+1);
		elems[i].childNodes[0].style.display = "block";
		elems[i].innerHTML = "<p>(<a id=\"coll_anchor_"+(i+1)+"\" class=\"\" href=\"javascript:toggleCollapsible('coll_anchor_"+(i+1)+"', 'collapsible_"+(i+1)+"');\">more info...</a>)</p>"+elems[i].innerHTML;
		toggleCollapsible("coll_anchor_"+(i+1), "collapsible_"+(i+1));
	}

}


function toggleCollapsible(anchor_id, collapsible_id){
	collapsible = document.getElementById(collapsible_id);
	anchor = document.getElementById(anchor_id);
	
	if(collapsible.style.display == "block"){
		anchor.innerHTML = "more info...";
		collapsible.style.display = "none";
	}
	else{
		anchor.innerHTML = "less info..";
		collapsible.style.display = "block";
	}

}





