/*
	created by		: Richard Clifford, http://binaryink.com
	created on		: December 1, 2003
	purpose			: Main JavaScript file for WoWcompany.net
*/

/* onload function, calls all onload functions */
function getOnload(rootlevel)
{
	preloadImages(rootlevel);
}

// swap image - rollover effects for navigational elements
function swapImg(rootlevel,tag,img)
{
	document.images[tag].src = rootlevel+"images/navigation/"+img;
}
// swap image - rollover effects for other elements
function swapImgAll(rootlevel,foldr,tag,img)
{
	document.images[tag].src = rootlevel+"images/"+foldr+"/"+img;
}
// preload navigation rollovers
function preloadImages(rootlevel) 
{
	var preload_imgs = new Array("trainingPrograms_over.gif","group_over.gif","description_over.gif","course_over.gif","printable_over.gif","corporate_over.gif","personal_over.gif","eventsSchedules_over.gif","upcoming_over.gif","schedules_over.gif","wowRoom_over.gif","news_over.gif","talking_over.gif","stories_over.gif","wowStore_over.gif","clothing_over.gif","register_over.gif","contact_over.gif","about_over.gif","contactO_over.gif");
	var load_imgs = new Array();
	for(i=0; i < preload_imgs.length;i++)
	{
		load_imgs[i] = new Image();
		load_imgs[i].src = rootlevel+"images/navigation/"+preload_imgs[i];
	}
	// non navigation
	var nonNav = new Array("wowStore/buyOver.gif");
	var loadNonNav = new Array();
	for(i=0; i < nonNav.length;i++)
	{
		loadNonNav[i] = new Image();
		loadNonNav[i].src = rootlevel+"images/"+nonNav[i];
	}
}
// open pop-up window for event photos
function pop_photo(rootlevel,photo)
{
	open(rootlevel+'eventPhotos/photo.php?photo='+photo,'popuppick','width=400,height=400');
}
//  login pop-up window
// rootlevel indicates how many ../../ to get to the root folder
// url is either login.php or login.php?error_msg=logout&email=member
function loginPopUp(rootlevel,url)
{
	w = 410;
	h = 500;
	x = (screen.width - w)/2;
	y = (screen.height - h)/2;
	open(rootlevel+"login/"+url,"login","width="+w+",height="+h+",screenX="+x+",left="+x+",screenY="+y+",top="+y+",resizable,scrollbars");
}
// submit training programs form
function orderNow(f)
{
	if(isNaN(parseInt(document.forms[f].quantity.value)))
	{
		alert("Please enter a valid quantity!");
		document.forms[f].quantity.value = 1;
		return false;
	} 
	else document.forms[f].submit();
}
// shopping cart validation (for deleting cart items)
function validate_cart(f)
{
	if(isNaN(f.quantity.value) || f.quantity.value < 1)
	{
		alert("Please enter a valid quantity!");
		location.href='shopping.php';
		return false;		
	}
}

// training schedules pop-up window
function show_window(item)
{
	open(item,"login","width=410,height=500,screenX=150,left=150,screenY=150,top=150,resizable,scrollbars")
}

// validate shipping information
function validateShipping(f)
{
	if(f.firstName.value=="")
	{
		alert("Please enter your first name!");
		f.firstName.focus();
		return false;
	}
	else if(f.lastName.value=="")
	{
		alert("Please enter your last name!");
		f.lastName.focus();
		return false;
	}
	else if(f.email.value=="" || f.email.value.indexOf("@") == -1 || f.email.value.indexOf(".") == -1)
	{
		alert("Please enter a valid email address!");
		f.email.focus();
		return false;
	}
	else if(f.company.value=="")
	{
		alert("Please enter your company!");
		f.company.focus();
		return false;
	}
	else if(f.address.value=="")
	{
		alert("Please enter your address!");
		f.address.focus();
		return false;
	}
	else if(f.city.value=="")
	{
		alert("Please enter your city!");
		f.city.focus();
		return false;
	}
	else if(f.province.value=="")
	{
		alert("Please enter your province / state!");
		f.province.focus();
		return false;
	}
	else if(f.country.value=="")
	{
		alert("Please enter your country!");
		f.country.focus();
		return false;
	}
	else if(f.postal.value=="")
	{
		alert("Please enter your postal / zip code!");
		f.postal.focus();
		return false;
	}
}

