/*
	created by		: Richard Clifford - Binary Ink Web Applications :: http://binaryink.com
	created on		: February 23, 2003
	purpose			: Tritech Fall Protection Systems website JavaScript file
*/

/*
	Animation class
	- creates an animation object complete with methods that:
		1. preload all the animation pictures
		2. Start, Stop and Zoom features and previous and next links
	...and properties to allow flexibility with:
	  	1. folder locations of images
		2. speed of animation		
		
		Arguments required:
			1. list_images -> an array of images for the animation
			2. img_location -> relative path to images
			3. img_tag -> <img> tag name for where the animation is to be displayed
			4. speed -> speed of animation
*/
	
	// Animation properties - set from query string, creates the animation object
	function get_slides(section,sub_section,page)
	{
		// test to see if slides are listed in querystring
		// if not redirect to get_slides.cfm to pick up the slides
		// if they are call animation initiation function
		var q_s = location.search.substring(1); // get rid of the ?
		if(q_s.indexOf('slides')==-1 && q_s.indexOf('keywords')==-1) location.href = '../includes/get_slides.cfm?section='+section+'&sub_section='+sub_section+'&page='+page ;	
		// keep keywords in search string
		else if(q_s.indexOf('slides')==-1 && q_s.indexOf('keywords')>-1)
		{
			location.href = '../includes/get_slides.cfm?section='+section+'&sub_section='+sub_section+'&page='+page+'&'+q_s ;
		}
		else
		{
			// isolate the "slides" variables from any other query string variables
			if(q_s.indexOf("&")!=-1)
			{
				s = q_s.split("&");
				s = s[0];
				s = s.split("=");
			}
			else s = q_s.split("=");
			list_images = s[1].split(",");
			// folder location of the images for the animation
			img_location = "../images/"+section+"/slide_show/"+sub_section+"/";
			img_tag = "animation"; // image tag name, where the animation will take place (keep it the same for all pages)
			speed = 1500; // speed of animation in milliseconds
			animate_it = new Obj_Animate(list_images,img_location,img_tag,speed);
			// if there are no slides set the image to blank.gif
			if(s[1]=="") document.images["animation"].src = "../images/presentation_layer/blank.gif";
		}
	}
	
	function Obj_Animate(list_images,img_location,img_tag,speed)
	{
		this.list_images = list_images; // images to include in the animation
		this.img_location = img_location; // folder location of images
		this.img_tag = img_tag // name of the image tag where the animation is being displayed
		this.speed = speed; // animation speed
		this.preload_it = new Array(); // array for preloading images
		this.preload_imgs = preload_imgs; // method to preload images
		this.animate = animate; // animation method
		this.start_it = start_it; // method to start animation
		this.stop_it = stop_it; // method to stop animation
		this.display_next = display_next; // method to display next picture in slide show (not animated)
		this.display_prev = display_prev; // method to display previous picture in slide show (not animated)
		this.display_first = display_first; // display first photo
		this.display_last = display_last; // display last photo
		this.zoom_it = zoom_it; // open up pop-up window of larger view of image (server side scripting required)
		this.current_img = current_img; // number value representing the index key of the current image
		this.next_img = next_img;  // number value representing the index key of the next image
		this.prev_img = prev_img; // number value representing the index key of the previous image
		this.stop_ID = 0; // clearInterval id
		
		// call preload method to preload images on object initialization
		this.preload_imgs();
		
		// set initial image
		this.display_first();
	}
	
	// method to preload images to be included in the animation
	function preload_imgs()
	{
		for(i=0;i<this.list_images.length;i++)
		{
			this.preload_it[i] = new Image();
			this.preload_it[i].src = this.img_location + this.list_images[i];
		}
	}
	
	// animation method
	function animate()
	{
		document.images[this.img_tag].src = this.img_location + this.list_images[this.next_img()];
	}
	
	// start animation
	function start_it()
	{
		this.stop_ID = setInterval("this.animate()",this.speed);
	}
	
	// stop animation
	function stop_it()
	{
		clearInterval(this.stop_ID);
	}
	
	// display next photo in slide show sequence
	function display_next()
	{
		document.images[this.img_tag].src = this.img_location + this.list_images[this.next_img()];
	}
	
	// display previous photo in slide show sequence
	function display_prev()
	{
		document.images[this.img_tag].src = this.img_location + this.list_images[this.prev_img()];
	}
	
	// display first photo in slide show sequence
	function display_first()
	{
		document.images[this.img_tag].src = this.img_location + this.list_images[0];
	}
	
	// display last photo in slide show sequence
	function display_last()
	{
		document.images[this.img_tag].src = this.img_location + this.list_images[this.list_images.length-1];
	}
	
	// zoom it opens up a pop-up window to display a larger version of the picture
	function zoom_it()
	{
		w = screen.width;
		h = screen.height;
		win_w = parseInt(document.images[this.img_tag].width)+25; // width of image plus some breathing room
		win_h = parseInt(document.images[this.img_tag].height)+25;
		img = this.next_img()-1;
		l = (w - win_w)/2;
		t = (h - win_h)/2;
		// pass image name and folder location to the pop-up window
		open('../includes/zoom.cfm?img_location='+this.img_location+'&current_img='+this.list_images[this.current_img()],'zoom_it','width='+win_w+',height='+win_h+',screenX='+l+',left='+l+',screenY='+t+',top='+t+',resizable=yes,scrollbars');
	}
	
	// determine current image
	function current_img()
	{
		for(i=0;i<this.list_images.length;i++)
		{
			var current_img_path = document.images[this.img_tag].src; // this gives the absolute path
			if(current_img_path.indexOf(this.list_images[i])!=-1) return i;
			else continue;
		}
		return 0;
	}
	
	// determine next image
	function next_img()
	{
		if((this.current_img()+1)<this.list_images.length) return this.current_img()+1;
		else return 0;
	}
	
	// determine previous image
	function prev_img()
	{
		if((this.current_img()-1)>=0) return this.current_img()-1;
		else return this.list_images.length-1; // return the last image in the list
	}
