/*
	Xstrata plc
	Common javascript functions

	Author: David Roessli (david dot roessli at cybmed dot com)
	Created on 2006-06-09
	Version: 1
	Revision: 0

	History:

		200801111455: amended form check
		200708061416: added popup class/function
		200606091508: genesis

	Terms
	You may adapt and learn from the elements of this CSS to use in your own
	projects, but you may not copy the design (the unique combination of images,
	colors, sizes, typography, and positioning) since this is copyright of
	Xstrata plc and may not be reproduced.

*/


/*
	::: preload images
*/
image = new Image();
image.src = "/img/ajax-loader_16.gif";

function checkorderform(form)
{
// see http://www.thesitewizard.com/archive/validation.shtml
// for an explanation of this script and how to use it on your
// own website

// ** START **
if (form.pub_order_name.value == "") {
	Effect.Shake('pub_order_name');
	alert( "Your name is missing.\nPlease try again." );
	form.pub_order_name.focus();
	return false ;
}
if (form.pub_order_email.value == "") {
	Effect.Shake('pub_order_email');
	alert( "Your e-mail address is missing.\nPlease try again." );
	form.pub_order_email.focus();
	return false ;
}
if (form.pub_order_address.value == "") {
	Effect.Shake('pub_order_address');
	alert( "Your postal address is missing.\nPlease  try again." );
	form.pub_order_address.focus();
	return false ;
}
// ** END **

showLoading();
return true ;
}

function checkdigitalorderform(form)
{
// see http://www.thesitewizard.com/archive/validation.shtml
// for an explanation of this script and how to use it on your
// own website

// ** START **
if (form.pre_order_name.value == "") {
	Effect.Shake('pre_order_name');
	alert( "Your name is missing.\nPlease try again." );
	form.pre_order_name.focus();
	return false ;
}
if (form.pre_order_email.value == "") {
	Effect.Shake('pre_order_email');
	alert( "Your e-mail address is missing.\nPlease try again." );
	form.pre_order_email.focus();
	return false ;
}
if (form.pre_order_usage.value == "") {
	Effect.Shake('pre_order_usage');
	alert( "The intended usage is missing.\nPlease  try again." );
	form.pre_order_usage.focus();
	return false ;
}
// ** END **

showLoading();
return true ;
}

