function dojoEnabled(){
	//return false;
	return !(isIe()=="1" && isMac()=="1");
}

function e(id)
{
	return document.getElementById(id)?document.getElementById(id):false;
} 

function isIe(){
	return (navigator.appName.indexOf ("Microsoft") != -1) ? "1" : "0";
}

function isMac(){
	return (navigator.appVersion.indexOf("Mac") != -1) ? "1" : "0";
}

function isSafari(){
	var str = navigator.userAgent.toLowerCase();
	str =str.indexOf("safari");
	return (str != -1) ? "1" : "0";
}


// Suppress errors??
function stopError() {
  return true;
}
window.onerror = stopError;

function confirmWhenPopupsBlockedOpenInCurrent(){

	return confirm("It appears that popups are blocked on your browser.\nClick 'OK' to open the requested page in the current window or click 'Cancel' and enable popups in your browser settings to continue.");
}

// ===========================================================================================
// This function forces a download



var now_downloading = false;
var now_downloading_url = "";
function forceDownload(url){
	if (!now_downloading){
		now_downloading_url = "";
		now_downloading_url = url;
		now_downloading = true;
		var frame_there_already = false
		if (inline_frame.areYouStillAround){
			if (inline_frame.areYouStillAround()){
				frame_there_already = true;
			}
		}
		if (frame_there_already){
			onDownloadPreLoaded();
		} else {
			inline_frame.location = "download_pre.asp";
		} 
	} else {
		// If the test frame is still there then unlock
		var bln_frame_there_still = false;
		if (inline_frame.areYouStillAround){
			if (inline_frame.areYouStillAround()){
				bln_frame_there_still = true;
			}
		}
		if (bln_frame_there_still){
			// Should be ok to unlock now ...
			now_downloading = "";
			now_downloading_url = "";
			forceDownload(url);
		} else {
				
		}
	}
}

function onDownloadPreLoaded(){
	if (now_downloading){
		// Load url in new window, wait 5 seconds to see if page is still there...
		inline_frame.location = now_downloading_url;
		window.setTimeout("checkIfDownloadWasSuccessful()" , 5000);	
	}
}

function checkIfDownloadWasSuccessful(){
	if (now_downloading){
		// If function still exists then download went successfully.
		var bln_force_download_success = false;
		//alert(inline_frame.location)
		
		if (inline_frame.areYouStillAround){
			if (inline_frame.areYouStillAround()){
				bln_force_download_success = true;
			}
		}
		if (bln_force_download_success) {
			// Ready for next download now.

		} else {
			// Open url in a new window.
			var newwin = window.open(now_downloading_url, "new_win");
			if (!newwin) {
				if (confirmWhenPopupsBlockedOpenInCurrent()){
					window.location = now_downloading_url;
				}
			}
		}
		now_downloading = false;
		now_downloading_url = "";
	
	}
}

// ===========================================================================================
var current_type = ""



function registerCurrentItem(str_type, obj_content){
	current_type = str_type;
	current_obj = new Object();
	for (var p in obj_content) {
		
		current_obj[p] = obj_content[p];
		//alert(	p + "=" + obj_content[p] + " : " + current_obj[p]);
	}
	// current_key = str_key;
}





function emailCurrentPageToFriend(){
	if (e("email-compose-popup") ){
		e("email-compose-popup").style.display = "";
		if (e("from_name")){
			e("from_name").focus();
		}
	}
}

function onAddedToSelection(bln_success){
	if (bln_success){
		showAddMessage("Item has been successully added to your selection.");	
	} else {
		showAddMessage("Unable to add to selection. Please try again.");				
	}
}