// email order to wow
function redirectProcessForm()
{
	document.forms["process_payment"].action = "thank_you.php";	
}

/*
Manage Maincategories
*/

// inserts the maincategory name and ID into the addItemText or addItemID
function insertMainCategory(f,s)
{
	// reload page passing selected value to the page as a querystring
	maincategoryID = f.mainCategorySelect.options[s].value;
	location.href = "manage_maincategory.php?maincategoryID="+maincategoryID;
}

/*
Manage Productlines
*/

// inserts the productline name and ID into the addItemText or addItemID
function insertProductline(f)
{
	
	var thisIndex = f.mainCategorySelect.options.selectedIndex;
	var thisText = f.mainCategorySelect.options[thisIndex].text;
	if (thisText == "Select...")
	{
		alert("Please choose a Maincategory first.");
		return false;
	}
			
	thisIndex = f.categorySelect.options.selectedIndex;
	thisText = f.categorySelect.options[thisIndex].text;
	var thisID = f.categorySelect.options[thisIndex].value;
	if (thisText != "Select...")
	{
		f.addItemText.value = thisText;
		f.addItemID.value = thisID;
	}
	else
	{
		f.addItemText.value = "";
		f.addItemID.value = "";
	}
}
	
/*
		Manage Products
*/

// Confirm the deletion of an item
function delete_category_confirm()
{
	if (!confirm("Are you sure you want to delete this?")) return false;
}

// Confirm the deletion of an item
function delete_item_confirm(msg)
{
	if (!confirm(msg)) return false;
}

// Populate the pulldowns pending other pulldown selections on the page
function refresh_category_pulldowns(f)
{
	var parameters = "";
	// Determine the maincategory to view
	if (f.mainCategorySelect.value != 'select')
	{
		var mainCategorySelect = f.mainCategorySelect.value;
		parameters += 'mainCategorySelect='+mainCategorySelect;
	}
	
	// Only add the productline in the URL if we are on the manage_productline or manage_product page
	if (f.name == 'manage_productline' || f.name == 'manage_product')
	{
		if (f.categorySelect.value != 'select')
		{
			var categorySelect = f.categorySelect.value;
			if (parameters.length ==0)
			{
				parameters += 'categorySelect='+categorySelect;
			}
			else
			{
				parameters += '&categorySelect='+categorySelect;
			}
		}
	}
	
	// Only add the products in the URL if we are on the manage_product page
	if (f.name == 'manage_product')
	{
		if (f.productSelect.value != 'select')
		{
			var productSelect = f.productSelect.value;
			if (parameters.length ==0)
			{
				parameters += 'productSelect='+productSelect;
			}
			else
			{
				parameters += '&productSelect='+productSelect;
			}
		}
	}
	location.replace(f.name+'.php?'+parameters);
}

// Require the user to select a maincategory and a productline first
function confirmCategorySelection(f)
{
	// Maincategory
	if (f.mainCategorySelect.value == '')
	{
		alert("Please choose a Maincategory first.");
		f.mainCategorySelect.focus();
		return false;
	}
	
	// Productline
	if (f.categorySelect.value == '')
	{
		alert("Please choose a Productline first.");
		f.categorySelect.focus();
		return false;
	}
}


// Validate manage_image
function validate_manage_image(f)
{
	var thisIndex = f.addItemText;
	var thisText = f.addItemText.value;
	var thisID = f.addItemID;
	if (thisText == "Select..." && f.addItemText.value == '')
	{
		alert("Please choose an option from the list.");
		f.categorySelect.focus();
		return false;
	}
}

// inserts the size name and ID into the addItemText or addItemID
function insertSize(f)
{
	var thisIndex = f.sizeSelect.options.selectedIndex;
	var thisText = f.sizeSelect.options[thisIndex].text;
	var thisID = f.sizeSelect.options[thisIndex].value;
	f.addItemText.value = thisText;
	f.addItemID.value = thisID;
}

// inserts the color name and ID into the addItemText or addItemID
function insertColor(f)
{
	var thisIndex = f.colorSelect.options.selectedIndex;
	var thisText = f.colorSelect.options[thisIndex].text;
	var thisID = f.colorSelect.options[thisIndex].value;
	f.addItemText.value = thisText;
	f.addItemID.value = thisID;
}

