if (window.ActiveXObject) {
	var srvrConnect=new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
	var srvrConnect=new XMLHttpRequest();
}

function reverse(obj){

	if (obj.className!='selected'){
		obj.style.backgroundColor=(obj.style.backgroundColor=='white'?'black':'white');
		obj.style.color=(obj.style.color=='black'?'white':'black');
	}
}

var lastElement=0;
var slctdIndex=0;

function selElement(obj,indx){
		document.getElementById('editor').style.display='none';
		document.getElementById(lastElement).style.color='black';
		document.getElementById(lastElement).style.backgroundColor='white';
		document.getElementById(lastElement).className='notSelected';
	obj.style.color='white';
	obj.style.backgroundColor='black';
	obj.className='selected'
	lastElement=obj.id;
	slctdIndex=indx;
	document.getElementById('subCatOf').innerHTML='Sub-category of: <b>' + obj.innerHTML + '</b>';
}

function assignCat(obj,itemID){
	var catSpans=obj.parentNode.getElementsByTagName('span');
	for (i=0;i<catSpans.length;i++){
		catSpans[i].style.backgroundColor='white';
		catSpans[i].style.color='black';
		catSpans[i].className='notSelected';
	}
	if (obj.className=='selected'){
		//obj.style.backgroundColor='#fffffe';
		//obj.style.color='#000001';
		//obj.className='notSelected';
		//var srch=new RegExp(obj.id +',','g');
		//categories=categories.replace(srch,'');
		categories='';
	}else{
		obj.style.backgroundColor='black';
		obj.style.color='white';
		obj.className='selected';
		//categories=categories+obj.id+','
		categories=obj.id;
	}
	sendStr='act=1&newCats=' + escape(categories)+'&id='+itemID;
	srvrConnect.open('post','store/storeAdmin/_modProducts.cfm',0);
	srvrConnect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	srvrConnect.send(sendStr);
	document.getElementById('response').innerHTML=srvrConnect.responseText;
}

function modCat(action){
	if (action==1 && document.getElementById('newCat').value==''){
		alert('You must enter name for the new category!');
		document.getElementById('newCat').focus()
		return;
	}
	sendStr='act='+action+'&cat='+escape(document.getElementById('newCat').value)+'&relID='+lastElement;
	srvrConnect.open('post','store/storeAdmin/_modCategories.cfm',0);
	srvrConnect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	srvrConnect.send(sendStr);
	var out=srvrConnect.responseText.split('||||');
	document.getElementById('hierarchy').innerHTML=out[0];
	hierID=out[1].replace(/\s/g,'').split(',');
	hierLevel=out[2].replace(/\s/g,'').split(',');
	document.getElementById('adder').style.display='none';
	document.getElementById('newCat').value='';
		if (action==2) lastElement=0;
		document.getElementById('subCatOf').innerHTML='Sub-category of: <b>' + document.getElementById(lastElement).innerHTML + '</b>';
		document.getElementById(lastElement).style.color='white';
		document.getElementById(lastElement).style.backgroundColor='black';
		document.getElementById(lastElement).className='selected';
		slctdIndex=lastElement;		
}

function updateCatList(){
	sendStr='init=true&relID=' + lastElement;
	srvrConnect.open('post','store/storeAdmin/categories_hierarchy.cfm',0);
	srvrConnect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	srvrConnect.send(sendStr);
	var out=srvrConnect.responseText.split('||||');
	document.getElementById('hierarchy').innerHTML=out[0];
		document.getElementById(lastElement).style.color='white';
		document.getElementById(lastElement).style.backgroundColor='black';
		document.getElementById(lastElement).className='selected';
		slctdIndex=lastElement;		
}

var focusElement='';
var hierLevel=new Array(1);
var hierID=new Array(1);