function addCurrentPageToSelection(){
	//alert("addCurrentPageToSelection()")
	var content = new Object()
	for (var p in  current_obj){
		content[p] = current_obj[p];
		//alert(	p + "=" + current_obj[p] + " : " + content[p]);	
	}
	
	if (dojoEnabled()){
				showAddMessage('Adding to selection...' , true );				
				//var content = {from_name : frm.from_name.value, from_address : frm.from_address.value, to_name : frm.to_name.value, to_address : frm.to_address.value , message : frm.message.value};
				dojo.io.bind({ 
					url: "gateway.asp?gateway_action=add_item_to_selection&type=" + current_type,
					content: content,
					handler: function(type, data, evt , content){
						//	alert("(debug) Got result: " + data);
						  if (type == "load"){
							  							
								if (data == "1"){
									onAddedToSelection(true);
								} else {
									onAddedToSelection(false);
								}							
								
								onAddedToSelection(true); 
								//////////////////////////////////
						  } else if(type == "error"){
							   onAddedToSelection(false);
						  } else { 
							   onAddedToSelection(false);						  
						  }
					},
					mimetype: "text/plain",		
					changeURL: false,
					transport: "XMLHTTPTransport"
				});
	} else {
		showAddMessage('Adding to selection...' , true );
    	self.setTimeout('onAddedToSelection(true)', 3000);
		 //
		var current_obj_qs = ""
		for (var p in current_obj){
			current_obj_qs += "&" + p + "=" +current_obj[p];
		}
		var url_str = "gateway.asp?gateway_action=add_item_to_selection&type=" + current_type + current_obj_qs
		//alert(url_str);
		window.open(url_str, 'inline_frame') ;
		
		
	}	
}

function spitOutDownload(str_pipe){

	str_pipe = String(str_pipe);
	var arr_pipe = str_pipe.split("|");
	
	var url_str = arr_pipe[0];
	var url_label = arr_pipe[1];
	
	forceDownload(url_str);
	
	showDownloadMessage("<a href='"+url_str+"'>Click here</a> if your download does not start automatically." , false);
	//'closeDownloadMessage();

}
function downloadCurrentPage(){
	
	showDownloadMessage("Generating download..." , false)
	
		
	var	str_item_key = "";
	var	str_group_key = "";
		
	for (var p in current_obj){
		str_item_key = current_obj[p];
	}
	str_group_key = current_type;
	
	//alert( str_group_key +":" + str_item_key);
	
	
	//var url_str = "gateway.asp?gateway_action=download_item&type=" + current_type + current_obj_qs	
		
		
						
	if (dojoEnabled()){
	
		content_obj = {item_key:str_item_key , group_key:str_group_key};
		//content: content,
		//alert("DOJO BIND" );
		dojo.io.bind({ 
			url: "gateway.asp?gateway_action=generate_pdf_for_download",
			content:  content_obj,
			handler: function(type, data, evt , content){
				var all_ok = false;
			  if (type == "load"){
					///////////////////////////////////////				  
					//alert("ERR1");
					
					
					var arr_tmp = String(data)
					arr_tmp = arr_tmp.split("<!--END_DOWNLOAD_LINKS//-->");
					arr_tmp = String(arr_tmp[0]);
					arr_tmp = arr_tmp.split("<!--BEGIN_DOWNLOAD_LINKS//-->");
					if (arr_tmp.length > 1 ){									

						all_ok = true;
						var str_content = String(arr_tmp[1]);
						
						spitOutDownload(str_content);				
					} else {
						all_ok = false;
					}	
			  } else if(type == "error"){
					 
					//alert("ERR4");
			  } else { 
					//alert("ERR5");
			  }
			  
			  if (!all_ok){
				  // alert("ERR6");
				 // showDownloadError();
				 showDownloadMessage("Unable to generate download. Please try again." , true);	
			  } else {
					//alert("ERR7");
			  }
			},
			mimetype: "text/plain",		
			changeURL: false,
			transport: "XMLHTTPTransport"
		});
	} else {
	//	alert("gateway.asp?gateway_action=generate_pdf_for_download&item_key="+str_item_key+"&group_key="+str_group_key)
		window.inline_frame.location = "gateway.asp?gateway_action=generate_pdf_for_download&item_key="+str_item_key+"&group_key="+str_group_key ;

	}
//	return false;		
	
//	var w = 658;
//	var h = 559; 
//	var wint = (screen.height-h)/2;
//	var winl = (screen.width-w)/2;
//	var settings  ='height='+h+',';
//	settings +='width='+w+',';
//	settings +='top='+wint+',';
///	settings +='left='+winl+',';
//	settings +='scrollbars=no,';
//	settings +='resizable=yes';
//	
//	var current_obj_qs = ""
//		
//	for (var p in current_obj){
//		current_obj_qs+= "&" + p + "=" +current_obj[p]
//	}
//	//
//	var url_str = "gateway.asp?gateway_action=download_item&type=" + current_type + current_obj_qs
//	win = window.open(url_str, '_blank', settings) ; //
//	if (win){
//		win.focus();
//	} else {
//		window.location = url_str;
// }
//		
			
}


