// JavaScript Document
/*  This Javascript library is the property of Robert E. Schuon Jr.
    and Virtual Light Creative Group, Inc., and may not be used, transmitted,
	copied or otherwise duplicated without the express written consent 
	of Robert E. Schuon Jr. and Virtual Light Creative Group.
	Copyright October 26, 2009
	
*/

function trim(stringToTrim) 
  {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
  }

function ltrim(stringToTrim) 
  {
	return stringToTrim.replace(/^\s+/,"");
  }

function rtrim(stringToTrim) 
  {
	return stringToTrim.replace(/\s+$/,"");
  }

  
function capWords(fieldName)
  {
    var inputString = document.getElementById(fieldName);
	var outputString = document.getElementById(fieldName);
    var tmpStr, tmpChar, preString, postString, strlen;
    tmpStr = inputString.value.toLowerCase();
    stringLen = tmpStr.length;
if (stringLen > 0)
{
  for (i = 0; i < stringLen; i++)
  {
    if (i == 0)
	{
	  var outputString;
      tmpChar = tmpStr.substring(0,1).toUpperCase();
      postString = tmpStr.substring(1,stringLen);
      tmpStr = tmpChar + postString;
    }
    else
	{
      tmpChar = tmpStr.substring(i,i+1);
      if (tmpChar == " " && i < (stringLen-1))
	  {
      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase();
      preString = tmpStr.substring(0,i+1);
      postString = tmpStr.substring(i+2,stringLen);
      tmpStr = preString + tmpChar + postString;
      }
    }
  }
}
outputString.value = tmpStr;
return;
}  
  

function lowWords(fieldName)
  {
    var inputString = document.getElementById(fieldName);
	var outputString = document.getElementById(fieldName);
	var tmpStr;
	tmpStr = inputString.value.toLowerCase();
	outputString.value = tmpStr;
    return;
  }


function upWords(fieldName)
  {
    var inputString = document.getElementById(fieldName);
	var outputString = document.getElementById(fieldName);
	var tmpStr;
	tmpStr = inputString.value.toUpperCase();
	outputString.value = tmpStr;
    return;
  }

function JumpTo(destination)	
  {
    //alert(destination);
    window.location = destination;
  }

function SubmitMessage(Message, Destination)
  {
    alert(Message);
	//alert("In SubmitIt!");
	window.location = Destination;
    return;
  }

function CloseWindow()
  {
	window.opener=self;
    window.close();  
  }

function CloseMessage(Message)
  {
    alert(Message);
    CloseWindow();
  }


function SelectText(FormName,Field)
  {
    var x=document.getElementById(FormName);
	x.elements[Field].focus();
	x.elements[Field].select(); 
    return;
  }
  
function AddUpper(FormName,Field1,Field2)
  {
    var x=document.getElementById(FormName);
    var boxval = x.elements[Field1].value;
	x.elements[Field2].value = boxval.toUpperCase();
    return;
  
  }
  
function get_cookie (cookie_name)
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
  //alert(results);
  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