/*
	------------ Animation class ends --------------
*/

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

// mouse over function
function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}


// preload images
var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		tn1_on= newImage ("images/tn1_on.gif");
		tn2_on= newImage ("images/tn2_on.gif");
		tn3_on= newImage ("images/tn3_on.gif");
		tn4_on= newImage ("images/tn4_on.gif");
		tn5_on= newImage ("images/tn5_on.gif");
		tn6_on= newImage ("images/tn6_on.gif");
		sn1_on= newImage ("images/sn1_on.gif");
		sn2_on= newImage ("images/sn2_on.gif");
		sn3_on= newImage ("images/sn3_on.gif");
		sn4_on= newImage ("images/sn4_on.gif");
		fn1_on= newImage ("images/fn1_on.gif");
		fn2_on= newImage ("images/fn2_on.gif");
		preloadFlag = true;
	}
}

// validate keyword search (no special characters, etc.)
function search_validate(f)
{
	if(f.search.value=="")
	{
		alert("Please enter keywords to search for, or try visiting the Site Navigation!");
		f.search.focus();
		return false;
	}
}

// form validation for more info
function validate_more_info(f)
{
	if(f.nameTxt.value=="")
	{
		alert("Please enter your name.");
		f.nameTxt.focus();
		return false;
	}
	else if(f.titleTxt.value=="")
	{
		alert("Please enter your title.");
		f.titleTxt.focus();
		return false;
	}
	else if(f.companyTxt.value=="")
	{
		alert("Please enter your company name.");
		f.companyTxt.focus();
		return false;
	}
	else if(f.emailTxt.value=="" || f.emailTxt.value.indexOf('@')==-1 || f.emailTxt.value.indexOf('.')==-1)
	{
		alert("Please enter your email address.");
		f.emailTxt.focus();
		return false;
	}
	else if(f.addressTxt.value=="")
	{
		alert("Please enter your address.");
		f.addressTxt.focus();
		return false;
	}
	else if(f.cityTxt.value=="")
	{
		alert("Please enter your city.");
		f.cityTxt.focus();
		return false;
	}
	else if(f.provinceSlt.value=="--")
	{
		alert("Please enter your state/province.");
		f.provinceSlt.focus();
		return false;
	}
	else if(f.CountryCodeSlt.value=="--")
	{
		alert("Please enter your country.");
		f.CountryCodeSlt.focus();
		return false;
	}
	else if(f.postalCodeTxt.value=="")
	{
		alert("Please enter your zip/postal code.");
		f.postalCodeTxt.focus();
		return false;
	}
	else if(f.phoneTxt.value=="")
	{
		alert("Please enter your phone number.");
		f.phoneTxt.focus();
		return false;
	}
}

