

/******************************
*
* Define an HTML template for most dialog windows.
* the only thing we really need here is a title.
*
*******************************/
function dialogTemplate(title, body) {

	var  html = '';
	html += '<h2>' + title + '</h2>';
	html += '<div>' + body + '</div>';

	return html;
}


/******************************
*
* Product & Category Management Functions
*
*******************************/
function toggleCategory(id) {

	$("#category_" + id + "_toggle").toggleClass('ico-right-arrow').toggleClass('ico-down-arrow');

	var cont = dynamItElem("category_" + id + "_container");
	var inner = $(cont).html();
	if(inner == '' || inner == null) {
		dynamItSend(dURL + 'category.open&id=' + id);
		dynamItLoad(dURL + 'category.subs&id=' + id, cont, null, null, function() {
			$(cont).slideToggle('slow');
		});
	} else {
		dynamItSend(dURL + 'category.close&id=' + id);
		$(cont).slideToggle('slow');
	}

}

function openCategory(id) {

	$("#category_" + id + "_toggle").removeClass('ico-right-arrow').addClass('ico-down-arrow');

	var cont = dynamItElem("category_" + id + "_container");
	var inner = $(cont).html();
	if(inner == '' || inner == null) {
		dynamItLoad(dURL + 'category.subs&id=' + id, cont, null, null, function() {
			$(cont).slideToggle('slow');
		});
	} else {
		$(cont).slideDown('slow');
	}
}

function addCategory(top) {

	var body = '';
	body += '<div class="center padd">Category Name: <input type="text" id="newcategory" value="" class="txt" /></div>';
	body += '<div class="center padd"><input type="button" value="Add" onclick="act_addCategory(' + top + ', dynamItElem(\'newcategory\').value);" /> &nbsp; <input type="button" value="Cancel" onclick="dynamItCloseDialog();" /></div>';

	var html = dialogTemplate('Add Sub Category', body);

	dynamItOpenDialog(html, 300, 'auto');
}

	function act_addCategory(top, newcat) {
		var url = dURL + 'category.create';
		url += '&parent=' + top;
		url += '&category=' + urlencode(newcat);

		dynamItSend(url);
	}


function editCategory(id) {
	var currentName = $('#categorysub_' + id + '_name').html();

	var body = '';
	body += '<div class="center padd">Category Name: <input type="text" id="newcategory" value="' + currentName + '" class="txt" /></div>';
	body += '<div class="center padd"><input type="button" value="Update" onclick="act_editCategory(' + id + ', dynamItElem(\'newcategory\').value);" /> &nbsp; <input type="button" value="Cancel" onclick="dynamItCloseDialog();" /></div>';

	var html = dialogTemplate('Edit Sub Category', body);

	dynamItOpenDialog(html, 300, 'auto');

}

	function act_editCategory(id, newcat) {
		var url = dURL + 'category.update';
		url += '&category=' + urlencode(newcat);
		url += '&id=' + id;

		dynamItSend(url);
	}


function delCategory(id) {

	dynamItSendOnConfirm(dURL + 'category.delete&id=' + id, 'Are you sure you wish to permanently delete this category?');
}

function checkCategory(id, on) {
	var active = (on) ? 1 : 0;
	dynamItSend(dURL + 'category.active&active=' + active + '&id=' + id);
}

function addCategoryTop() {

	var body = '';
	body += '<div class="center padd">Category Name: <input type="text" id="newcategory" value="" class="txt" /></div>';
	body += '<div class="center padd"><input type="button" value="Add" onclick="act_addCategoryTop(dynamItElem(\'newcategory\').value);" /> &nbsp; <input type="button" value="Cancel" onclick="dynamItCloseDialog();" /></div>';

	var html = dialogTemplate('Add Main Category', body);

	dynamItOpenDialog(html, 300, 'auto');

}

	function act_addCategoryTop(newcat) {
		var url = dURL + 'category.createmain';
		url += '&category=' + urlencode(newcat);

		dynamItSend(url);
	}

function editCategoryTop(id) {

	var currentName = $('#category_' + id + '_name').html();

	var body = '';
	body += '<div class="center padd">Category Name: <input type="text" id="newcategory" value="' + currentName + '" class="txt" /></div>';
	body += '<div class="center padd"><input type="button" value="Update" onclick="act_editCategoryTop(' + id + ', dynamItElem(\'newcategory\').value);" /> &nbsp; <input type="button" value="Cancel" onclick="dynamItCloseDialog();" /></div>';

	var html = dialogTemplate('Edit Main Category', body);

	dynamItOpenDialog(html, 300, 'auto');

}

	function act_editCategoryTop(id, newcat) {
		var url = dURL + 'category.updatemain';
		url += '&category=' + urlencode(newcat);
		url += '&id=' + id;

		dynamItSend(url);
	}


function delCategoryTop(id) {
	dynamItSendOnConfirm(dURL + 'category.deletemain&id=' + id, 'Are you sure you wish to permanently delete this category and ALL sub-categories?');
}

function checkCategoryTop(id, on) {
	var active = (on) ? 1 : 0;
	dynamItSend(dURL + 'category.activemain&active=' + active + '&id=' + id);
}

function viewCategory(id) {
	location.href = dURL + 'category.managesub&id=' + id;
}

function addProduct(cat) {
	location.href = dURL + 'product.add&cat=' + cat;
}

function editProduct(id) {
	location.href = dURL + 'product.edit&id=' + id;
}

function delProduct(id) {
	dynamItSendOnConfirm(dURL + 'product.delete&id=' + id, 'Are you sure you wish to premanently delete this product? <b>Warning:</b> all data related to this product will be deleted. You may NOT undo this action.');
}

