//Common global javascript functions
//

//To enable/disable the background: 
function backgroundFilter()
{
    var div;

    if(document.getElementById)  // Standard way to get element
    	div = document.getElementById('backgroundFilter');
    else if(document.all)        // Get the element in old IE's
    	div = document.all['backgroundFilter'];   

    // if the style.display value is blank we try to check it out here
    if(div.style.display == '' && div.offsetWidth != undefined && div.offsetHeight != undefined)
    {
        div.style.display = (div.offsetWidth!=0 && div.offsetHeight!=0)?'block':'none';
    }

    // If the background is hidden ('none') then it will display it ('block').
    // If the background is displayed ('block') then it will hide it ('none').
    div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block';
}


//To display/hide the popup:
//posType (screen,cursor)
function popUp(posX,posY,layerToShow)
{
    var div;

    if(document.getElementById)    // Standard way to get element
    	div = document.getElementById(layerToShow);
    else if(document.all)          // Get the element in old IE's
    	div = document.all[layerToShow];   

    // if the style.display value is blank we try to check it out here
    if(div.style.display== '' && div.offsetWidth != undefined && div.offsetHeight != undefined)
    {
        div.style.display = (div.offsetWidth!=0 && elem.offsetHeight!=0)?'block':'none';
    }

    // If the PopUp is hidden ('none') then it will display it ('block').
    // If the PopUp is displayed ('block') then it will hide it ('none').
    div.style.display = (div.style.display==''||div.style.display=='block')?'none':'block';
    
    posY += (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    
    // Sets the position of the DIV
    div.style.left = posX+'px';
    div.style.top = posY+'px';    
}


//toggle display of US state dropdown list or textbox for freeform state
function setStateDisplay(countryDDList)
{
	var divStateDD = document.getElementById('jobs_state_d');
	var divStateText  = document.getElementById('jobs_state_t');
	var stateDD = document.getElementById('jobs_state_dropdown');
	var stateText = document.getElementById('jobs_state_text');
	
	var divCCStateDD = document.getElementById('card_state_d');
	var divCCStateText  = document.getElementById('card_state_t');
	var stateCCDD = document.getElementById('card_state_dropdown');
	var stateCCText = document.getElementById('card_state_text');
	var cardCountry = document.getElementById('payments_cardCountryId');
	var selCountryValue = countryDDList.options[countryDDList.selectedIndex].value;
	
	if (cardCountry)
		cardCountry.value = selCountryValue;
	
	if (selCountryValue=="1")
	{
		if (divStateDD)
			divStateDD.style.display = 'block';
		if (divStateText)
		{
			if (stateText)
				stateText.value = '';
			divStateText.style.display = 'none';
		}
		
		if (divCCStateDD)
			divCCStateDD.style.display = 'block';
		if (divCCStateText)
		{
			if (stateCCText)
				stateCCText.value = '';
			divCCStateText.style.display = 'none';
		}
	}
	else
	{	
		if (divStateDD)
		{
			if (stateDD)
				stateDD.selectedIndex = 0;
			divStateDD.style.display = 'none';
		}
		if (divStateText)
			divStateText.style.display = 'block';
		
		
		if (divCCStateDD)
		{
			if (stateCCDD)
				stateCCDD.selectedIndex = 0;
			divCCStateDD.style.display = 'none';
		}
		if (divCCStateText)
			divCCStateText.style.display = 'block';
	}
}


//set home page
function setDefaultHomePage(objLink)
{	
	objLink.setHomePage('http://www.feetrader.com');
}


//check checkbox limit for checkbox list and disable if limit reached
function checkCheckboxLimit(formObj, checkBoxName, limit)
{
	var checkBoxes = formObj[checkBoxName];	
	var boxCount = checkBoxes.length;
	var numChecked = 0;	
		
	//get number of checked boxes
	for (i = 0; i < boxCount; i++) 
	{
		if (checkBoxes[i].checked) 
			numChecked +=1;		
	}
	
		
	//check if limit reached and if so disable all unchecked boxes
	//otherwise make sure all boxes are enabled
	if (numChecked >= limit)
	{
		for (i = 0; i < boxCount; i++) 
		{
			if (!checkBoxes[i].checked) 
				checkBoxes[i].disabled=true;
		}
	}	
	else
	{
		for (i = 0; i < boxCount; i++) 
		{			 
			checkBoxes[i].disabled=false;
		}
	}
}


//hide any select controls on page
function hideSelect()
{
	var selectObjs = document.getElementsByTagName("select");

	if (selectObjs)
	{
		for (i = 0; i < selectObjs.length; i++) 
		{
			selectObjs[i].style.visibility = "hidden";
		}
	}
}

//show select controls on page
function showSelect()
{
	var selectObjs = document.getElementsByTagName("select");

	if (selectObjs)
	{
		for (i = 0; i < selectObjs.length; i++) 
		{
			selectObjs[i].style.visibility = "visible";
		}
	}
}

function getWindowSize() 
{
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	   
	  return [myWidth, myHeight];
}

function getScrollXY() 
{
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) 
	  {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  }
	  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
	  {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } 
	  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
	  {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  return [ scrOfX, scrOfY ];
}


function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
    return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else
  {
    return document.getElementById(movieName);
  }
}


function stopFlashMovie(movieId)
{
     var flashMovie=getFlashMovieObject(movieId);
     if (flashMovie)
     {
    	 if(typeof flashMovie.playerStop == 'function') 
    	 { 
    		 flashMovie.playerStop();
    	 }     			 
     }
}



/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	} 
}