// validate login form
function validate_members_login(f) {
	if (f.login.value== "") {
		alert("Please enter your login name.");
		f.login.focus();
		return false;
	}
	
	if (f.password.value== "") {
		alert("Please enter your password.");
		f.password.focus();
		return false;
	}
}

// validate alter password form
// action determine whether they are changing their password, or just forgot it
function validate_alter_pass(f,action) {
	if (f.login.value== "") {
		alert("Please enter your login name.");
		f.login.focus();
		return false;
	}
	if(action = "change")
	{
		if (f.password.value== "") {
			alert("Please enter your password.");
			f.password.focus();
			return false;
		}
		else if(f.password.value != f.confirm_password.value)
		{
			alert("Your \"new password\" and \"confirm new password\" do not match, please re-enter your new password!");
			f.password.value = f.confirm_password.value = '';
			return false;
		}
	}
}

// alter password pop-up window
function alter_pass(action)
{
	w = screen.width;
	h = screen.height;
	win_w = 250;
	(action=='forget')? win_h = 200 : win_h = 250 ;
	l = (w - win_w)/2;
	t = (h - win_h)/2;
	open("login/pass_alter.cfm?action="+action,action+"_window","width="+win_w+",height="+win_h+",screenX="+l+",left="+l+",screenY="+t+",top="+t+",resizable=yes");
}

// alter password pop-up window size from email
function alter_pass_page()
{
	w = screen.width;
	h = screen.height;
	win_w = 300;
	win_h = 350 ;
	l = (w - win_w)/2;
	t = (h - win_h)/2;
	resizeTo(win_w,win_h);
	moveTo(l,t);
}

// member's site location pop window
function site_location(membersID,email)
{
	w = screen.width;
	h = screen.height;
	win_w = 300;
	win_h = 300 ;
	l = (w - win_w)/2;
	t = (h - win_h)/2;
	open('siteLocation.cfm?membersID='+membersID+'&email='+email,'getLocation','width='+win_w+',height='+win_h+',screenX='+l+',left='+l+',screenY='+t+',top='+t+',resizable=yes,scrollbars');
}

// validate administrators
function validate_administrators(f,the_form)
{
	if(f.login.value=="")
	{
		alert("Please enter a login value!");
		f.login.focus();
		return false;
	}
	// further validation for members form
	if(the_form=="members")
	{
		if(f.password.value=="")
		{
			alert("Please enter this member\'s password!");
			f.password.focus();
			return false;
		}
	}
}

// validate member intros
function validate_intro(f)
{
	if(f.intro.value=="")
	{
		alert("Please enter an introduction!");
		f.intro.focus();
		return false;
	}	
}

// empty province and city selects before submitting
function empty_submit(f)
{
	if(f=="customer") document.get_place.province.value='';
	if(f=="customer" || f=="province")document.get_place.city.value='';
	document.get_place.submit();
}

// pop-up help window for displaying how to enter a proper servicing Job
function show_nl(s)
{
	var w,h,l,t;
	w = 350
	h = 200;
	l = (screen.width - w)/2;
	t = (screen.height - h)/2;
	open('members/show_me.cfm?action='+s,s,'width='+w+',height='+h+',screenX='+l+',left='+l+',screenY='+t+',top='+t+',resizable=yes,scrollbars=yes');
}

