/* 
  Global JavaScript File
  It contains the commum script for the whole web site.
  Author    : Luciano Sampaio Martins de Souza
  Date      : 08/01/2007
  Version   : 1.0
  Copyright : ApolloMedia Inc
  Web Site  : www.apollomedia.ca
*/
// it says to the browser if any error occurs in the page, call this method.
window.onerror = HandleAllErrors;

// Check if is Internet Explorer IE ou FireFox FF.
var isIE = ( navigator.appName.indexOf("Netscape") === -1 );

// Handle the errors and don't stop the flow of the page to keep going.
function HandleAllErrors(msg, url, line)
{
	ShowErrorMessage(msg, url, line);
	return true;
}
// Show the message to the user.
function ShowErrorMessage(msg, url, line)
{
  alert("Error: " + msg + "\nUrl: "  + url + "\nLine: " + line);		
}
/**
 * @projectDescription Sets the focus on the element passed as parameter.
 * Supported: IE 4.0+, Mozilla 1.0+, Netscape 3.0+, Opera 7.0+, Safari 1.0+
 * Example:
 * SetFocus("Email");
 * @param {String} Id
 * @author 	Luciano Sampaio lsampaio@apollomedia.ca
 * @version 	0.1
 */
function SetFocus(Id)
{ 
	var Obj = GetObj(Id);
	if (Obj) 
	{
		Obj.focus();
	}
}

/**
 * @projectDescription Sets the click on the element passed as parameter.
 * Supported: IE 4.0+, Mozilla 1.0+, Netscape 3.0+, Opera 7.0+, Safari 1.0+
 * Example:
 * SetFocus("Email");
 * @param {String} Id
 * @author 	Luciano Sampaio lsampaio@apollomedia.ca
 * @version 	0.1
 */
function SetClick(Id)
{ 
	var Obj = GetObj(Id);
	if (Obj) 
	{
		Obj.click();
	}
}

// Check to see the all the required fields were filled by the user.
function CheckRequiredFields()
{
	// It needs to go through the whole collection.
  for (var I = 0; I < arguments.length; I++)
	{
		var ElementId = arguments[I];
		if (document.getElementById(ElementId))
		{
			if ( (document.getElementById(ElementId).style.display != "none") && (document.getElementById(ElementId).value === "") )
			{
				// Show a message to inform the user that this field is required.
				ShowMSG("Error" + ElementId, "This is a required field.");
				// Set the focus on the element that was not filled.
				SetFocus(ElementId)
				// It means this field was not filled.
				return false;
			}
			else
			{
				// Show a message to inform the user that this field is required.
				ShowMSG("Error" + ElementId, "");
			}
		}
  }
	// If it reachs here it means all the fields were filled by the user.
	return true;
}
// Show a message to the user.
function ShowMSG(Id, MSG)
{
  if (document.getElementById(Id))
  {
    document.getElementById(Id).innerHTML = MSG;
  }
}

/**
 * @projectDescription Move the focus to the next field of the form.
 * Supported: IE 4.0+, Mozilla 1.0+, Netscape 3.0+, Opera 7.0+, Safari 1.0+
 * Example:
 * onkeydown="return NextField(event, 'txtEmail');"
 * @param {event} e
 * @param {string} Field
 * @author 	Luciano Sampaio lsampaio@apollomedia.ca
 * @version 	0.1
 */
function NextField(e, Field)
{
  return ClickorNextField(e, Field, "focus");
}

/**
 * @projectDescription Invoke the onclick method of the next field of the form.
 * Supported: IE 4.0+, Mozilla 1.0+, Netscape 3.0+, Opera 7.0+, Safari 1.0+
 * Example:
 * onkeydown="return ClickField(event, 'btnSave');"
 * @param {event} e
 * @param {string} Field
 * @author 	Luciano Sampaio lsampaio@apollomedia.ca
 * @version 	0.1
 */
function ClickField(e, Field)
{
  return ClickorNextField(e, Field, "click");
}