// Validate manage_color
function validate_manage_color(f)
{
	if (f.addItemText.value == '')
	{
		alert("Please choose an option from the list.");
		return false;
	}
}

// Validate manage_size
function validate_manage_size(f)
{
	if (f.addItemText.value == '')
	{
		alert("Please choose an option from the list.");
		return false;
	}
}

// open pop-up window
function openNewWindow(win,w,h)
{
	x = (screen.width - w)/2;
	y = (screen.height - h)/2;
	open(win,'new_win','screenX='+x+',left='+x+',screenY='+y+',top='+y+',width='+w+',height='+h+',scrollbars');
}

function selectOrRemoveListItem(f, myAction, itemType)
{
	if (myAction == 'select' && itemType =='size')
	{
		if (f.sizeSelect.selectedIndex != -1)
		{
			// get the index value of the selected item
			var thisIndex = f.sizeSelect.selectedIndex;
			// get the text of that selected item
			var thisOption = f.sizeSelect.options[thisIndex].text
			var thisID = f.sizeSelect.options[thisIndex].value
			// get the next index value for the selected value
			var next_index = f.sizeSelected.length;
			// add this index and value to the 'selected' list
			f.sizeSelected.options[next_index] = new Option(thisOption, thisID);
		}
	}
	else if (myAction == 'remove' && itemType =='size')
	{
		if (f.sizeSelected.selectedIndex != -1)
		{
			// get the index value of the selected item
			var thisIndex = f.sizeSelected.selectedIndex;
			// get the text of that selected item
			var thisOption = f.sizeSelected.options[thisIndex].text
			// remove this option from the list
			f.sizeSelected.options[thisIndex] = null;
		}
	}
	else if (myAction == 'select' && itemType =='color')
	{
		if (f.colorSelect.selectedIndex != -1)
		{
			// get the index value of the selected item
			var thisIndex = f.colorSelect.selectedIndex;
			// get the text of that selected item
			var thisOption = f.colorSelect.options[thisIndex].text
			var thisID = f.colorSelect.options[thisIndex].value
			// get the next index value for the selected value
			var next_index = f.colorSelected.length;
			// add this index and value to the 'selected' list
			f.colorSelected.options[next_index] = new Option(thisOption, thisID);
		}
	}
	else if (myAction == 'remove' && itemType =='color')
	{
		if (f.colorSelected.selectedIndex != -1)
		{
			// get the index value of the selected item
			var thisIndex = f.colorSelected.selectedIndex;
			// get the text of that selected item
			var thisOption = f.colorSelected.options[thisIndex].text
			// remove this option from the list
			f.colorSelected.options[thisIndex] = null;
		}
	}
}
// vaidate the manage_category input
function validate_manage_product(f)
{
	if (f.name == 'manage_product')
	{
		if (f.title.value =='')
		{
			alert("Please enter a valid 'Title'.");
			f.title.focus();
			return false;
		}
				
		if (isNaN(f.price.value) || f.price.value < 0 || f.price.value =='')
		{
			alert("Please enter a valid 'Price'.");
			f.price.focus();
			return false;
		}
		
		if (isNaN(f.weight.value) || f.weight.value < 0 || f.weight.value =='')
		{
			alert("Please enter a valid 'Weight'.");
			f.weight.focus();
			return false;
		}
		
		
		if (isNaN(f.carton_length.value) || f.carton_length.value < 0 || f.carton_length.value =='')
		{
			alert("Please enter a valid 'Carton Length'.");
			f.carton_length.focus();
			return false;
		}
		
		if (isNaN(f.carton_width.value) || f.carton_width.value < 0 || f.carton_width.value =='')
		{
			alert("Please enter a valid 'Carton Width'.");
			f.carton_width.focus();
			return false;
		}
		
		if (isNaN(f.carton_height.value) || f.carton_height.value < 0 || f.carton_height.value =='')
		{
			alert("Please enter a valid 'Carton Height'.");
			f.carton_height.focus();
			return false;
		}
		
		// 	Make sure the length is greather than the width as per Canada Post
		if (parseFloat(f.carton_length.value) <= parseFloat(f.carton_width.value))
		{
			alert("Please make sure the shipping carton length is greater than the width.");
			f.carton_length.focus();
			return false;
		}
		
		// 	Make sure the width is greather than the height as per Canada Post
		if (parseFloat(f.carton_width.value) <= parseFloat(f.carton_height.value))
		{
			alert("Please make sure the shipping carton width is greater than the height.");
			f.carton_width.focus();
			return false;
		}
	}
	
	if (f.name == 'manage_maincategory')
	{
		var thisIndex = f.mainCategorySelect.options.selectedIndex;
		var thisText = f.mainCategorySelect.options[thisIndex].text;
		var thisID = f.mainCategorySelect.options[thisIndex].value;
		if (thisText == "Select..." && f.addItemText.value == '')
		{
			alert("Please choose an option from the list.");
			f.mainCategorySelect.focus();
			return false;
		}
	}
	
	if (f.name == 'manage_productline')
	{
		// Make sure a maincategory is selected first
		if (f.mainCategorySelect.value == 'select')
		{
			alert("Please choose a Maincategory first.");
			return false;
		}
		
		// Make sure an option from the list is chosen first
		var thisIndex = f.categorySelect.options.selectedIndex;
		var thisText = f.categorySelect.options[thisIndex].text;
		var thisID = f.categorySelect.options[thisIndex].value;
		if (thisText == "Select..." && f.addItemText.value == '')
		{
			alert("Please choose an option from the list.");
			f.categorySelect.focus();
			return false;
		}
	}
	
	if (f.name != 'manage_maincategory' && f.name != 'manage_productline')
	{
		// Make a list of all the items selected by the user and store this list in a hidden field
		var numListItems = f.colorSelected.options.length;
		for (i=0;i<numListItems;i++)
		{
			if (i == 0)
			{
				f.colorSelections.value = f.colorSelected.options[i].value;
			}
			else
			{
				f.colorSelections.value = f.colorSelections.value + ', ' + f.colorSelected.options[i].value;
			}
		}
		
		// Make a list of all the items selected by the user and store this list in a hidden field
		var numListItems = f.sizeSelected.options.length;
		for (i=0;i<numListItems;i++)
		{
			if (i == 0)
			{
				f.sizeSelections.value = f.sizeSelected.options[i].value;
			}
			else
			{
				f.sizeSelections.value = f.sizeSelections.value + ', ' + f.sizeSelected.options[i].value;
			}
		}
	}
}

