var agent  = navigator.userAgent.toLowerCase();
var isNN = (agent.indexOf("netscape")!=-1);
var isOpera = (agent.indexOf("opera")!=-1);
var isIE = (agent.indexOf("msie") != -1);

function _$(ElementID)
{
	return document.getElementById(ElementID);
}

function GetAvailableChars(textboxID, displayID, reflectionID, limit)
{
	var textbox = document.getElementById(textboxID);
	var display = document.getElementById(displayID); //available chars display
	var reflection = document.getElementById(reflectionID);
	
	
	if (limit > 0)
	{
		if (textbox)
		{
			if (textbox.value.length > limit)
			{
				textbox.value = textbox.value.substring(0, limit);
			}
			else if (textbox.value.length < limit)
			{
				if (textbox.value.indexOf('<') > -1)
				{
					textbox.value = textbox.value.replace('<', '');
				}
				
				if (reflection)
				{
					if (textbox.value.length > 0 && reflection.style.display == '')
					{
						reflection.innerHTML =  textbox.value + '<br />';
					}
					else
					{
						reflection.innerHTML = '';
					}
				}
			}
			
			if (display)						
			{
				display.innerHTML = (limit - textbox.value.length);
			}

		}//textbox
	}//limit > 0	
}

function HasAvailableChars(textbox, limit, eventArg)
{
	var valid = false;	
	var charCode = (eventArg.which) ? eventArg.which : event.keyCode
	
	if (textbox)
	{													//backspace		  //del
		if (textbox.value.length < limit || charCode == 8 || charCode == 127)
		{
			valid = true;
		}
		else
		{
			valid = false;
		}			
	}//textbox
	
	return valid;
}

function AutoTab(textbox, limit, eventArg)
{
	//eventArg not being used at the moment.
	
	var control = null;
	
	if (textbox.value.length == limit)
	{
		try
		{
			var index = GetIndex(textbox);
			
			if (index > -1)
			{
				control = textbox.form[(index + 1)];
				
				if (control != null)
				{
					control.focus();
				}
			}//if (index > -1)
		}
		catch (e)
		{
			//do nothing...
		}
	}//if (textbox.value.length == limit)	
}

function GetIndex(control)
{

	var index = -1;
	var i = 0;
	var found = false;
		
	try
	{
		while (i < control.form.length)
		{
			if (control.form[i] == control)
			{
				index = i;
				break;
			}
			
			i++;
		}
	}
	catch (e)
	{
		//do nothing...
	}

	return index;
}



function select_deselectAll (checkbox)
{
    var frm = document.forms[0];


    // Loop through all elements
    for (i=0; i < frm.length; i++)
    {				
		if (frm.elements[i].type == 'checkbox')
		{			
			if(checkbox.checked == true)
			{
					frm.elements[i].checked = true;
			}
			else
			{
					frm.elements[i].checked = false;
			}//if
		}//if
    }//for
    
    return true;
}

function CreateFlash(containerID, width, height, file)
{  
   var d         = null;
   var version   = '9,0,16,0'; //'6,0,0,0';
   var classid   = 'clsid:D27CDB6E-AE6D-11cf-96B8-444553540000';
   var codebase  = 'https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + version;
   var type      = 'application/x-shockwave-flash';
   var pluginurl = 'https://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash';      
      
   try
   {   

      d = document.getElementById(containerID);

      d.innerHTML = '<object classid="' + classid + '" ' + 'codebase="' + codebase + '" ' + 'width="' + width + '" height="' + height + '" VIEWASTEXT> ' +
				        '<param name="movie" value="' + file + '"> ' +
                    '<param name="quality" value="high"> ' +
				        '<param name="menu" value="false"> ' +
                    '<!--[if !IE]> <--> ' +
                    '<object data="' + file + '" ' + 'width="' + width + '" height="' + height + '" type="' + type + '" VIEWASTEXT> ' +
                    '<param name="quality" value="high"> ' +
                    '<param name="menu" value="false"> ' +
                    '<param name="pluginurl" value="' + pluginurl + '"> ' +
                    '</object>' +
                    '<!--> <![endif]--> ' +
                    '</object> ';
	
	} 
	catch(e)
	{
		   alert(e.toString());
	}	
}

function SetDefaultButton(e, focusid)
{  
   //netscape
   if (e.which && e.which == 13)
   {
      document.getElementById(focusid).focus();
      document.getElementById(focusid).click();
   }
   
   //ie
   if (window.event.keyCode && window.event.keyCode == 13)
   {
      document.getElementById(focusid).focus();
      document.getElementById(focusid).click();
   }
   
   return true;
}

