/********************
COMMON FUNCTIONS
********************/

/*
Hide/show on click
*/
function expandcollapse (id)
{ 
	which = document.getElementById(id); 
	if (which.className=="hidden")
	{ 
		which.className="shown"; 
	}
	else
	{ 
		which.className="hidden"; 
	} 
} 

/*
Strip whitespace from the beginning and end of a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value))
	{
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0)
	{
		textBox.value = 0;		
	}
	else
	{
		textBox.value = parseInt(textBox.value);
	}
*/
}

/*
Check if a form element is empty. If so, display an alert box and focus	on the element
*/
function isEmpty(formElement, message)
	{
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '')
	{
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++)
	{
		if (listElement.options[i].value == listValue)
		{
			listElement.selectedIndex = i;
		}
	}	
}

/*
Opening picture window
*/
function picWin(URL)
{
	aWindow = window.open(URL, "composerwindow", "toolbar=no, width=650, height=850, status=no, scrollbars=yes, resize=yes, menubar=no, left=0, top=0");
}

/* 
Live preview of text area.
Created by: Jonathan Lau; Web Site: http://lauweijie7715.excell.org
*/
function livePreviewAdd()
{
  document.getElementById("preview").innerHTML = document.frmAdd.mtxContent.value
  setTimeout("livePreviewAdd()",80);
}
function livePreviewModify()
{
  document.getElementById("preview").innerHTML = document.frmModify.mtxContent.value
  setTimeout("livePreviewModify()",80);
}

/*
Picture Rollover Code
*/
function movepic(img_name,img_src)
{
	document[img_name].src=img_src;
}

/********************
CHECKOUT FUNCTIONS
********************/
function setPaymentInfo(isChecked)
{
	with (window.document.frmPayShip)
	{
		if (isChecked)
		{
			txtShippingFirstName.value  = txtPaymentFirstName.value;
			txtShippingLastName.value   = txtPaymentLastName.value;
			txtShippingAddress1.value   = txtPaymentAddress1.value;
			txtShippingAddress2.value   = txtPaymentAddress2.value;
			txtShippingPhone.value      = txtPaymentPhone.value;
			txtShippingState.value      = txtPaymentState.value;			
			txtShippingCity.value       = txtPaymentCity.value;
			txtShippingPostalCode.value = txtPaymentPostalCode.value;
			txtShippingCountry.value    = txtPaymentCountry.value;

			txtShippingFirstName.readOnly  = true;
			txtShippingLastName.readOnly   = true;
			txtShippingAddress1.readOnly   = true;
			txtShippingAddress2.readOnly   = true;
			txtShippingPhone.readOnly      = true;
			txtShippingState.readOnly      = true;			
			txtShippingCity.readOnly       = true;
			txtShippingPostalCode.readOnly = true;			
			txtShippingCountry.readOnly    = true;			
		}
		else
		{
			txtShippingFirstName.readOnly  = false;
			txtShippingLastName.readOnly   = false;
			txtShippingAddress1.readOnly   = false;
			txtShippingAddress2.readOnly   = false;
			txtShippingPhone.readOnly      = false;
			txtShippingState.readOnly      = false;			
			txtShippingCity.readOnly       = false;
			txtShippingPostalCode.readOnly = false;			
			txtShippingCountry.readOnly    = false;			
		}
	}
}


