// <copyright company="Cubic Compass Software">
// Copyright (c) 2006 Cubic Compass Software http://www.cubiccompass.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, 
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, 
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE 
// AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

var windowWidth = 630, windowHeight = 460;

function InitWindowSize() {
  var windowWidth = 0, windowHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    windowWidth = window.innerWidth;
    windowHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }
}

function OpenWindow(url, windowName, params){
	var newWindow = window.open(url, windowName, params);
	if(windowName=="ViewPermissions")
		return false;
}

function setTime() {
	var date = new Date();
	document.forms[0].clientTimeVariable.value = date.getTimezoneOffset();
}

function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expire.toGMTString();+";path=/";
}

function ReadCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return "";
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length;
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function ConfirmRedirection(confirmText, actionURL){
	var result = confirm(confirmText);
	if(result==true)
		document.location.href=actionURL;
	
	return false;
}

function KeySpy(sender, event){	
	var ch = event.keyCode;
	if(ch == 13){
		if(sender == "SigninBtn"){
			setTime();
		}
		__doPostBack(sender,'');
		return false;
	}
	else
		return true;
}
/*****  Tabbed Interface Methods *****/
var Tab = { cur:0 };

function initab( classname, w, h ) {
 var tabs = [], divs = [], texts = [], f, g, i = 0, j = 0, k, t, o, p,
  d = document, a = d.getElementsByTagName( 'h3' ),
  b = d.createElement('div'), c = d.createElement('a'),
  r = new RegExp( '\\b' + classname + '\\b' ),
  y = function() { return false; },
  z = function(e) { e = e || window.event || {}; var t;
   if( e.keyCode===37 &&
    ( ( t = this.previousSibling ) || ( t = this.parentNode.lastChild ) ) ||
    e.keyCode===39 &&
    ( ( t = this.nextSibling ) || ( t = this.parentNode.firstChild ) ) ) {
     if( /a/i.test( t.tagName ) ) { t.focus(); e.returnValue = false; }
     return false;
   }
  };
 b.className = 'tab' + classname;
 c.className = 'tab0';
 c.hideFocus = true;
 while( i < a.length ) { p = a[i];
  if( r.test( p.className ) && ( g = getnext(p) ) ) {
   p.style.width = ( w || 700 ) + 'px';
   p.style.height = ( h || 400 ) + 'px';
   p.className += ' slab0';
   g.className += ' slab0';
   t = gettext( p );
   o = c.cloneNode( true );
   o.onfocus = new Function( 'return dotob(' + j + ');' );
   o.onclick = y;
   o.onkeydown = z;
   o.href = '?' + ( j + 1 );
   o.appendChild( d.createTextNode( t || 'Tab ' + (j+1) ) );
   b.appendChild(o);
   tabs[j] = o; divs[j] = g; texts[j] = t.toLowerCase();
   f = f || p;
   j++;
  }
  i++;
 }
 f.parentNode.insertBefore( b, f );
 Tab.tabs = tabs; Tab.divs = divs; Tab.texts = texts; i = 0;
 if( ( j = window.location.search.substring(1) ) ) {
  k = ( '. ' + texts.join(' ') + ' ' ).split( new RegExp( ' ' + j + ' ' ) )[0].split( /\s/ ).length-1;
  if( tabs[k] ) { i = k; }
  else if( ( j = parseInt( j, 10 ) - 1 ) && tabs[j] ) { i = j; }
 }
 dotob(i);
}

function untab( classname ) {
 var i, p, q,
  d = document, a = d.getElementsByTagName( 'h3' ),
  r = new RegExp( '\\b' + classname + '\\b' );
 i = a.length; while(i--) { p = a[i]; q = p.className;
  if( r.test( q ) ) {
   p.className = q.replace( / slab\d/, '' );
( g = getnext(p) )
  }
 }
 a = d.getElementsByTagName( 'div' );
 r = new RegExp( '\\btab' + classname + '\\b' );
 i = a.length; while(i--) { p = a[i];
  if( r.test( p.className ) ) {
   p.parentNode.removeChild(p);
  }
 }
}