function ToggleStateDisplay(CountryList, StateSelectID, StateTextBoxID)
{
	var StateList = document.getElementById(StateSelectID);
	var State = document.getElementById(StateTextBoxID);
		
	if (CountryList.value != "USA")
	{
		if (StateList != null && State != null)
		{
			StateList.style.display = 'none';
			State.style.display = '';
		}
	}
	else
	{
		if (StateList != null && State != null)
		{
			StateList.style.display = '';
			State.value = '';
			State.style.display = 'none';			
		}
	}
}

function IsCharKey(e)
{
	var status = true;	
	var charCode = (e.which) ? e.which : window.event.keyCode
	
	//Check Upper Case
	if (charCode < 65 || charCode > 90)
		status = false;
			
	//Check Lower Case
	if (status == false)
	{
		if ((charCode < 97 || charCode > 122))
			status = false;
		else
			status = true;
	}	
	
	return status;
}

function IsNumberKey(e)
{	
	var status = true;	
	var charCode = (e.which) ? e.which : window.event.keyCode
		
	
	if (charCode < 48 || charCode > 57)
		status = false;		
	
	return status;
}

function StopEnterKey(e)
{
	var e = (e) ? e : ((window.event) ? window.event : null);
	var charCode = (e.which) ? e.which : window.event.keyCode
	
	if (charCode == 13)
		return false;
	else
		return true;
}

function ToggleVisibility(controlID, show)
{
	var control = document.getElementById(controlID);
	
	if (control)
	{	
		if (show != true && show != false)
		{
			show = false;
		}
		
		if (show == true)
		{
			control.style.display = '';
		}
		else
		{
			control.style.display = 'none';
		}
	
	}// if (control)
}

//PreloadImage was a concept copied from MySpace.
function PreloadImage(img)
{ 
	//for some reason, img does not return Height but newCover does.	
	var newCover = new Image();	
	
	if (img != null)
	{
		newCover.src = img.src;

		return newCover;
	} 
	else
	{
		return img;
	}
}

//AdjustCropCover was a concept copied from MySpace.
function AdjustCropCover(img, div)
{	
	var cover = { w: 170, h: 127 };

	//this does not work with large gifs.
	//we haven't figured out how to resize animated
	//gifs using only the server language.
	if (img.src.indexOf('.gif') > 1)
	{
		img.width = cover.w;
		img.height = cover.h;
		return;
	}
		
	//if the image is not completely loaded then preload image
	//this is most often possible in IE browsers 6,7
	if (!img.height)
	{
		if(!img.complete)
			img = PreloadImage(img);			
	}

	img.style.cssFloat = "left";

	if (img.width < cover.w || img.width > cover.w)
	{
		img.width = cover.w;
		img.style.width = cover.w;				
	}
	
	var halfWay = img.height / 2;
	var topOfCover = cover.h / 2;


	//PreloadImage must be executed first before this can work.
	var offset = topOfCover - halfWay;
	
	if(div != undefined)
	{
		div.style.marginTop = Math.round(offset) + "px";
	}
}

function AdjustCustomCropCover(img, div, coverWidth, coverHeight)
{	
	var cover = { w: coverWidth, h: coverHeight };

	//this does not work with large gifs.
	//we haven't figured out how to resize animated
	//gifs using only the server language.
	if (img.src.indexOf('.gif') > 1)
	{
		img.width = cover.w;
		img.height = cover.h;
		return;
	}
			
	//if the image is not completely loaded then preload image
	//this is most often possible in IE browsers 6,7
	if (!img.height)
	{
		if(!img.complete)
			img = PreloadImage(img);			
	}		

	img.style.cssFloat = "left";

	if (img.width < cover.w || img.width > cover.w)
	{
		img.width = cover.w;
		img.style.width = cover.w;				
	}

	var halfWay = img.height / 2;
	var topOfCover = cover.h / 2;

	//PreloadImage must be executed first before this can work.
	var offset = topOfCover - halfWay;
	
	if(div != undefined)
		div.style.marginTop = Math.round(offset) + "px";
}

//Gets query parameter from url.
//Ref: http://www.netlobo.com/url_query_string_javascript.html
function GetParameter(Link, Key)
{
	if (!Link)
		Link = window.location.href;

	Key = Key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	
	var regexS = "[\\?&]" + Key + "=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( Link );
	
	if( results == null )
		return "";
	else 
	
	return results[1];
}

function SetElementValue(ElementID, ElementValue)
{
	_$(ElementID).value = ElementValue;
	
}

function SetTextBoxValue(ElementID, Value)
{
	try
	{
		_$(ElementID).value = Value;
	}
	catch (e)
	{
		document.getElementsByName(ElementID)[0].value = Value;
	}
}

function ClearDefaultValue(ElementID, Value)
{
	var element = _$(ElementID);
	
	if (element)
	{
		if (element.value == Value)
		{
			element.value = '';
		}
	}
}