function checkProduct(id, on) {
	var active = (on) ? 1 : 0;
	dynamItSend(dURL + 'product.active&active=' + active + '&id=' + id);
}

function removeProductPhoto(id) {
	dynamItSendOnConfirm(dURL + 'product.remphoto&id=' + id, 'Really remove this photo from the product? Warning: this will delete the image file from the server.');
}

function removeProductPrice(id) {
	dynamItSendOnConfirm(dURL + 'product.remprice&id=' + id, 'Really remove this price range from the product?');
}

function removeProductCategory(prod, id) {
	dynamItSendOnConfirm(dURL + 'product.remcategory&product=' + prod + '&category=' + id, 'Really remove the product from this category?');
}

function removeProductTag(prod, id) {
	dynamItSendOnConfirm(dURL + 'product.remtag&product=' + prod + '&tag=' + id, 'Really remove this tag from the product?');
}
function removeSimilarProduct(prod, id) {
	dynamItSendOnConfirm(dURL + 'product.remsimilar&product=' + prod + '&similar=' + id, 'Really remove this similar product?');
}

function doAddPhoto(phid, path, file) {

	var html = '<div id="photo_' + phid + '" style="height:60px; width:200px;">';
	html += '	<span class="controls"><a href="javascript:removeProductPhoto(' + phid + ')" class="ico-small" id="ico-del"></a></span>';
	html += '<img src="' + path + 'tiny/' + file + '" alt="" /> </div>';

	$(html).appendTo("#photo_div");
}

function addBrand() {

	var body = '';
	body += '<div class="center padd">Brand Name: <input type="text" id="newbrand" value="" class="txt" /></div>';
	body += '<div class="center padd"><input type="button" value="Add" onclick="act_addBrand(dynamItElem(\'newbrand\').value);" /> &nbsp; <input type="button" value="Cancel" onclick="dynamItCloseDialog();" /></div>';

	var html = dialogTemplate('Add Brand', body);

	dynamItOpenDialog(html, 300, 'auto');

}
	function act_addBrand(newbrand) {
		var url = dURL + 'admin.addbrand';
		url += '&brand=' + urlencode(newbrand);

		dynamItSend(url);
	}

function addTag() {

	var body = '';
	body += '<div class="center padd">Tag Name: <input type="text" id="newtag" value="" class="txt" /></div>';
	body += '<div class="center padd"><input type="button" value="Add" onclick="act_addTag(dynamItElem(\'newtag\').value);" /> &nbsp; <input type="button" value="Cancel" onclick="dynamItCloseDialog();" /></div>';

	var html = dialogTemplate('Add Tag', body);

	dynamItOpenDialog(html, 300, 'auto');

}

	function act_addTag(newtag) {
		var url = dURL + 'tag.create';
		url += '&tag=' + urlencode(newtag);

		dynamItSend(url);
	}

function editTag(id) {
	var currentName = $('#tag_' + id + '_name').html();

	var body = '';
	body += '<div class="center padd">Tag Name: <input type="text" id="newtag" value="' + currentName + '" class="txt" /></div>';
	body += '<div class="center padd"><input type="button" value="Update" onclick="act_editTag(' + id + ', dynamItElem(\'newtag\').value);" /> &nbsp; <input type="button" value="Cancel" onclick="dynamItCloseDialog();" /></div>';

	var html = dialogTemplate('Edit Tag', body);

	dynamItOpenDialog(html, 300, 'auto');

}

	function act_editTag(id, newtag) {
		var url = dURL + 'tag.update';
		url += '&tag=' + urlencode(newtag);
		url += '&id=' + id;

		dynamItSend(url);
	}


function delTag(id) {

	dynamItSendOnConfirm(dURL + 'tag.delete&id=' + id, 'Are you sure you wish to permanently delete this tag?');
}

function archiveRequest(id) {
	dynamItSend(dURL+'request.archive&id='+id);
}

function delRequest(id) {
	dynamItSendOnConfirm(dURL + 'request.delete&id=' + id, 'Are you sure you wish to permanently delete this request?');
}

function viewRequest(id) {
	dynamItLoad(dURL + 'request.view&id=' + id,400,400);
}

function delBanner(id) {
	dynamItSendOnConfirm(dURL + 'admin.removebanner&id=' + id, 'Are you sure you wish to permanently delete this banner?');
}

function checkBanner(id, on) {
	var active = (on) ? 1 : 0;
	dynamItSend(dURL + 'admin.activebanner&active=' + active + '&id=' + id);
}

function delFeature(id) {
	dynamItSendOnConfirm(dURL + 'admin.removefeature&id=' + id, 'Are you sure you wish to permanently delete this feature?');
}

function checkFeature(id, on) {
	var active = (on) ? 1 : 0;
	dynamItSend(dURL + 'admin.activefeature&active=' + active + '&id=' + id);
}


/******************************
*
* Frontend AJAX/JavaScript Functions
*
*******************************/
function swapPhoto(path, id) {

	var i = '<img src="' + path + '" alt="" />';
	$("#productPhoto").html(i);
}

function requestInformation(pid) {
	var url = dURL + 'request.form&id=' + pid;
	dynamItLoad(url, null, 400, 'auto');
}
function requestThankYou() {

	var html = '<div class="padd">Thank you for your interest in our products! We will review your inquiry and contact you with more details.';
	html += ' If you would like immediate assistance please give us a call at 614-481-8864. <div>&nbsp;</div>';
	html += '<div style="text-align:center;"><input type="button" value=" &nbsp; Ok &nbsp; " onclick="dynamItCloseDialog();" /></div></div>';

	html = dialogTemplate('Request Information', html);
	dynamItOpenDialog(html, 400, 'auto');
}