// pop-up view picture window (for category pictures)
// pic is picture name; fold is folder name; level is the root level to the folder
function show_picture(pic,f,level)
{
	var w,h,l,t;
	w = 200
	h = 200;
	l = (screen.width - w)/2;
	t = (screen.height - h)/2;
	open(level+'show_picture.cfm?picture='+pic+'&f='+f,'show_picture','width='+w+',height='+h+',screenX='+l+',left='+l+',screenY='+t+',top='+t+',resizable=yes,scrollbars=yes');
}

// customer form validation
function validate_customer()
{
	var f;
	f = document.update_servicing;
	// customer
	if(f.serviceCustomerID.value=="" && f.customer.value=="")
	{
		alert("Please enter the customer's company name!");
		f.serviceCustomerID.focus();
		return false;
	}
	// check if the name entered in the other box already exists
	else if(f.serviceCustomerID.value=="" && f.customer.value!="")
	{
		var choices, other;
		 choices = f.serviceCustomerID.options;
		 other = f.customer.value;
		// loop through choices and test value entered in other box against pull-down menu
		for(i=0;i<choices.length;i++)
		{
			if(choices[i].text == other)
			{
				alert("The customer name which you have entered into the \"Other\" text box, already exists, please select it from the pull-down menu!");
				choices[i].selected = true;
				f.serviceCustomerID.focus();
				return false;
			}
		}
	}
	// city
	else if(f.city.value == "" && f.city_other.value=="")
	{
		alert("Please enter a city!");
		f.city.focus();
		return false;
	}
	// province
	else if(f.province.value == "" && f.province_other.value == "")
	{
		alert("Please enter a province or state!");
		f.province.focus();
		return false;
	}
}

// validate add terms
function validate_terms(f)
{
	if(f.term.value=="")
	{
		alert("Please enter the term!");
		f.term.focus();
		return false;
	}
	else if(f.definition.value=="")
	{
		alert("Please enter the definition!");
		f.definition.focus();
		return false;
	}
}

// validate Add Product category
function validate_product_category(f)
{
	if(f.category.value=="")
	{
		alert("Please enter a product category name!");
		f.category.focus();
		return false;
	}
	else if(f.description.value=="")
	{
		alert("Please enter your product category description!");
		f.description.focus();
		return false;
	}
	else if(f.description.value.length > 4000)
	{
		alert("Please note your product category description is too long \("+f.description.value.length+" characters\), the maximum length is 4000 characters!");
		f.description.focus();
		return false;
	}
	else if(f.picture.value=="")
	{
		alert("Please select a picture for this category!");
		f.picture.focus();
		return false;
	}
}

// check for categoryID before submitting
function validate_categoryID(f)
{
	if(f.categoryID.value=="")
	{
		alert("Please select a product category!");
		f.categoryID.focus();
		return false;
	}
}

// check for product code before submitting
function validate_productCode(f)
{
	if(f.productCode.value=="")
	{
		alert("Please select a product!");
		f.productCode.focus();
		return false;
	}
}

// validate product update form
function validate_product(f)
{
	if(f.categoryID.value=="")
	{
		alert("Please select a product category to list this product under!");
		f.new_categoryID.focus();
		return false;
	}	
	else if(f.productCode.value=="")
	{
		alert("Please enter a product code!");
		f.productCode.focus();
		return false;
	}
	else if(f.title.value=="")
	{
		alert("Please enter a product title!");
		f.title.focus();
		return false;
	}
	else if(f.description.value=="")
	{
		alert("Please enter a product description!");
		f.description.focus();
		return false;
	}
	else if(f.description.value.length > 4000)
	{
		alert("Please note your product description is too long \("+f.description.value.length+" characters\), the maximum length is 4000 characters!");
		f.description.focus();
		return false;
	}
	else if(f.shipping_weight.value=="" || isNaN(parseInt(f.shipping_weight.value)))
	{
		alert("Please enter the shipping weight of your product in \(lb\), do not enter the \(lb\) acronym as it will be automatically added, and use 0.00 format for weight less then one pound!");
		f.shipping_weight.focus();
		return false;
	}
	else if(f.CDN_price.value=="" || isNaN(parseInt(f.CDN_price.value)))
	{
		alert("Please enter the price of your product \($CDN\), do not enter a \($\) sign, and use 0.00 format for prices less then a $1.00!");
		f.CDN_price.focus();
		return false;
	}
	else if(f.picture.value=="")
	{
		alert("Please select a picture for this product!");
		f.picture.focus();
		return false;
	}
}