//function stopError() {
//  alert("(!)")
//  return true;
//}
//
//window.onerror = stopError;
//
//try{
//   var x = NonExistentFunction();
//   document.write(x);
//}
//
//catch(err){
//   alert("(?)")
//}
//
//try{
//	
//}

/* -------------------------------------------------------------------------------------------
*/





function setImageSource(obj , image_src){
	obj.src = image_src;
}









function validateEmail(str){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(str)){
		return true;
	} else {		
		return false;
	}
}


/* MAIN NAV
---------------------------*/
var active_main_nav_src="";
function setMainNavActive(id) {
	if (!e('nav-img-' + id)){
		return;
	} else {
		var nav_image = e('nav-img-' + id);
	}
	nav_image.src = String(nav_image.src).split("off").join("on");
	active_main_nav_src = nav_image.src;
	// Set subnav
	if (e("subnav-" + id)){
		e("subnav-" + id).style.display = "";
	}
}

function isActive(img_obj){
	return (img_obj.src == active_main_nav_src);
}

function onMainNavOver(img_obj){
	if (!isActive(img_obj)){
		img_obj.src=String(img_obj.src).split('-off').join('-on');
	}
}

function onMainNavOut(img_obj){
	if (!isActive(img_obj)){
		img_obj.src=String(img_obj.src).split('-on').join('-off');
	}
}



/* SUB NAV
---------------------------*/
var active_sub_nav_src="";
function setSubNavActive(id) {
	if (!e('sub-img-' + id)){
		return;
	} else {
		var nav_image = e('sub-img-' + id);
	}
	nav_image.src = String(nav_image.src).split("off").join("on");
	active_sub_nav_src = nav_image.src;
}

function isSubActive(img_obj){
	return (img_obj.src == active_sub_nav_src);
}

function onSubNavOver(img_obj){
	if (!isSubActive(img_obj)){
		img_obj.src=String(img_obj.src).split('-off').join('-on');
	}
}

function onSubNavOut(img_obj){
	if (!isSubActive(img_obj)){
		img_obj.src=String(img_obj.src).split('-on').join('-off');
	}
}

/* FOOTER NAV
---------------------------*/
var active_footer_nav_src="";
function setFooterNavActive(id) {
	if (!e('footer-img-' + id)){
		return;
	} else {
		var nav_image = e('footer-img-' + id);
	}
	nav_image.src = String(nav_image.src).split("off").join("on");
	active_footer_nav_src = nav_image.src;
}

function isFooterActive(img_obj){
	return (img_obj.src == active_footer_nav_src);
}

function onFooterNavOver(img_obj){
	if (!isFooterActive(img_obj)){
		img_obj.src=String(img_obj.src).split('-off').join('-on');
	}
}

function onFooterNavOut(img_obj){
	if (!isFooterActive(img_obj)){
		img_obj.src=String(img_obj.src).split('-on').join('-off');
	}
}