function checkfeedbackform(form)
{
// see http://www.thesitewizard.com/archive/validation.shtml
// for an explanation of this script and how to use it on your
// own website

// ** START **
if (form.con_feedback_name.value == "") {
	Effect.Shake('con_feedback_name');
	alert( "Your name is missing.\nPlease try again." );
	form.con_feedback_name.focus();
	return false ;
}
if (form.con_feedback_email.value == "") {
	Effect.Shake('con_feedback_email');
	alert( "Your e-mail address is missing.\nPlease try again." );
	form.con_feedback_email.focus();
	return false ;
}
if (form.con_feedback_message.value == "") {
	Effect.Shake('con_feedback_message');
	alert( "Your feedback message is missing.\nPlease  try again." );
	form.con_feedback_message.focus();
	return false ;
}
if ((form.con_feedback_subject.value == "Other") && (form.con_feedback_subject_other.value == "")) {
	Effect.Shake('con_feedback_subject_other');
	alert( "Your feedback subject is missing.\nPlease  try again." );
	form.con_feedback_subject_other.focus();
	return false ;
}
// ** END **

showLoading();
return true ;
}
function checkregistrationform(form)
{
// see http://www.thesitewizard.com/archive/validation.shtml
// for an explanation of this script and how to use it on your
// own website

// ** START **
if (form.inv_registration_firstname.value == "") {
	Effect.Shake('inv_registration_firstname');
	alert( "Your firstname is missing.\nPlease try again." );
	form.inv_registration_firstname.focus();
	return false ;
}
if (form.inv_registration_lastname.value == "") {
	Effect.Shake('inv_registration_lastname');
	alert( "Your elastname is missing.\nPlease try again." );
	form.inv_registration_lastname.focus();
	return false ;
}
if (form.inv_registration_email.value == "") {
	Effect.Shake('inv_registration_email');
	alert( "Your e-mail address is missing.\nPlease  try again." );
	form.inv_registration_email.focus();
	return false ;
}
if (form.inv_registration_phone.value == "") {
	Effect.Shake('inv_registration_phone');
	alert( "Your phone is missing.\nPlease  try again." );
	form.inv_registration_phone.focus();
	return false ;
}
if (form.inv_registration_country.value == "") {
	Effect.Shake('inv_registration_country');
	alert( "Your country is missing.\nPlease  try again." );
	form.inv_registration_country.focus();
	return false ;
}
if (form.inv_registration_category.value == "") {
	Effect.Shake('inv_registration_category');
	alert( "Your category is missing.\nPlease  try again." );
	form.inv_registration_category.focus();
	return false ;
}
if ((form.inv_registration_printed_ar.checked == true) || (form.inv_registration_printed_ir.checked == true) || (form.inv_registration_printed_sr.checked == true)) {

	/* check if numeric value entered and is within bounds */
	
	var years = form.inv_registration_printed_years.value;
	var anum = /(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(years)) {
		
		if ((years > 0) && (years <= 10)) {
			
			checkyears = true;
		}
		else {
			
			checkyears = false;
		}
	}
	else {
		
		checkyears = false;
	}
	
	if (checkyears == false) {

		Effect.Shake('inv_registration_printed_years');
		alert( "The number of subscription years to our printed material is invalid (" + years + ").\nPlease select a value between 1 and 10 years." );
		form.inv_registration_printed_years.focus();
		return false ;
	}
}
/*
if (form.inv_registration_passwd1.value == "") {
	Effect.Shake('inv_registration_passwd1');
	alert( "Your password is missing.\nPlease  try again." );
	form.inv_registration_passwd1.focus();
	return false ;
}
if (form.inv_registration_passwd2.value == "") {
	Effect.Shake('inv_registration_passwd2');
	alert( "Your confirmation password is missing.\nPlease  try again." );
	form.inv_registration_passwd2.focus();
	return false ;
}
if (form.inv_registration_passwd1.value != form.inv_registration_passwd2.value) {
	Effect.Shake('inv_registration_passwd1');
	Effect.Shake('inv_registration_passwd2');
	alert( "Your passwords don't match is missing.\nPlease  try again." );
	form.inv_registration_passwd1.focus();
	return false ;
}
if (form.inv_registration_hint.value == "") {
	Effect.Shake('inv_registration_hint');
	alert( "Your personal question is missing.\nPlease  try again." );
	form.inv_registration_hint.focus();
	return false ;
}
if (form.inv_registration_answer.value == "") {
	Effect.Shake('inv_registration_answer');
	alert( "Your secret answer is missing.\nPlease  try again." );
	form.inv_registration_answer.focus();
	return false ;
}
*/
if (form.inv_registration_captcha.value == "") {
	Effect.Shake('inv_registration_captcha');
	alert( "The visual code is missing.\nPlease  try again." );
	form.inv_registration_captcha.focus();
	return false ;
}
// ** END **

showLoading();
return true ;
}

function showLoading() {
		
	if (!document.getElementById) return false;
	var button = document.getElementById("buttons");	// submit button
/*
	var image = document.getElementById("progress");	// animated GIF

	button.style.display = "none";	// hide button
	image.style.display = "block";	// show loading animated GIF
*/
	button.innerHTML = '<img src="/img/ajax-loader_16.gif" alt="Uploading file and related information to server.." title="Uploading file and related information to server.."  /> Uploading file and related information to server..';

	return true;
}

function showhide(id) {

	if (!document.getElementById) return true;			// do it the php way
	if (!document.getElementById(id)) return true;		// do it the php way
	if (Element.visible(id)) {

		new Effect.BlindUp(id);
	}
	else {
		new Effect.BlindDown(id);
	}

	return false;
}