// validate currency exchange rate
function validate_exchg(f)
{
	if(f.country.value=="")
	{
		alert("Please enter the country that this exchange rate is for!");
		f.country.focus();
		return false;
	}
	else if(f.currencyExchg.value=="" || isNaN(parseInt(f.currencyExchg.value)))
	{
		alert("Please enter the currency exchange rate, do not enter a \($\) sign, and use 0.00 format for prices less then a $1.00!");
		f.currencyExchg.focus();
		return false;
	}
}

// validate shipping rate
function validate_shipping(f)
{
	if(f.shipping_weight.value=="" || isNaN(parseInt(f.shipping_weight.value)))
	{
		alert("Please enter the shipping weight, use 0.00 format for weight less then a lb!");
		f.shipping_weight.focus();
		return false;
	}
	else if(f.shipping_rate_CDN.value=="" || isNaN(parseInt(f.shipping_rate_CDN.value)))
	{
		alert("Please enter the CDN shipping rate, do not enter a \($\) sign, and use 0.00 format for prices less then a $1.00!");
		f.shipping_rate_CDN.focus();
		return false;
	}
	else if(f.shipping_rate_US.value=="" || isNaN(parseInt(f.shipping_rate_US.value)))
	{
		alert("Please enter the US shipping rate, do not enter a \($\) sign, and use 0.00 format for prices less then a $1.00!");
		f.shipping_rate_US.focus();
		return false;
	}
}

// validate_order(this)
function validate_qty(qty)
{
	if(isNaN(parseInt(qty)))
	{
		alert("Please enter a valid quantity!");
		return false;
	}
}

// validate search request
function validate_search(f)
{
	if(f.search.value.length < 2)
	{
		alert("Please make sure that your keyword string is at least 2 characters long!");
		f.search.focus();
		return false;
	}
}

// validate search engine indexing
function validate_search_index(f)
{
	if(f.section.value=="")
	{
		alert("Please indicate which section you are indexing!");
		f.section.focus();
		return false;
	}
	else if(f.search_page_title.value=="")
	{
		alert("Please indicate which page you are indexing \(i.e. Hazard Assessment\)!");
		f.search_page_title.focus();
		return false;
	}
	else if(f.search_file.value=="")
	{
		alert("Please indicate which page file you are indexing \(i.e. index.html\)!");
		f.search_file.focus();
		return false;
	}
	else if(f.content.value=="")
	{
		alert("Please copy the content from the appropriate section into the textarea box! You may copy right from the browser window and then paste the results into the content box.");
		f.content.focus();
		return false;
	}
}

// remember keywords by placing them into the search form field
function remember_search()
{
	// get query string value
	var q_s = location.search.substring(1); // get rid of the ?
	// check for multiple variables
	if(q_s.indexOf('&')!=-1)
	{
		// split variables
		q_s_array = q_s.split("&");
		// search for keywords<br>
		for(i in q_s_array)
		{
			j = q_s_array[i].split("=");
			if(j[0]=="keywords")
			{
				// put keywords into search form
				document.search_form.search.value = unescape(j[1]);
			}
			else continue;
		}
	}
	else
	{
		j = q_s.split("=");
		if(j[0]=="keywords")
		{
			// put keywords into search form
			document.search_form.search.value = unescape(j[1]);
		}
	}
}

// validate remove items from cart
function validate_remove_items(f)
{
	// check to make sure that items have been selected for deletion
	item_checked = false; // default value (no items checked)
	for(i=0;i<f.length;i++)
	{
		if(f.elements[i].type == "checkbox" && f.elements[i].checked) item_checked = true;
	}
	// alter confirmation prompt depending on if any checkboxes have been checked.
	if(item_checked)
	{
		if(confirm(remove_order)) f.submit();
	}
	else alert(no_items_checked);	
}