function set_cookie ( name, value, plus_y, plus_m, plus_d, plus_s, path, domain, secure )
{
	
  /*
   This will set the cookie. It's the most complicated part, but actually for most purposes all you need to set are the first 4 parameters, name, value, expires, and path. If you want the cookie available only in one folder, you will need to add '/folder_name/' as a path variable, that tells the script to set the cookie for the just that path, not for the whole domain.

As a good general rule, set the path to '/', the root of your website. Generally 'domain' and 'secure' are not something you will be needing to use unless you set the cookie on a subdomain, in which case you may want to set the domain to your primary domain if you want it to be accessible from the main domain, or other subdomains. 
  */
  
  var cookie_string = name + "=" + escape ( value );
  var d=new Date();
  var timezone = d.getTimezoneOffset()/60;
  if (plus_s < 1) plus_s = 1;
//  alert(plus_d);
//  alert(plus_m);
//  alert(plus_y);
  if (plus_s)
    {
		
	
	  var totalsecs = plus_s;
	  var minuteadd = 0;
	  var houradd = 0;
	  var dayadd = 0;
	  var monthadd = 0;
      var addhour = Math.floor(totalsecs / 3600);
      var addmin = Math.floor((totalsecs - (addhour * 3600)) / 60);
      var addsec = (totalsecs - ((addhour * 3600) + (addmin * 60)))

	  var seconds =  d.getUTCSeconds() + addsec;	  

      if (seconds > 59)
	    {
		  minuteadd = Math.floor(seconds/60);
		  seconds = seconds - (minuteadd * 60);
		}
	  var minutes = d.getUTCMinutes() + addmin + minuteadd;
      if (minutes > 59)
	    {
		  houradd = Math.floor(minutes/60);
		  minutes = minutes - (houradd * 60);
		}
	  	  
      var hours = d.getUTCHours() + addhour + houradd - timezone;
	  //alert(d.getUTCHours());
	  //alert("hour: " + hours);
	  if (hours > 24)
	    {
		  dayadd = Math.floor(hours/24);
          hours =  hours - (dayadd * 24);
		}

	}
  else
    {
	  var seconds =  d.getUTCSeconds();
	  var minutes = d.getUTCMinutes();
	  var hours = d.getUTCHours()
	}
  if (plus_d)
    {
	  var day=d.getUTCDate() + plus_d + dayadd;	
	}
  else
    {
	  var day=d.getUTCDate() + dayadd;
	}

  if (plus_m)
    {
      var month=d.getUTCMonth() + plus_m;
	}
  else
    {
      var month=d.getUTCMonth();
	}

  if (plus_y)
    {
      var year=d.getUTCFullYear() + plus_y;
    }
  else
    {
	  var year=d.getUTCFullYear();
	}
      //alert("plus_s= " + plus_s);
      //alert("Year: " + year);
//      alert("Month: " + month);
  //    alert("Day: " + day);	  
    //  alert("hours: " + hours);
	  //alert("minutes: " + minutes);
//	  alert("seconds: " + seconds);
//alert(year+" "+month+" "+day+" "+hours+" "+minutes+" "+seconds);

  var expires = new Date (year,month,day,hours,minutes,seconds);
  //alert(expires);
  cookie_string += "; expires=" + expires.toUTCString();
//alert("Expires= " + expires);
var k = new Date();
//alert(k);

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  //alert(cookie_string);
  document.cookie = cookie_string;
  //alert(cookie_string);
  
  }

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

function getDivHeight(divName)
  {
	var D = document.getElementById(divName);
	var Safari, Safari2;
	if (isNaN(D.innerHeight)) Safari = 0;
    else Safari = D.innerHeight;
	if (isNaN(D.height)) Safari2 = 0;
    else Safari2 = D.height;
	
    return Math.max(D.scrollHeight, D.offsetHeight, D.clientHeight, Math.max(Safari, Safari2));
	
  }


function getDocHeight() {
    var D = document;
	var Safari, Safari2;
	if (isNaN(window.innerHeight)) Safari = 0;
    else Safari = window.innerHeight;
	if (isNaN(document.height)) Safari2 = 0;
    else Safari2 = document.height;
	
	//alert("safari= " + Safari);
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight),
		Math.max(Safari, Safari2)
		
    );
}

function setBodyHeight(DivName, newHeight)
  {
    var scrWid;
    var scnHei;
	var testHei;
	if (newHeight > 0)
	  {
		//alert(newHeight);  
		scnHei = newHeight;
	  }
	else scnHei = getDocHeight();
	
/*
  if (self.innerHeight) // all except Explorer
	{
		
		scnWid = self.innerWidth;
		scnHei = self.innerHeight;
		testHei = self.outerHeight;
		alert("test " + testHei);
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		scnWid = document.documentElement.clientWidth;
		scnHei = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		scnWid = document.body.clientWidth;
		scnHei = document.body.clientHeight;
	}
*/  

	//alert(scnHei);
    document.getElementById(DivName).style.height = scnHei + "px";
  }