function dotab(i) {
  Tab.tabs[Tab.cur].className = 'tab0';
  Tab.divs[Tab.cur].className = 'slab0';
  Tab.cur = i;
  Tab.tabs[i].className = 'tab1';
  Tab.divs[i].className = 'slab1';
  return false;
}

function dotob(i) {
 var r = /(t|sl)ab\d/, t = function(s) {
  Tab.tabs[Tab.cur].className = Tab.tabs[Tab.cur].className.replace( r, 'tab' + s );
  Tab.divs[Tab.cur].className = Tab.divs[Tab.cur].className.replace( r, 'slab' + s );
 };
 t( '0' ); Tab.cur = i; t( '1' );
 return false;
}

if( typeof gettext === 'undefined' ) { function gettext(o) { for ( var s = '', j = o.firstChild; j !== null; j = j.nextSibling ) { if( j.nodeType === 1 ) { s += gettext( j ); } else if( j.nodeType === 3 ) { s += j.nodeValue || ' '; } } return s.replace( /\s+/, ' ' ); } }

if( typeof getnext === 'undefined' ) { function getnext(o) { while( ( o = o.nextSibling ) ) { if( /div/i.test( o.tagName ) ) { return o; } return null; } } }
/********* Element Toggle Methods ***********/
var hotspots = document.getElementsByName('hotspot');
var toggles = document.getElementsByName('toggle');

function visibilitytoggle()
{
  for (var i = 0; i < hotspots.length; i++)
  {
  hotspots[i].someProperty = i;
  hotspots[i].onclick = function() {toggle(this.someProperty)};
  }

  for (var i = 0; i < toggles.length; i++)
  {
  toggles[i].style.display = 'none';
  }
}

function toggle(i)
{
  if (toggles[i].style.display == 'none')
  {toggles[i].style.display = ''
  }
  else
  toggles[i].style.display = 'none'
} 

function toggleShowAll()
{
  for (var i = 0; i < toggles.length; i++)
  {
  toggles[i].style.display = '';
  }
}

function toggleHideAll()
{
  for (var i = 0; i < toggles.length; i++)
  {
	toggles[i].style.display = 'none';
  }
}

function toggleElement(me){
	if (me.style.visibility=="hidden"){
		me.style.visibility="visible";
	}
	else {
		me.style.visibility="hidden";
	}
}

//
//Begin: clientside url encode methods
//
function encode(xmlString)
{
	if(xmlString == '')
	{
		return xmlString;
	}
	else
	{
		return transform(xmlString);
	}
}

function decode(hexString)
{
	if(hexString == '')
	{
		return hexString;
	}
	else
	{
		return unescape(hexString);
	}
}

function transform(aString)
{
	var hexString='';
	var i;
	for (i=0; i<aString.length; i++)
	{
		hexString += '%'+hexfromdec( aString.charCodeAt(i) );
    }
    return hexString;
}

// ------------ begin conversion routines ----------
// these are the functions that actually convert
// from decimal ascii to hexidecimal ascii code
function hexfromdec(num)
{
	if (num > 65535) 
	{
		return ("err!");
	}
	first = Math.round(num/4096 - .5);
	temp1 = num - first * 4096;
	second = Math.round(temp1/256 -.5);
	temp2 = temp1 - second * 256;
	third = Math.round(temp2/16 - .5);
	fourth = temp2 - third * 16;
	return (""+getletter(third)+getletter(fourth));
}

function getletter(num)
{
	if (num < 10) 
	{
		return num;
	}
	else 
	{
		if (num == 10) { return "A" }
		if (num == 11) { return "B" }
		if (num == 12) { return "C" }
		if (num == 13) { return "D" }
		if (num == 14) { return "E" }
		if (num == 15) { return "F" }
	}
}
// --------------- end conversion routines --------------
//
//End: clientside url encode methods