// validate shipping information
function validate_shipping_to(f,own_shipping)
{
	if(f.xxxName.value=="")
	{
		alert("Please enter the name of the person that this order will be shipped to!");
		f.xxxName.focus();
		return false;
	}
	else if(f.titleTxt.value=="")
	{
		alert("Please enter the title of the person that this order will be shipped to!");
		f.titleTxt.focus();
		return false;
	}
	else if(f.xxxCompany.value=="")
	{
		alert("Please enter the name of the company that this order will be shipped to!");
		f.xxxCompany.focus();
		return false;
	}
	else if(f.xxxEmail.value=="")
	{
		alert("Please enter the email address of the person that this order will be shipped to!");
		f.xxxEmail.focus();
		return false;
	}
	else if(f.xxxAddress.value=="")
	{
		alert("Please enter the address of the company that this order will be shipped to!");
		f.xxxAddress.focus();
		return false;
	}
	else if(f.xxxCity.value=="")
	{
		alert("Please enter the city of the company that this order will be shipped to!");
		f.xxxCity.focus();
		return false;
	}
	else if(f.xxxProvince.value=="--")
	{
		alert("Please enter the province of the company that this order will be shipped to!");
		f.xxxProvince.focus();
		return false;
	}
	else if(f.xxxCountry.value=="--")
	{
		alert("Please enter the country of the company that this order will be shipped to!");
		f.xxxCountry.focus();
		return false;
	}
	else if(f.xxxPostal.value=="")
	{
		alert("Please enter the postal / zip code of the company that this order will be shipped to!");
		f.xxxPostal.focus();
		return false;
	}
	else if(f.xxxPhone.value=="")
	{
		alert("Please enter the phone number of the company that this order will be shipped to!");
		f.xxxPhone.focus();
		return false;
	}
	else if(f.elements[10].checked==false && f.elements[11].checked==false)
	{
		alert("Is this shipping address the same as your billing address? Please indicate \"Yes\" or \"No\"!");
		f.elements[10].focus();
		return false;
	}
	else if(own_shipping==1 && f.courier.value=="")
	{
		alert("Please enter the name of your carrier and your account number!");
		f.courier.focus();
		return false;
	}
}

// change shipping carrier function
function change_carrier(si)
{
	if(si==3)
	{
		return confirm(my_carrier);
	} 
	else if(si) return true;
	else return false;
}

// reset form warning
var reset_warning = "Are you sure that you want to reset this form, all the fields will be returned to their default values, in some cases this value is empty!";

// cancel form warning
var cancel_form = "Are you sure that you want to cancel this action?";

// remove cart item warning
var remove_order = "Are you sure that you want to remove the \"checked\" items from your shopping cart?";

// remove cart (no items checked) warning
var no_items_checked = "Please indicate the items you would like to remove by placing a \"check\" next to the appropriate item!";

// cancel form warning
var back_form = "Are you sure that you want to go back?";

// delete admin warning
var del_admin = "Are you sure that you want to delete all the \"checked\" administrators?";

// update admin warning
var update_admin = "Are you sure that you want to update all the administrators' access information?";

// delete member warning
var del_member = "Are you sure that you want to delete all the \"checked\" members?";

// update admin warning
var update_member = "Are you sure that you want to update all the members' access information?";

// delete customer warning
var del_customer = "Are you sure that you want to permanently delete this customer?";

// delete terms warning
var del_term = "Are you sure that you want to permanently delete all the \"checked\" term(s)?";

// delete category warning
var del_category = "Are you sure that you want to permanently delete this category, you will need to re-assign any products listed under this category to one of the remaining categories?";

// delete product warning
var del_product = "Are you sure that you want to permanently delete this product?";

// use my carrier shipping warning
var my_carrier = "When choosing \"Use My Carrier\" you must have an existing account with a carrier that can provide service throughout North America. You will be prompted for your account number on the following page.\n\nIf you do \"not\" have an existing account click \"Cancel\" and select another option.";

// delete member service
var del_member_service = "Are you sure that you want to permanently delete this service?";