function resizeEvent(evnt)
   {
	 var dheight = getDocHeight();  
	 if ( BrowserDetect.OS == "Mac" || BrowserDetect.OS == "Windows")
       {
         setBodyHeight("main", dheight);
       }

     //setBodyHeight("main", dheight);
     setBodyHeight("left_panel", dheight);
     setBodyHeight("right_panel", dheight);
     //setBodyHeight("content_wrapper");
//     setBodyHeight("default_home");	 
   }
   

function setOpacity(fadeobj, percent)
  {
	switch (BrowserDetect.browser)
	  {
	    case "Mozilla":
		  fadeobj.style.MozOpacity = percent / 100;
		  break;
		  
		case "Explorer":
		  fadeobj.style.filter = 'alpha(opacity=' + percent + ')';
	      break;
		case "Safari":
		  fadeobj.style.opacity = percent / 100;
		  break;
		default:
		  fadeobj.style.opacity = percent / 100; 
	  }  
	  
	  
  }


//slowly fades in a div from a background color
// 3 variables needed:
// var opacity = 0;
// var fadetime = 100;  in ms time between steps
// var faderatio = 5;  steps from 0% to 100% opacity
// requires div Id
function fade_in(thisDiv)
  {
	
    imgobj = document.getElementById(thisDiv);
    //alert(browserdetect);
    setOpacity(imgobj, opacity);
    highlighting=setInterval("gradualfade(imgobj)", fadetime);
  }

function fade_out(divName)
  {
    cleartimer();
    setOpacity(divName, opacity);
  }

function cleartimer()
  {
    if (window.highlighting) clearInterval(highlighting);
  }

function gradualfade(divName2)
  {

    var newopacity = opacity;
    setOpacity(imgobj, newopacity);

    opacity = opacity + faderatio;
	if (opacity > 100) opacity = 100;
    if (opacity == 100 && window.highlighting)
	  {
		setOpacity(imgobj, 100);
		clearInterval(highlighting);
		//finishfade();
	  }

  }



// Forces a div to 100% opacity instantly.
// needs div Id
function solid(thisDiv)
  {
	var newopacity = 100;
	imgobj = document.getElementById(thisDiv);
    setOpacity(imgobj, newopacity);
	finishfade();
  }
   
   
function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  // example = grayOut(true, {'opacity':'50'});
  
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}   

// BrowserDetect is used as follows:
// BrowserDetect.browser == "Explorer"  (which is Identity)
// BrowserDetect.OS == "Windows" (Which is Identity)
// BrowserDetect.version == "6" (Which is VersionSearch - it finds out version)
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


function isSet(variable)
  {
    return( typeof( variable ) != 'undefined' );
  }
  
/*********************************************************************
 * No onMouseOut event if the mouse pointer hovers a child element 
 * *** Please do not remove this header. ***
 * This code is working on my IE7, IE6, FireFox, Opera and Safari
 * 
 * Usage: 
 * <div onMouseOut="fixOnMouseOut(this, event, 'JavaScript Code');"> 
 *		So many childs 
 *	</div>
 *
 * @Author Hamid Alipour Codehead @ webmaster-forums.code-head.com		
**/
function is_child_of(parent, child) {
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}

function fixOnMouseOut(element, event, JavaScript_code) {
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		eval(JavaScript_code);
	}
}
/*********************************************************************/

/**
 * Function : dump()
 * Arguments: The data - array,hash(associative array),object
 *    The level - OPTIONAL
 * Returns  : The textual representation of the array.
 * This function was inspired by the print_r function of PHP.
 * This will accept some data as the argument and return a
 * text that will be a more readable version of the
 * array/hash/object that is given.
 * Docs: http://www.openjs.com/scripts/others/dump_function_php_print_r.php
 */
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

