//
// 2009.07.08
//

function inArray(f,a)
{
for(var i=0;i<a.length;i++)
  {
  if(a[i] == f) return true;
  }//for

return false;
}

function fixpngs()
{//assumes we're using the supersleight fix.
if(typeof(supersleight) != 'undefined') supersleight.run();
}

function eventLinkIn(event_str,new_event)
{// 2008.03.07
    // window.onload = somefunc;
    // onload_chain = eventLinkIn("window.onload",somefunc);
    // then in your somefunc function if(onload_chain) onload_chain(e) 
var result = false;
var event = eval(event_str); 
if(typeof(event) == 'function')
  {
  result = event;
  }

eval(event_str + " = new_event");

return result;
}

function el(id)
{
return document.getElementById(id);
}

function elToggle(id)
{
 var o = el(id);
 
 if(o)
   {
   if(o.style.display != 'none')
     o.style.display = "none";
   else
      o.style.display = "block";
   }//if
}//func

function elShow(id)
{
 var o = el(id);
 
 if(o)
      o.style.display = "block";
}//func


function elShowAndFocus(id_show,id_focus,id_hide)
{
 var os = el(id_show);
 var of = el(id_focus);
 var oh = el(id_hide);
 
 
 if(os)
      os.style.display = "block";
      
 if(oh)
      oh.style.display = "none";
      
      
 if(of)
      of.focus();
      
}//func


function toggleSet(toggleSet,id)
  {//toggleSet = {}
  var i,o;
  
  for(i in toggleSet)
    {
    o = toggleSet[i];
    el(i).style.display = 'none';
    }//for
    
    el(id).style.display = 'block';
    toggleSet[id] = true;
  }//func
    


function swapBlock(block)
{ //display as block the first element...
  //hide all the rest (variable params)
  //
  // "cat","dog","rabbit"
  // (id #cat will be set to block.. dog and rabbit will be set to hidden)
   
  el(block).style.display = "block";
  for(var i=1; i<arguments.length; i++) 
      el(arguments[i]).style.display = "none";
}//func

function lprops(obj)
{
var r = "";
var o;
for(i in obj)
  {
  r += i+" = "+obj[i]+"\n";  
  }//for
  
return r;
}//func


function formatCurrency(num,hide_cents) {
res = 0;
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
res = ((sign)?'':'-') + '$' + num;
if(!hide_cents)
  res +=  '.' + cents;
return (res);
}


function cleanCurrency(num) {

num = parseFloat(num.toString().replace(/\$|\,/g,''));
return (num);
}


function stripTags(t)
{// 2007.11.20
 // http://zamov.online.fr/EXHTML/DHTML/DHTML_983246.html
 
 if(t)
   return t.replace(/(<([^>]+)>)/ig,"");
 else
   return t;
 
}//func


function nice4url(str)
{// 2008.03.03
  
 if(str)
   return str.replace(/ /ig,"-");
 else
   return str;
 
}//func



function getElementsByClass(searchClass,node,tag) {
// http://www.dustindiaz.com/top-ten-javascript/
// 2007.12.04
//
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}



/* You are welcome to re-use these [TEA] scripts [without any warranty express or implied] provided you retain the copyright notice and when possible a link to Chris' website. 
copyright 2002-2005 Chris Veness [http://www.movable-type.co.uk/scripts/tea-block.html] */  
    
 

//
// TEAencrypt: Use Corrected Block TEA to encrypt plaintext using password
//             (note plaintext & password must be strings not string objects)
//
// Return encrypted text as string
//
function TEAencrypt(plaintext, password)
{
    if (plaintext.length == 0) return('');  // nothing to encrypt
    // 'escape' plaintext so chars outside ISO-8859-1 work in single-byte packing, but keep
    // spaces as spaces (not '%20') so encrypted text doesn't grow too long (quick & dirty)
    var asciitext = escape(plaintext).replace(/%20/g,' ');
    var v = teaStrToLongs(asciitext);  // convert string to array of longs
    if (v.length <= 1) v[1] = 0;  // algorithm doesn't work for n<2 so fudge by adding a null
    var k = teaStrToLongs(password.slice(0,16));  // simply convert first 16 chars of password as key
    var n = v.length;

    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = 0;

    while (q-- > 0) {  // 6 + 52/n operations gives between 6 & 32 mixes on each word
        sum += delta;
        e = sum>>>2 & 3;
        for (var p = 0; p < n; p++) {
            y = v[(p+1)%n];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z);
            z = v[p] += mx;
        }
    }

    var ciphertext = teaLongsToStr(v);

    return teaEscCtrlCh(ciphertext);
}

//
// TEAdecrypt: Use Corrected Block TEA to decrypt ciphertext using password
//
function TEAdecrypt(ciphertext, password)
{
    if (ciphertext.length == 0) return('');
    var v = teaStrToLongs(teaUnescCtrlCh(ciphertext));
    var k = teaStrToLongs(password.slice(0,16)); 
    var n = v.length;

    var z = v[n-1], y = v[0], delta = 0x9E3779B9;
    var mx, e, q = Math.floor(6 + 52/n), sum = q*delta;

    while (sum != 0) {
        e = sum>>>2 & 3;
        for (var p = n-1; p >= 0; p--) {
            z = v[p>0 ? p-1 : n-1];
            mx = (z>>>5 ^ y<<2) + (y>>>3 ^ z<<4) ^ (sum^y) + (k[p&3 ^ e] ^ z);
            y = v[p] -= mx;
        }
        sum -= delta;
    }

    var plaintext = teaLongsToStr(v);

    // strip trailing null chars resulting from filling 4-char blocks:
    plaintext = plaintext.replace(/\0+$/,'');

    return unescape(plaintext);
}


