/* Custom javascript
	Written by Chad Grigsby
	8/6/2009
*/

// Ajax

function GetXmlHttpObject() { 
	var objXMLHttp=null;
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

var target_span;

function updateCategoryList (theValue)  {
	var theValueArray = theValue.split(',');
	var categoryID = theValueArray[0];
	var targetID = theValueArray[1];
	var title = theValueArray[2];
	var formName = theValueArray[3];
	
	if (typeof(formName) != 'undefined' && formName != '') {
		theForm = document.getElementById(formName);
		theForm.categoryID.value = categoryID;
	} else { document.withSelected.categoryID.value = categoryID; }
	target_span = targetID;
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="get_categories.php";
	url=url+"?categoryID="+categoryID;
	url=url+"&targetName="+targetID;
	url=url+"&title="+title;
	url=url+"&formName="+formName;
	url=url+"&sid="+Math.random();	//prevents a cached version from being used
	//document.getElementById(target_span).innerHTML = url;
	xmlHttp.onreadystatechange=FillBox2;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}
function FillBox2() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		var theResponse = xmlHttp.responseText;
		//alert(theResponse);
		if (target_span == 'sub_categoryID') { document.getElementById('sub_categoryID2').innerHTML = ''; }
		//alert(theResponse);
		document.getElementById(target_span).innerHTML = theResponse;
    }
}

// Product editor

function deleteImage (imageID, title) {
	var confirm_delete = confirm("Are you sure you want to delete product '" + title + "'?");
	if (confirm_delete) {
		document.getElementById('imageID').value = imageID;
		document.getElementById('delete_image').value = true;
		document.delete_image.submit();
		return;
	} else { return false; }
}

// With selected products editor
var selected_images = new Array();

function select_image (image_selector) {
	if (image_selector.checked) {
		selected_images.push(image_selector.value);
	} else {
		for (x in selected_images) {
			if (selected_images[x] == image_selector.value) {
				selected_images.splice(x, 1);
			}
		}
	}
}
function moveToParent () {
	if (selected_images.length > 0) {
		if (!confirm('Are you sure you want to move your selection to a different category?')) {
			return false;
		}
		document.withSelected.do_with_selected.value = 'Move';
		document.withSelected.moveToParentID.value = document.withSelected.parentID.value;
		document.withSelected.selected_images.value = selected_images;
		document.withSelected.submit()
	} else {
		alert('No items selected!');
		return false;
	}
}
function send_withSelected (theForm) {
	if (selected_images.length > 0) {
		if (theForm.do_with_selected.value == 'Delete') {
			if (!confirm('Are you sure you want to delete your selection?')) {
				return false;
			}
		} else if (theForm.do_with_selected.value == 'Move') {
			if (!confirm('Are you sure you want to move your selection to a different category?')) {
				return false;
			}
		}
		theForm.selected_images.value = selected_images;
		return true;
	} else {
		alert('No items selected!');
		return false;
	}
}

// Category editor
function send_editCategories (theForm) {
	if (theForm.delete_categoryID.value != '') {
		//alert(theForm.delete_categoryID.value);
		if (!confirm('Are you sure you want to delete this category?')) {
			return false;
		}
	}
	if (theForm.edit_categoryID.value == 'addCategory') {
		theForm.categoryID = '';
	}
}
function addCategory () {
	document.editCategories.categoryName.value = '';
	document.editCategories.parentID.value = document.editCategories.categoryID.value;
}
function updateCategory (categoryID_categoryName_style) {
	if (categoryID_categoryName_style == 'addCategory') {
		addCategory();
		return;
	} else if (categoryID_categoryName_style != '') {
		var categoryID_categoryName_styleArray = categoryID_categoryName_style.split('_');
		var categoryID = categoryID_categoryName_styleArray[0];
		var categoryName = categoryID_categoryName_styleArray[1];
		var style = categoryID_categoryName_styleArray[2];
		if (categoryName != '' && categoryID > 0) {
			document.editCategories.categoryID.value = categoryID;
			document.editCategories.categoryName.value = categoryName;
			//alert(document.getElementById(style).selected);
			document.getElementById(style).selected = true;
		}
	}
}

// Content editor

function deleteContentImage (contentID) {
	var confirm_delete = confirm('Are you sure you want to delete this content?');
	if (confirm_delete) {
		theForm = document.delete_content_form;
		theForm.contentID.value = contentID;
		theForm.delete_content.value = true;
		theForm.submit();
		return;
	} else { return false; }
}

if (typeof(alert_message) != 'undefined' && alert_message != '') { alert(alert_message); }