/* GENERAL IMAGE ROLLOVER
-------------------------- */

function rollOver(img_obj){
	img_obj.src=String(img_obj.src).split('-off').join('-on');
}

function rollOut(img_obj){
	img_obj.src=String(img_obj.src).split('-on').join('-off');
}


/* POUPLATE id.innerHTML WITH obj.ALT
-------------------------- */

function rollOverPopulateIdWithAlt(img_obj, id){
	if (e(id)){
		e(id).innerHTML = img_obj.alt;
	}
}
function rollOutPopulateIdWithAlt(id){
	if (e(id)){
		e(id).innerHTML = "&nbsp;";
	}
}
/* WOULD YOU LIKE TO...
-------------------------- */
var current_icon_prompt_id = "icon-prompt-would-you"
function rollOverLikeTo(img_obj){
	/*
	if (!e("would-you-like-to-prompt")){
		return;				
	} else {
		var p = e("would-you-like-to-prompt");
	}
	
	if (s.split("-add-").length > 1){
		p.style.backgroundImage = "url(images/would-add-prompt.gif)";
	}
	if (s.split("-email-").length > 1){
		p.style.backgroundImage = "url(images/would-email-prompt.gif)";
	}
	if (s.split("-download-").length > 1){
		p.style.backgroundImage = "url(images/would-download-prompt.gif)";
	}
	*/
	if (e(current_icon_prompt_id)){
		e(current_icon_prompt_id).className = "icon_prompt_hide";
	}
	
	var s=img_obj.src;
	var str_id = "";
	if (s.split("-add-").length > 1){
		str_id = "icon-prompt-add";
	} else 	if (s.split("-contact-").length > 1){
		str_id = "icon-prompt-email";
	} else 	if (s.split("-brochure-").length > 1){
		str_id = "icon-prompt-order";
	} else 	if (s.split("-download-").length > 1){
		str_id = "icon-prompt-download";
	} else 	if (s.split("-email-").length > 1){
		str_id = "icon-prompt-friend";
	} 
	
	if (e(str_id)){
		e(str_id).className = "icon_prompt_show";
		current_icon_prompt_id = str_id
	}
	
	
}
 






function rollOutLikeTo(img_obj){

	if (e(current_icon_prompt_id)){
		e(current_icon_prompt_id).className = "icon_prompt_hide";
	}
	if (e("icon-prompt-would-you")){
		e("icon-prompt-would-you").className = "icon_prompt_show";
		current_icon_prompt_id = "icon-prompt-would-you"
	}
	/*if (!e("would-you-like-to-prompt")){
		return;				
	} else {
		var p = e("would-you-like-to-prompt")
	}
	p.style.backgroundImage= "none";
	*/
}


/* EMAIL COMPOSE
-------------------------- */





function cancelEmailPopup(){
	if (e("email-compose-popup") ){
		e("email-compose-popup").style.display = "none";
	}
}