function keyCap(e){
	if (window.event){
		 key=event.keyCode;
	} else {
		key=e.which;
	}
	
	// DELETE ROUTINE
	if (key==46 && typeof(focusElement)=='string' && lastElement!=0) {
		sendStr='act=7&catID=' + lastElement;
		var resp=getXML(sendStr,'store/_doStoreAjax.cfm');
		//crawlXML(resp);
		var chk=resp.getElementsByTagName('success')[0].firstChild.data;
		if (chk!='true'){
			document.getElementById('response').innerHTML=resp.getElementsByTagName('message')[0].firstChild.data;
			alert("An error occurred while attempting to delete the category. If this problem persists, contact Big Giant Media for assistance.");
			return;
		} else {
			if (resp.getElementsByTagName('canDelete')[0].firstChild.data=='true'){
				if (!confirm('You are about to delete a category. Do you wish to continue?')) return;
				var loopCount=1;
				while (hierLevel[slctdIndex+loopCount]>hierLevel[slctdIndex]){
					lastElement=lastElement+','+hierID[slctdIndex+loopCount];
					loopCount+=1;
				}
				modCat(2);
			} else {
				var msg='This category already has associated products. You may not delete a category '
					+'until all products have been deleted or reassigned.';
				alert(msg);
			}
		}
		
	}
	
	// MOVE CATEGORY UP
	if (key==38 && typeof(focusElement)=='string' && lastElement!=0) {
		var el=0;
		while (hierID[el]!=lastElement && el<hierID.length){
			el+=1;
		}
		var temp=hierID[el];
		hierID[el]=hierID[el-1];
		hierID[el-1]=temp;
		modCat(3);
		if (event) event.returnValue=false;
		return false;
	}
	
	//MOVE CATEGORY DOWN
	if (key==40 && typeof(focusElement)=='string' && lastElement!=0) {
		var el=0;
		while (hierID[el]!=lastElement && el<hierID.length){
			el+=1;
		}
		if (el<hierID.length-1){
			var temp=hierID[el];
			hierID[el]=hierID[el+1];
			hierID[el+1]=temp;
		}
		modCat(4);
		if (event) event.returnValue=false;
		return false;
	}
	
	// CYCLE UP THROUGH CATEGORIES
	if (key==37 && typeof(focusElement)=='string') {
		var el=0;
		while (hierID[el]!=lastElement && el<hierID.length){
			el+=1;
		}
		if (el>0){
			document.getElementById(lastElement).style.color='black';
			document.getElementById(lastElement).style.backgroundColor='white';
			document.getElementById(lastElement).className='notSelected';
			lastElement=hierID[el-1];
			document.getElementById('subCatOf').innerHTML='Sub-category of: <b>' + document.getElementById(lastElement).innerHTML + '</b>';
			document.getElementById(lastElement).style.color='white';
			document.getElementById(lastElement).style.backgroundColor='black';
			document.getElementById(lastElement).className='selected';			
			slctdIndex=el-1;
//			document.getElementById(lastElement).scrollIntoView(false);
		}
		if (event) event.returnValue=false;
		return false;
	}
	
	//CYCLE DOWN THROUGH CATEGORIES
	if (key==39 && typeof(focusElement)=='string') {
		var el=0;
		while (hierID[el]!=lastElement && el<hierID.length){
			el+=1;
		}
		if (el<hierID.length-1){
			document.getElementById(lastElement).style.color='black';
			document.getElementById(lastElement).style.backgroundColor='white';
		document.getElementById(lastElement).className='notSelected';
			lastElement=hierID[el+1];
			document.getElementById('subCatOf').innerHTML='Sub-category of: <b>' + document.getElementById(lastElement).innerHTML + '</b>';
			document.getElementById(lastElement).style.color='white';
			document.getElementById(lastElement).style.backgroundColor='black';
			document.getElementById(lastElement).className='selected';			
			slctdIndex=el+1;
//			document.getElementById(lastElement).scrollIntoView(false);
		}
		if (event) event.returnValue=false;
		return false;
	}
	
	//DISPLAY PROPERTIES OF CATEGORY OR SAVE NEW CATEGORY FROM INPUT BOX
	if (key==13 && document.getElementById('adder').style.display=='block') {
		modCat(1);
		if (event) event.returnValue=false;
		return false;
	} else if (key==13 && typeof(focusElement)=='string' && lastElement!=0) {
		//document.getElementById('editor').style.top=(-30+(document.all?document.body.scrollTop:window.pageYOffset)) +'px';
		document.getElementById('editFrame').src='store/storeAdmin/categories_detail.cfm?catID='+lastElement;
		document.getElementById('editor').style.display='block';
		if (event) event.returnValue=false;
		return false;
		//lastElement=lastElement;
	}
	
	// SHOW NEW CATEGORY INPUT BOX
	if (key==45 && typeof(focusElement)=='string') {
		//document.getElementById('adder').style.top=(110+(document.all?document.body.scrollTop:window.pageYOffset)) +'px';
		document.getElementById('adder').style.display='block';
		document.getElementById('newCat').focus();
	}
}

  //SHOW IMAGES PRIOR TO UPLOADING - categories EDITOR
	
function showImg(file,objName,imgHolder,loaderSpan,noneRadio){
	document.getElementById(imgHolder).src=file;
	if (loaderSpan!=''){
		document.getElementById(loaderSpan).innerHTML='<input type="file" name="'+ objName +'" style="width: 250px" onchange="showImg(this.value,this.name,\''+imgHolder+'\',\'\',\''+ noneRadio +'\')" />';
	} else {
		document.getElementById(noneRadio).checked=false;
	}
}

// UPDATE SIZE/PRICE TABLE ON SIZE CATEGORY INPUT

function setSizes(strSizes,itemID){
	sendStr='act=2&sizes=' + escape(strSizes)+'&id='+itemID;
	srvrConnect.open('post','store/storeAdmin/_modProducts.cfm',0);
	srvrConnect.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	srvrConnect.send(sendStr);
	document.getElementById('tblSizePrice').innerHTML=srvrConnect.responseText;

}