function refresh_category_pulldowns_manage_products(f)
{
	var parameters = "";
	
	// Determine the maincategory to view
	if (f.mainCategorySelect.value != 'select' && f.mainCategorySelect.selectedIndex != -1)
	{
		var mainCategorySelect = f.mainCategorySelect.value;
		if (parameters.length ==0)
		{
			parameters += 'mainCategorySelect='+mainCategorySelect;
		}
		else
		{
			parameters += '&mainCategorySelect='+mainCategorySelect;
		}
	}
		
	// Determine the productline to view
	if (f.categorySelect.selectedIndex != -1)
	{
		var categorySelect = f.categorySelect.value;
		if (parameters.length ==0)
		{
			parameters += 'categorySelect='+categorySelect;
		}
		else
		{
			parameters += '&categorySelect='+categorySelect;
		}
	}
	
	// Determine the product to view
	if (f.productSelect.value != 'select' && f.productSelect.selectedIndex != -1)
	{
		var productSelect = f.productSelect.value;
		if (parameters.length ==0)
		{
			parameters += 'productSelect='+productSelect;
		}
		else
		{
			parameters += '&productSelect='+productSelect;
		}
	}
	location.replace(f.name+'.php?'+parameters);
}

// Changes the product image source on the fly 
function swapProductImages(f, imageType, imageSelectTagName)
{
	// f - form name
	// imageType - 'fullsize' or 'thumbnail'
	// imageSelectTagName - 'fullsizeImageSelect' or 'thumbnailImageSelect'
	
	// Determine the index, text, and ID of the image
	var thisIndex = eval("f." + imageSelectTagName + ".options.selectedIndex");
	var thisText = eval("f." + imageSelectTagName + ".options[thisIndex].text");
	var thisID = eval("f." + imageSelectTagName + ".options[thisIndex].value");
	// Place the image
	var myImage = new Image;
	if (thisID != '')
	{
		myImage.src = "../../images/products/"+imageType+"/" + thisText;
		document.images[imageType].src = myImage.src;
	}
	else
	{
		// Use the noimage.jpg if there is one
		myImage.src = "../../images/products/"+imageType+"/noimage.jpg";
		document.images[imageType].src = myImage.src;
	}
}
/* 
	Manage Products Ends
*/