var on_ok_focus_id = "";
function sendEmailPopup(){
	if (e("email-compose-popup") ){
		var frm = window.document.send_to_friend
		var msg_str = "";
		var ok = true
		if (frm.from_address.value.length<1 || !validateEmail(frm.from_address.value) ){
			ok = false;		 
			on_ok_focus_id =  "from_address";
		} else if ( frm.to_address.value.length<1 || !validateEmail(frm.to_address.value) ) {
			ok = false;
			on_ok_focus_id =  "to_address";
		} else {
			on_ok_focus_id = "";
		}		 
		
		//	alert("link: " + getCurrentURL());
		// ddd	return
		 
		if (!ok){
			msg_str = "Please enter valid email{br}addresses to continue";
			showEmailMessage(msg_str, true);
		} else {
			if (dojoEnabled()){
				
				showEmailMessage('Sending email...' , -1 );				
				var content = {from_name : frm.from_name.value, from_address : frm.from_address.value, to_name : frm.to_name.value, to_address : frm.to_address.value , message : frm.message.value, link : getCurrentURL()};
								
				// content["type"] = current_type;	
				// for (var p in  current_obj){
				// 		content[p] = current_obj[p];	
				// }
				
				dojo.io.bind({ 
					url: "gateway.asp?gateway_action=sendtofriend",
					content: content,
					handler: function(type, data, evt , content){
						  if (type == "load"){
								// outputString(data);
								if (data == "1"){
									onSendToFriendResult(true);
								} else {
									onSendToFriendResult(false);
								}
						  } else if(type == "error"){
								//outputString("ERROR :("); 
								onSendToFriendResult(false);
						  } else { 
								//outputString("???" + type);
								onSendToFriendResult(false);
						  }
					},
					mimetype: "text/plain",		
					changeURL: false,
					transport: "XMLHTTPTransport"
				});
			} else {
				showEmailMessage('Sending email...' , -1 );
				self.setTimeout('onSendToFriendResult(true)', 3000);
				frm.action = "gateway.asp?gateway_action=sendtofriend&link=" + getCurrentURL(); 
				//frm.target = "_blank";
				frm.submit();
			}
		}
	}
}

function onSendToFriendResult(success){
	if (success){

		if (bln_inline_mode) {
			var show_ok = true;
		} else {
			var show_ok = false;
		}
		var frm = window.document.send_to_friend
		frm.to_name.value = "";
		frm.to_address.value= "";
		frm.message.value= "";
		showEmailMessage('Thank you{br}Your message has been sent', show_ok);
	} else {
		showEmailMessage('Unable to send email. Please try again.' , true);
	}

}
 



function showDownloadMessage(str , hide_btn) {
	if (e("download-message-popup") && e("add_message_text" )){
		e("download-message-popup").style.display = "";
		e("download_message_text" ).innerHTML = str;
		//if (hide_btn && e("download-close-btn")){			
		//	e("download-close-btn").style.display = "none";
		//} else {
		e("download-close-btn").style.display = "";		
		//}
	}
}

function closeDownloadMessage(){
	if (e("download-message-popup") ){
		e("download-message-popup").style.display = "none";
	}
}


function showAddMessage(str , hide_btn) {
	if (e("add-message-popup") && e("add_message_text" )){
		e("add-message-popup").style.display = "";
		e("add_message_text" ).innerHTML = str;
		if (hide_btn && e("add-close-btn")){			
			e("add-close-btn").style.display = "none";
		} else {
			e("add-close-btn").style.display = "";		
		}
	}
}

function closeAddMessage(){
	if (e("add-message-popup") ){
		e("add-message-popup").style.display = "none";
	}
}

function showEmailMessage(str, ok_button){
	if (e("email-message-popup") && e("email_message_text") && e("email-compose-popup")){
		str = String(str).split("{").join("<");
		str = String(str).split("}").join(">");
		e("email_message_text").innerHTML = str;
		e("email-message-popup").style.display = "";
		e("email-compose-popup").style.display = "none";
		if (e("email-close-btn") && e("email-ok-btn")){
			if (ok_button == -1){
				e("email-close-btn").style.display = "none";
				e("email-ok-btn").style.display = "none";			
			} else if (ok_button){
				e("email-close-btn").style.display = "none";
				e("email-ok-btn").style.display = "";
			} else {
				e("email-close-btn").style.display = "";
				e("email-ok-btn").style.display = "none";		
			}
		}
	}
}

function onEmailOk(){
	if (e("email-message-popup") && e("email-compose-popup")){
		e("email-compose-popup").style.display = "";
		e("email-message-popup").style.display = "none"; 
		if (on_ok_focus_id != ""){
			if (e(on_ok_focus_id)){
				e(on_ok_focus_id).focus();
			}
		}
	}
}