function checkShippingAndPaymentInfo()
{
	with (window.document.frmCheckout)
	{
		if (isEmpty(txtPaymentEmail, 'Enter email address.')) {return false;}
		else if (isEmpty(txtPaymentFirstName, 'Enter first name.'))	{return false;}
		else if (isEmpty(txtPaymentLastName, 'Enter last name.')) {return false;}
		else if (isEmpty(txtPaymentAddress1, 'Enter address.')) {return false;}
		else if (isEmpty(txtPaymentPhone, 'Enter phone number.')) {return false;}
		else if (isEmpty(txtPaymentState, 'Enter state.')) {return false;}
		else if (isEmpty(txtPaymentCity, 'Enter city.')) {return false;}
		else if (isEmpty(txtPaymentPostalCode, 'Enter postal/zip code.')) {return false;}
		else if (isEmpty(txtShippingFirstName, 'Enter first name.')) {return false;}
		else if (isEmpty(txtShippingLastName, 'Enter last name.')) {return false;}
		else if (isEmpty(txtShippingAddress1, 'Enter address.')) {return false;}
		else if (isEmpty(txtShippingPhone, 'Enter phone number.')) {return false;}
		else if (isEmpty(txtShippingState, 'Enter state.')) {return false;}
		else if (isEmpty(txtShippingCity, 'Enter city.')) {return false;}
		else if (isEmpty(txtShippingPostalCode, 'Enter postal/zip code.')) {return false;}
		else {return true;}
	}
}

/********************
ADMIN FUNCTIONS
********************/
//EVENT FUNCTIONS
function deleteEvent(eventId)
{
	if (confirm('Delete this event?'))
	{
		window.location.href = 'process.php?action=delete&eventId=' + eventId;
	}
}

//CATEGORY FUNCTIONS
function checkCategoryForm()
{
	with (window.document.frmCategory)
	{
		if (isEmpty(txtName, 'Enter category name'))
		{
			return;
		}
		else if (isEmpty(mtxDescription, 'Enter category description'))
		{
			return;
		}
		else
		{
			submit();
		}
	}
}

function deleteCategory(categoryId)
{
	if (confirm('Deleting this will move all products in it to the Admin cateogry. Delete this category anyway?'))
	{
		window.location.href = 'process.php?action=delete&categoryId=' + categoryId;
	}
}

function deleteImage(catId)
{
	if (confirm('Delete this image?'))
	{
		window.location.href = 'processCategory.php?action=deleteImage&catId=' + catId;
	}
}

//ORDER FUNCTIONS
function viewOrder()
{
	statusList = window.document.frmOrderList.cboOrderStatus;
	status     = statusList.options[statusList.selectedIndex].value;	
	
	if (status != '')
	{
		window.location.href = 'index.php?status=' + status;
	}
	else
	{
		window.location.href = 'index.php';
	}
}

function deleteOrder(cartId)
{
	if (confirm('Do you want to delete this order?'))
	{
		window.location.href = 'process.php?action=delete&cartId=' + cartId;
	}
}

//PRODUCT FUNCTIONS
function viewProductByCat()
{
	catList = window.document.frmListProductByCat.cboCategory;
	category = catList.options[catList.selectedIndex].value;	
	
	if (category != '')
	{
		window.location.href = 'index.php?catId=' + category;
	}
	else
	{
		window.location.href = 'index.php';
	}
}

function viewProductBySale()
{
	saleList = window.document.frmListProductBySale.cboSale;
	sale = saleList.options[saleList.selectedIndex].value;	
	
	if (sale != '')
	{
		window.location.href = 'index.php?sale=' + sale;
	}
	else
	{
		window.location.href = 'index.php';
	}
}

function checkAddProductForm()
{
	with (window.document.frmAddProduct)
	{
		if (cboCategory.selectedIndex == 0)
		{
			alert('Choose the product category');
			cboCategory.focus();
			return;
		}
		else if (isEmpty(txtName, 'Enter Product name'))
		{
			return;
		}
		else
		{
			submit();
		}
	}
}

function deleteProduct(productId)
{
	if (confirm('Delete this product?'))
	{
		window.location.href = 'process.php?action=delete&productId=' + productId;
	}
}

function deleteImage(productId)
{
	if (confirm('Delete this image?'))
	{
		window.location.href = 'process.php?action=deleteImage&productId=' + productId;
	}
}