function toggleVisibility(frameID, imgID, imageDirectory){
	if (document.getElementById(frameID).style.display == 'none')
	{
		document.getElementById(frameID).style.display = 'block';
		document.getElementById(imgID).src	= imageDirectory + "/min.jpg";
		document.getElementById(imgID).alt	= "Minimize";
	}
	else
	{
		document.getElementById(frameID).style.display = 'none';
		document.getElementById(imgID).src	= imageDirectory + "/max.jpg";
		document.getElementById(imgID).alt	= "Maximize";
	}
}
//// Flash Version Detector  v1.1.5
// http://www.dithered.com/javascript/flash_detect/index.html
// code by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)
// with VBScript code from Alastair Hamilton

var flashVersion = 0;
function getFlashVersion() {
	var agent = navigator.userAgent.toLowerCase(); 
	
   // NS3 needs flashVersion to be a local variable
   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
      flashVersion = 0;
   }
   
	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') { 
			if (flashPlugin.description.indexOf('7.') != -1) flashVersion = 7;
			else if (flashPlugin.description.indexOf('6.') != -1) flashVersion = 6;
			else if (flashPlugin.description.indexOf('5.') != -1) flashVersion = 5;
			else if (flashPlugin.description.indexOf('4.') != -1) flashVersion = 4;
			else if (flashPlugin.description.indexOf('3.') != -1) flashVersion = 3;
		}
	}

	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
	   document.write('<scr' + 'ipt language="VBScript"\> \n');
		document.write('on error resume next \n');
		document.write('dim obFlash \n');
		document.write('set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.7") \n');
		document.write('if IsObject(obFlash) then \n');
		document.write('flashVersion = 7 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.6") end if \n');
		document.write('if flashVersion < 7 and IsObject(obFlash) then \n');
		document.write('flashVersion = 6 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.5") end if \n');
		document.write('if flashVersion < 6 and IsObject(obFlash) then \n');
		document.write('flashVersion = 5 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.4") end if \n');
		document.write('if flashVersion < 5 and IsObject(obFlash) then \n');
		document.write('flashVersion = 4 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.3") end if \n');
		document.write('if flashVersion < 4 and IsObject(obFlash) then \n');
		document.write('flashVersion = 3 \n');
		document.write('end if');
		document.write('</scr' + 'ipt\> \n');
  }
		
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;

	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;

	// Can't detect in all other cases
	else {
		flashVersion = flashVersion_DONTKNOW;
	}

	return flashVersion;
}

flashVersion_DONTKNOW = -1;

var fontTags			= new Array( 'div', 'table', 'td', 'span', 'a', 'li', 'h1', 'h2', 'h3', 'li');

function InitializeFonts(){
	var size = ReadCookie("CCS_LiquidFont_Size");
	if(size != "")
		ResizeFonts(size);
}

function ResizeFonts(selectedFontSize){	
	if(!document.getElementById)
		return;
	
	var rootElement = null,tagIndex,elementIndex,cfontTags;
	var target = 'body';
	
	if( !( rootElement = document.getElementById( target ) ) ) rootElement = document.getElementsByTagName( target )[ 0 ];
	
	var size		= 0;
	var coefficient	= 0.0;
	
	for(tagIndex = 0 ; tagIndex < fontTags.length ; tagIndex++ ) {
		cfontTags = rootElement.getElementsByTagName( fontTags[ tagIndex ] );
		
		for( elementIndex = 0 ; elementIndex < cfontTags.length ; elementIndex++ ){
			size = FontGetDefaultSize(cfontTags[ elementIndex ]);
			
			if(size == 0)
				continue;
			
			coefficient = size / 100;
			size = coefficient * selectedFontSize;
			cfontTags[ elementIndex ].style.fontSize = size.toString() + "%";
		}
	}
	SetCookie('CCS_LiquidFont_Size', selectedFontSize, 365);
}

var fontDefaultSizeNormalBig	= 130;
var fontDefaultSizeNormal		= 95;
var fontDefaultSizeNormalSmall	= 70;