function closeEmailMessage(){
	if (e("email-message-popup")){
		e("email-message-popup").style.display = "none";
		var frm = window.document.send_to_friend
		frm.to_name.value = "";
		frm.to_address.value= "";
		frm.message.value= "";
	}
}
/* PROD CAT ROLLOVERS
-------------------------- */
function onProdOver(id){
	if (e("prod_" + id)){
		e("prod_" + id).className = "prod_on";
	}
}
function onProdOut(id){
	if (e("prod_" + id)){
		e("prod_" + id).className = "prod_off";
	}
}


/* LAUNCH MOVIE POPUP
-------------------------- */


function launchMovie(innovation_anim_str_id){

	var w = 658;
	var h = 549;
	var wint = (screen.height-h)/2;
	var winl = (screen.width-w)/2;
	var settings  ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars=no,';
	settings +='resizable=yes';

	var url_str = 'movie.asp?content_id=' + innovation_anim_str_id;
	win = window.open(url_str, 'popup_win', settings) ; //
	if (win){
		win.focus();
	} else {
		window.location = url_str;
	}

}
 


function launchImage(image_name){

	var w = 658;
	var h = 559;
	var wint = (screen.height-h)/2;
	var winl = (screen.width-w)/2;
	var settings  ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars=no,';
	settings +='resizable=yes';

	var url_str = 'big_image.asp?image=' + image_name;
	win = window.open(url_str, 'popup_win', settings) ; //
	if (win){
		win.focus();
	} else {
		window.location = url_str;
	}

}

 function launchURL(url_str , w , h , str_use_scrollers ){
	var wint = (screen.height-h)/2;
	var winl = (screen.width-w)/2;
	var settings  ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars='+str_use_scrollers+',';
	settings +='resizable=yes';
	//var url_str = 'big_image.asp?image=' + image_name;
	win = window.open(url_str, 'popup_win', settings) ; //
	if (win){
		win.focus();
	} else {
		window.location = url_str;
	}
}

function launchImageCustomSize(image_name , w , h , str_use_scrollers ){
	var wint = (screen.height-h)/2;
	var winl = (screen.width-w)/2;
	var settings  ='height='+h+',';
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars='+str_use_scrollers+',';
	settings +='resizable=yes';
	var url_str = 'big_image.asp?image=' + image_name;
	win = window.open(url_str, 'popup_win', settings) ; //
	if (win){
		win.focus();
	} else {
		window.location = url_str;
	}
}


/* COOKIES
------------------------------------- */



function cookiesAreEnabled() {
  SetCookie( "check_if_cookies_are_enabled", "1" );
  if ( GetCookie( "check_if_cookies_are_enabled" ) ) {
    DeleteCookie( "check_if_cookies_are_enabled" );
    return true;
  } else {
    return false;
  }
}



function GetCookie( name ) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while ( i < clen ) {
    var j = i + alen;
    if ( document.cookie.substring(i, j) == arg ) return getCookieVal(j);
    i = document.cookie.indexOf( " ", i ) + 1;
    if ( i == 0 ) break;
  }
  return null;
}

function DeleteCookie( name, path, domain ) {
  if ( GetCookie( name ) ) {
    document.cookie = name + "=" +
    ( ( path ) ? "; path=" + path : "" ) +
    ( ( domain ) ? "; domain=" + domain : "" ) +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function SetCookie( name, value, expires, path, domain, secure ) {
  document.cookie = name + "=" + escape (value) +
  ( ( expires ) ? "; expires=" + expires.toGMTString() : "" ) +
  ( ( path ) ? "; path=" + path : "" ) +
  ( ( domain ) ? "; domain=" + domain : "" ) +
  ( ( secure ) ? "; secure" : "" );
}

/**
 *   Helper function for GetCookie()
 */
function getCookieVal( offset ) {
  var endstr = document.cookie.indexOf ( ";", offset );
  if ( endstr == -1 ) endstr = document.cookie.length;
  return unescape( document.cookie.substring( offset, endstr ) );
}


/* COOKIES (X)
------------------------------------- */