function toggleView(commodity) {		
	/*
		Turn on/off commodities on map and in list (region view only)
	*/
	
	if (!document.getElementById) return true;			// do it the php way
	if (!document.getElementsByClassName) return true;	// do it the php way

	// (1) get all elements with the 'commodity' class
	commodities = document.getElementsByClassName(commodity);
	
	// (2) get all elements with the 'menu_commodity' id
	commodity_menu = $("menu_"+commodity);
	commodity_menu_class = Element.classNames(commodity_menu);	// this avoid removing+add the class
	
	// (3) test to see if the commodity is visible and change class of menu commodity accordingly
	if (Element.hasClassName(commodity_menu, commodity+"_off")) {
		
		// set to _on
		commodity_menu_class.set(commodity+"_on");	// see? did it in 1 go
		status = "1";
	}
	else {
		
		// set to _off
		commodity_menu_class.set(commodity+"_off");
		status = "0";
	}

	// (4) toggle operation points and list entries
	for(i=0; i<commodities.length; i++) {

		if (!Element.visible(commodities[i])) {

			// this can't be overriden, so I changed the code and added an inline style (sic!)
			// #map ul li a.copper, #extra dl dt.copper, #extra dl dd.copper { display:none; }

			new Effect.Appear(commodities[i].id);
			new Effect.Pulsate(commodities[i].id);
		}
		else {

			//new Effect.Puff(commodities[i].id); // weird on IE6/win
			new Effect.Fade(commodities[i].id);
		}
	}		

	// (5) update the session variable to reflect new commodity visibility
	new Ajax.Request("/ajax/ajax_commodity_status_toggle.php", { 
		onSuccess : function(resp) { 
			// alert("The response from the server is: " + resp.responseText);
			new Effect.Highlight(commodity_menu);	
		}, 
		onFailure : function(resp) { 
			alert("Oops, there's been an error.");
			return true; 	// don't do it the php way
		}, 
		parameters : commodity+"="+status 
	});

	return false;	// don't do it the php way
}

/*
	::: toggleItem OLD :::::::::::::::::::::::::::::::::::::::::::::
	:::	fct: toggles the display property
	:::	ori: dr
	::: Whos uses this function? AR04? SUS04?
*/
function toggleItem(box) {
    if (document.getElementById('info_'+box).style.display == 'none') {
        document.getElementById('info_'+box).style.display = 'block';
    } else {
        document.getElementById('info_'+box).style.display = 'none';
    }
}

/*
	::: doPopups :::::::::::::::::::::::::::::::::::::::::::::
	:::	fct: opens a link in a new browser window
	:::	ori: http://adactio.com/atmedia2005/
*/
function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick = function() {
        window.open(this.href);
        return false;
      }
    }
  }
}
/*
	::: doAudioPopups :::::::::::::::::::::::::::::::::::::::::::::
	:::	fct: opens a link in a new browser window
	:::	ori: http://adactio.com/atmedia2005/
	::: scrollbars=yes,resizable=yes,width=350,height=350
*/
function doAudioPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup_audio")) {
      links[i].onclick = function() {
        window.open(this.href, 'audio', 'scrollbars=yes,resizable=yes,width=500,height=400');
        return false;
      }
    }
  }
}
/*
	::: doWebcastPopups :::::::::::::::::::::::::::::::::::::::::::::
	:::	fct: opens a link in a new browser window
	:::	ori: http://adactio.com/atmedia2005/
	::: scrollbars=yes,resizable=yes,width=350,height=350
*/
function doWebcastPopups() {
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup_webcast")) {
      links[i].onclick = function() {
        window.open(this.href, 'webcast', 'scrollbars=yes,resizable=yes,width=775,height=660');
        return false;
      }
    }
  }
}
/*
	::: window.onload :::::::::::::::::::::::::::::::::::::::::::::
*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

//addLoadEvent(prepareHovers);
addLoadEvent(doPopups);
addLoadEvent(doAudioPopups);
addLoadEvent(doWebcastPopups);