/**
 * @projectDescription Sets the focus on the element passed as parameter.
 * Supported: IE 4.0+, Mozilla 1.0+, Netscape 3.0+, Opera 7.0+, Safari 1.0+
 * Example:
 * return ClickorNextField(e, Field, "focus");
 * return ClickorNextField(e, Field, "click"); 
 * @param {event} e
 * @param {string} Field
 * @param {string} TypeCmd 
 * @author 	Luciano Sampaio lsampaio@apollomedia.ca
 * @version 	0.1
 */
function ClickorNextField(e, Field, TypeCmd)
{
	var KeyCode;
	// Get the key that was pressed.
	KeyCode = (window.event) ? window.event.keyCode : (e) ? e.which : 0;
		
	// Check if the key was the "Enter".
	if (KeyCode === 13) 
  {
		// Check if the next field exists.
    var Obj = GetObj(Field);
    if (Obj)
    {
      switch (TypeCmd)
      {
      case "click" :
        Obj.click();
        break;
      default :
        Obj.focus();
        break;
      }
    }		
		if(ie5)
		{
			// Tell IE to cancel the action for the pressed key;
	    event.keyCode = 0;
    	event.returnValue = false;
		}
		// Tell the browser to cancel the action for the pressed key;			
		return false;
  }
	return true;
}

function ConfirmDelete(Type, Name)
{
	return confirm("Are you sure you want to delete the " + Type + " '" + Name + "' ?\nThis action can not be undo.\n\nClick OK to continue or Cancel to abort.");
}

var dom = (document.getElementById) ? true : false; 
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false; 
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false; 
var ns4 = (document.layers && !dom) ? true : false; 
var ie4 = (document.all && !dom) ? true : false; 

function GetObj(Id) 
{ 
  if (dom) 
	{
		return document.getElementById(Id);
	}
  return (ns4) ? document.layers[Id] : (ie4) 
	             ? document.all[Id] : (ie5||ns5) 
							 ? document.getElementById(Id) : null; 
}

// This script checks and unchecks boxes on a form
// Checks and unchecks unlimited number in the group
function CheckorUnCheckAll(Field, Form, IdToSelect)
{	
	if (Field.checked)
		CheckorUnCheck(Form, IdToSelect, true);
	else
		CheckorUnCheck(Form, IdToSelect, false);
}
// This script checks and unchecks boxes on a form
// Checks and unchecks unlimited number in the group
function CheckorUnCheck(Form, IdToSelect, Checked)
{
	for (I = 0; I < Form.length; I++)
	{
		if ((Form[I].type === 'checkbox') && (Form[I].value === IdToSelect))
			Form[I].checked = Checked;
	}
}

function RefreshPage(URL)
{
	document.location.href = URL;
}

// Not been used yet.
function SubmitForm(URL, Value)
{
	if (Value)
		URL = URL + Value;	
	
	var Obj = GetObj("txtSearchBox");
	if ((Obj) && (Obj.value != "") && (Obj.value != "Search"))
	{
		// Set the action url to the passed url.
		document.forms[0].action = URL;
//		document.forms[0].submit();
		
		// The id of the button.
		SetClick("btnSearchBoxQuery");
	}
	else
	{
		RefreshPage(URL);
	}
}

function GetAllSelectedImages()
{
	// Get the form reference from the iframe.
//	var Form = window.frames["listimagesincategories"].document.forms["ListImagesInCategories"];
	//alert(window.frames.length);
	var Form = window.frames["listimagesinproducts"].document.forms[0];	
	var SelectedImages = "";
	for (I = 0; I < Form.length; I++)
	{
		if ((Form[I].type === 'checkbox') && (Form[I].checked === true))
		{
			// It means that this check was selected.
			if (SelectedImages != "") 
				SelectedImages += ","; 
	
			SelectedImages += Form[I].value;
		}
	}			
	// Get all the selected images.
	document.forms[0].SelectedImages.value = SelectedImages;
	
	// Get all the images in the page.
	document.forms[0].ImagesList.value = Form.elements["ImagesList"].value;
}