function FontGetDefaultSize(element){
	if(element.className == "")
		return 0;
	else if(element.className == "NormalBig" || element.className == "txt1" || element.className == "txt1Bold")
		return fontDefaultSizeNormalBig;
	else if(element.className == "Normal" || element.className == "NormalBold" || element.className == "txt3" || element.className == "txt3Bold" || element.className == "lnk3" || element.className == "fh"  || element.className == "f" || element.className == "SearchItemAbstract" || element.className == "SearchItemTitle")
		return fontDefaultSizeNormal;
	else if(element.className == "NormalSmall" || element.className == "txt4" || element.className == "txt4Bold" || element.className == "lnk4")
		return fontDefaultSizeNormalSmall;
	else
		return 0;
}

//Client side search term builder engine (add new control specific methods as needed using the following pattern)
function addRadioButtonListTerms(elementName, termNamePrefix, params)
{
  var rdo = getValidElement(elementName);
  if(rdo == null)
    return params;
  
  var rdochildren = rdo.all;
  for (var i = 0; i < rdochildren.length; i++) 
  { 
    var input = rdochildren(i) 
    if (input.tagName == "INPUT" && input.type == "radio" && input.checked == true)
    { 
      params = addSearchTerm(termNamePrefix + input.value, 'true', params);
    }
  }  
  return params;
}

function addComponentArtCalendarTerm(cal, termName, params)
{
  if(cal == null)
    return params;

  var sd = cal.GetSelectedDate();
  if(sd != null)
  {
    var dtStr = sd.getMonth() + "/" + sd.getDay() + "/" + sd.getFullYear();
    params = addSearchTerm(termName, dtStr, params);
  }
  return params;
}

function addCheckBoxListTerms(elementName, termName, params)
{
  var lst = "";
  var elm = getValidElement(elementName);
  if(elm == null)
    return params;

  var children = elm.all;
  for (var i = 0; i < children.length; i++) 
  { 
    var input = children(i) 
    if (input.tagName == "INPUT" && input.type == "checkbox" && input.checked == true)
    { 
      var ns = input.nextSibling;//get sibling label text
      if(ns.innerText != "")
      {
        if(lst != "")
        {
          lst += ",";
        }
        lst += ns.innerText;
      }
    }
  }
  params = addSearchTerm(termName, lst, params); 
  return params;
}

function addTextBoxMultTerms(elementName, termName, params)
{
  var kwLst = "";
  var te = getValidElement(elementName);
  if(te == null)
    return params;

  var kwText = te.value;
  var kwArr = kwText.split(" ");
   
  for(i=0; i<kwArr.length; i++) 
  {
    var cleanTxt = cleanText(kwArr[i]);
    if(cleanTxt.length > 2 || cleanTxt == '*')//ignore <3 characters or empty terms
    {
      if(kwLst != "")
      {
        kwLst += ",";
      }
      kwLst += cleanTxt;
    }
  }
  params = addSearchTerm(termName, kwLst, params); 
  return params;
}

function addTextBoxSingleTerm(elementName, termName, params)
{
  var te = getValidElement(elementName);
  if(te == null)
    return params;

  var cleanTxt = cleanText(te.value);  
  if(cleanTxt != "")
  {        
    params = addSearchTerm(termName, cleanTxt, params);
  }
  return params;
}

function addSearchTerm(termName, termValue, params)
{
  if(termValue != "") 
  {
    if(params.indexOf("?") == params.length-1)
    {
        params += termName+"="+termValue;
    }
    else
    {
        params += "&"+termName+"="+termValue;
    }
  }
  return params
}

function cleanText(dirtyText)
{
  var rx = new RegExp("[^a-zA-Z0-9 \*]", "gi");//currently no support for special chars
  var cleanText = dirtyText.replace(rx,"");
  if(cleanText == undefined)
    return "";
  else
    return cleanText;
}

function getValidElement(elementName)
{
  var el = document.getElementById(elementName);
  if(el == undefined)
  {
    throwErrorMsg("Could not find element: " + elementName);
    return null;
  }
  return el;
}

function isNum(value)
{
  return !isNaN(new Number(value));
}

function throwError(errorText, element)
{
  throwErrorMsg(errorText);
  element.focus();
  return;
}

function throwErrorMsg(errorText)
{
  alert(errorText);
  return;
}
//End client side search term builder engine