// supporting functions

function teaStrToLongs(s) {  // convert string to array of longs, each containing 4 chars
    // note chars must be within ISO-8859-1 (with Unicode code-point < 256) to fit 4/long
    var l = new Array(Math.ceil(s.length/4));
    for (var i=0; i<l.length; i++) {
        // note little-endian encoding - endianness is irrelevant as long as 
        // it is the same in longsToStr() 
        l[i] = s.charCodeAt(i*4) + (s.charCodeAt(i*4+1)<<8) + 
               (s.charCodeAt(i*4+2)<<16) + (s.charCodeAt(i*4+3)<<24);
    }
    return l;  // note running off the end of the string generates nulls since 
}              // bitwise operators treat NaN as 0

function teaLongsToStr(l) {  // convert array of longs back to string
    var a = new Array(l.length);
    for (var i=0; i<l.length; i++) {
        a[i] = String.fromCharCode(l[i] & 0xFF, l[i]>>>8 & 0xFF, 
                                   l[i]>>>16 & 0xFF, l[i]>>>24 & 0xFF);
    }
    return a.join('');  // use Array.join() rather than repeated string appends for efficiency
}

function teaEscCtrlCh(str) {  // escape control chars etc which might cause problems with encrypted texts
    return str.replace(/[\0\t\n\v\f\r\xa0'"!]/g, function(c) { return '!' + c.charCodeAt(0) + '!'; });
}

function teaUnescCtrlCh(str) {  // unescape potentially problematic nulls and control characters
    return str.replace(/!\d\d?\d?!/g, function(c) { return String.fromCharCode(c.slice(1,-1)); });
}
 

function getFields(d,remove_prefix)
{//given an dom object (scope) creates an aa list of fields;
 //[field-name] = "value";
 // 2008.03.26
 
 // requires selectOptionByValue
var ol,o,r;
var i,pid;
var scan = new Array();
var result = {};

ol = d.getElementsByTagName("input");
for(i in ol) if (typeof(ol[i]) == 'object') scan.push(ol[i])

ol = d.getElementsByTagName("select");
for(i in ol) if (typeof(ol[i]) == 'object') scan.push(ol[i])

ol = d.getElementsByTagName("textarea");
for(i in ol) if (typeof(ol[i]) == 'object') scan.push(ol[i])


for(i in scan) 
  {
  pi = i;
  o = scan[i];

    if((o) && (o.id))
     {
       pid = o.id;
       if(remove_prefix)
          pid = pid.substr(remove_prefix.length);
        
       
       r = o.value;
       
       if(o.type == 'checkbox')
         {
         if(!o.checked)
           r = "";
         }
         
         result[pid] = r;
     }//if

     
  }//for
  
  return result;
 
}//func


function extractObject(fl,obj)
{// 2008.03.05
 //
 //fl = {'apple','hat','boat'}
 //obj = ['hat' : 1234,'car' : 1234]
 //
 //result = ['hat' : 1234]
 //
var result = {};
var len = fl.length;

for(var i=0;i<len;i++)
  {
  var fn = fl[i];
  
  if(obj[fn])
    {
    result[fn] = obj[fn];
    } 
  }//for
  
return result;
}//func


function setFields(fl)
{//given an aa list of fields.. sets values to "";
 //[field-name] = "value";
var o;
var i;

for(i in fl) 
  {
  o = document.getElementById(i)

  if(o)
     {
     if(o.type == 'checkbox')
       {
       if(fl[i] == 0)
         o.checked = false;
       else
         o.checked = true;
       }
     else
       o.value = fl[i];
     
     }//if
  }//for
 
}//func

function clearFields(d)
{//given an dom object (scope) clears all values for each form element
 //[field-name] = "value";
 // 2008.03.02
 
var ol,o;
var i;
var scan = new Array();

ol = d.getElementsByTagName("input");
for(i in ol) if (typeof(ol[i]) == 'object') scan.push(ol[i])

ol = d.getElementsByTagName("select");
for(i in ol) if (typeof(ol[i]) == 'object') scan.push(ol[i])

ol = d.getElementsByTagName("textarea");
for(i in ol) if (typeof(ol[i]) == 'object') scan.push(ol[i])


for(i in scan) 
  {
  o = scan[i];

    if((o) && (o.id))
     {
       o.value = "";
     }//if

     
  }//for
  
  
 
}//func



function getEvent(e)
{
 var targ;
 
 //standard event (e)
 if (!e) var e = window.event;
 
 //standard key (e.aKey)
 e.aKey = e.keyCode; 
 if(!e.aKey)
   e.aKey = e.charCode;

 //standard target (e.aTarget)
 if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

 e.aTarget = targ;
 
 return e;
}

 function addAnEvent(obj,name,func)
        {// 2008.03.10
         // note: review ie 6 for memory leak.

         //name = 'click' (just click... the 'on' of onclick is assumed.)
        if(obj.addEventListener)
          {
          //W3C
          obj.addEventListener(name,func,false)
   
          }
        else
        if(obj.attachEvent)
          {
      
          //MS
          obj.attachEvent('on'+name,func)
          }
        else
          {
           //weird... unknown (we should use our own event chain)
           eval("obj.on"+name+" = func");
          }
        }//func

//ref: http://www.quirksmode.org/js/events_properties.html [2008.03.04]