/* 
	Manage Services Begins
*/

// validate the manage_service.php input
function validate_manage_service(f)
{
	
	if (f.categorySelect.value == '')
	{
		alert("Please select a Productline first.");
		f.categorySelect.focus();
		return false;
	}
	
	if (f.title.value =='')
	{
		alert("Please enter a valid 'Title'.");
		f.title.focus();
		return false;
	}
	
	if (f.event_time.value =='')
	{
		alert("Please enter a valid 'Time'.");
		f.event_time.focus();
		return false;
	}
	
	if (f.duration.value =='')
	{
		alert("Please enter a valid 'Duration'.");
		f.duration.focus();
		return false;
	}
	 
	if (f.location.value =='')
	{
		alert("Please enter a valid 'Location'.");
		f.location.focus();
		return false;
	}
		
	if (isNaN(f.cost_date_before.value) || f.cost_date_before.value < 0 || f.cost_date_before.value =='')
	{
		alert("Please enter a valid 'Cost Date Before'.");
		f.cost_date_before.focus();
		return false;
	}
	
	if (isNaN(f.cost_date_after.value) || f.cost_date_after.value < 0 || f.cost_date_after.value =='')
	{
		alert("Please enter a valid 'Cost Date After'.");
		f.cost_date_after.focus();
		return false;
	}
	
	if (isNaN(f.spaces_available.value) || f.spaces_available.value < 0)
	{
		alert("Please enter a valid 'Spaces Available' amount.");
		f.spaces_available.focus();
		return false;
	}
	
	if (isNaN(f.capacity.value) || f.capacity.value < 0)
	{
		alert("Please enter a  valid 'Capacity'.");
		f.spaces_available.focus();
		return false;
	}
}

// Require the user to select a maincategory and a productline first
function confirmCategorySelection_Events(f)
{
	// Productline
	if (f.categorySelect.value == '')
	{
		alert("Please choose a Productline first.");
		f.categorySelect.focus();
		return false;
	}
}

function refresh_category_pulldowns_manage_services(f)
{
	var parameters = "";
	// Determine the productline to view
	if (f.categorySelect.selectedIndex != -1)
	{
		var categorySelect = f.categorySelect.value;
		if (parameters.length ==0)
		{
			parameters += 'categorySelect='+categorySelect;
		}
		else
		{
			parameters += '&categorySelect='+categorySelect;
		}
	}
	
	// Determine the service to view
	if (f.serviceSelect.value != 'select' && f.serviceSelect.selectedIndex != -1)
	{
		var serviceSelect = f.serviceSelect.value;
		if (parameters.length ==0)
		{
			parameters += 'serviceSelect='+serviceSelect;
		}
		else
		{
			parameters += '&serviceSelect='+serviceSelect;
		}
	}
	
	location.replace(f.name+'.php?'+parameters);
}

/* 
	Manage Services Ends
*/


// Remove all the items from the parent select list for replacing with an updated list
function refresh_color_list_on_parent(chgsText,chgsValue,page,selectbox)
{
	// Remove the items from the parent list
	var itemsRemaining = true;
	for(i=document.forms[page].elements[selectbox].options.length;i>=0;i--)
	{
		// setting the value to null erases the array value
		document.forms[page].elements[selectbox].options[i]=null;
	}
	
	// Add the items from the color list on this page to the parent color list
	for (i=0;i<chgsValue.length;i++)
	{
		thisOption = chgsText[i];
		thisValue = chgsValue[i];
		document.forms[page].elements[selectbox].options[i] = new Option(thisOption, thisValue);
	}
	
}


// private messages warning (message board)
var msg = "This topic is private, for members only. You must log in before you can view private topics!";

// delete shopping cart items warning
var deletAll = "Are you sure that you want to delete all of these items?";