//USER FUNCTIONS
function deleteUser(userId)
{
	if (confirm('Delete this user?'))
	{
		window.location.href = 'process.php?action=delete&userId=' + userId;
	}
}

//CLIENT FUNCTIONS
function deleteClient(clientId)
{
	if (confirm('Delete this client?'))
	{
		window.location.href = 'process.php?action=delete&clientId=' + clientId;
	}
}

//LINK FUNCTIONS
function deleteLink(linkId)
{
	if (confirm('Delete this link?'))
	{
		window.location.href = 'process.php?action=delete&linkId=' + id;
	}
}

//TESTIMONIAL FUNCTIONS
function deleteTestimonial(testimonialId)
{
	if (confirm('Delete this testimonail?'))
	{
		window.location.href = 'process.php?action=delete&testimonialId=' + id;
	}
}

//DISCOUNT FUNCTIONS
function deleteDiscount(discountId)
{
	if (confirm('Delete this discount?'))
	{
		window.location.href = 'process.php?action=delete&discountId=' + discountId;
	}
}

/********************
CLIENT FUNCTIONS
********************/
function deleteListItem(productId)
{
	if (confirm('Delete this item from your wishlist?'))
	{
		window.location.href = 'process.php?action=delete&productId=' + productId;
	}
}

function deleteCartItem(productId)
{
	if (confirm('Delete this item from your shopping cart?'))
	{
		window.location.href = 'process.php?action=delete&productId=' + productId;
	}
}

function removeDiscount()
{
	if (confirm('Remove this discount from the shopping cart?'))
	{
		window.location.href = 'process.php?action=remove';
	}
}

/*********************
GALLERY FUNCTIONS
*********************/
function deletePic(picId)
{
	if (confirm('Delete this photo from your gallery?'))
	{
		window.location.href = 'process.php?action=delete&picId=' + picId;
	}
}

// p=picture  c=caption  t=thumbnail
pic01.src = "p01.jpg";
pic02.src = "p02.jpg";
pic03.src = "p03.jpg";
pic04.src = "p04.jpg";
pic05.src = "p05.jpg";
pic06.src = "p06.jpg";
pic07.src = "p07.jpg";
pic08.src = "p08.jpg";
pic09.src = "p09.jpg";
pic10.src = "p10.jpg";
pic11.src = "p11.jpg";
pic12.src = "p12.jpg";
pic13.src = "p13.jpg";
pic13.src = "p14.jpg";
pic15.src = "p15.jpg";
pic16.src = "p16.jpg";
pic17.src = "p17.jpg";
pic18.src = "p18.jpg";
pic19.src = "p19.jpg";
pic20.src = "p20.jpg";
pic21.src = "p21.jpg";
pic22.src = "p22.jpg";
pic23.src = "p23.jpg";
pic24.src = "p24.jpg";
pic25.src = "p25.jpg";
pic26.src = "p26.jpg";
pic27.src = "p27.jpg";
pic28.src = "p28.jpg";
pic29.src = "p29.jpg";
pic30.src = "p30.jpg";

var pic01 = new Image();
var pic02 = new Image();
var pic03 = new Image();
var pic04 = new Image();
var pic05 = new Image();
var pic06 = new Image();
var pic07 = new Image();
var pic08 = new Image();
var pic09 = new Image();
var pic10 = new Image();
var pic11 = new Image();
var pic12 = new Image();
var pic13 = new Image();
var pic14 = new Image();
var pic15 = new Image();
var pic16 = new Image();
var pic17 = new Image();
var pic18 = new Image();
var pic19 = new Image();
var pic20 = new Image();
var pic21 = new Image();
var pic22 = new Image();
var pic23 = new Image();
var pic24 = new Image();
var pic25 = new Image();
var pic26 = new Image();
var pic27 = new Image();
var pic28 = new Image();
var pic29 = new Image();
var pic30 = new Image();

function doButtons(picimage)
{
document['picture'].src=picimage;
}

