// JavaScript Document
// loadDiv('left',  '<? print teq_encrypt('tree_panel'); ?>');

var clear="/shared/pngfix.gif" //path to clear.gif

pngfix=function(){var els=document.getElementsByTagName('*');var ip=/\.png/i;var i=els.length;while(i-- >0){var el=els[i];var es=el.style;if(el.src&&el.src.match(ip)&&!es.filter){es.height=el.height;es.width=el.width;es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+el.src+"',sizingMethod='crop')";el.src=clear;}else{var elb=el.currentStyle.backgroundImage;if(elb.match(ip)){var path=elb.split('"');var rep=(el.currentStyle.backgroundRepeat=='no-repeat')?'crop':'scale';es.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+path[1]+"',sizingMethod='"+rep+"')";es.height=el.clientHeight+'px';es.backgroundImage='none';var elkids=el.getElementsByTagName('*');if (elkids){var j=elkids.length;if(el.currentStyle.position!="absolute")es.position='static';while (j-- >0)if(!elkids[j].style.position)elkids[j].style.position="relative";}}}}}

function popup_div(img,w,h) {
	$('#popup_div').html("<img id='popup_img' src='/tmp"+img+"'>");
	width = $(window).width();
	height = $(window).height();

	x = (width/2)-(w/2);
	y = (height/2)-(h/2)+$(window).scrollTop();
	
	//$('#popup_fade').css('display','block');
	$('#popup_div').css('top',y).css('left',x).fadeIn();
}

function popup_div_no_fade(img,w,h) {
	$('#popup_div_no_fade').html("<img id='popup_img' src='/tmp"+img+"'>");
	width = $(window).width();
	height = $(window).height();
	x = (width/2)-(w/2);
	y = (height/2)-(h/2)+$(window).scrollTop();;
	$('#popup_div_no_fade').css('top',y).css('left',x).fadeIn();
}

function id_check(){
	var allTags = document.body.getElementsByTagName('*');
	var ids = [];
	for (var tg = 0; tg< allTags.length; tg++) {
		var tag = allTags[tg];
		if (tag.id) {
			if (ids[tag.id] && tag.id != ',ilink' && tag.id != 'filename') ids.push(tag.id+'\n');
			else ids[tag.id] = 1;
		}
	}
	alert(ids);
}

function toggle_checkbox(id) {
	el = document.getElementById(id); 
	if (!el) {
		walert("Cannot set checkbox, no such id ["+id+"]");
		return;
	}
	el.value = 1-el.value;
}

function Height() {
  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 myHeight;
}

function Width() {
  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;
}

function getHeight() {
  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 myHeight-25;
}

function saveFormId(fn){
	form = document.getElementById(fn);
	if (form) {
		$('#spinner').show();
		form.submit();
	} else alert('cannot save: bad form id: '+fn);
}

function conf(s){
		return window.confirm(s);
}

var activeEditors = new Array()

function activateEditor(id) {
    //alert("activate "+id);
	activeEditors[activeEditors.length] = id;
    toggleEditor(id);
}

function deactivateEditors() {
	//alert('active '+activeEditors.length);
    for(x=0;x<activeEditors.length;x++) {
		walert("Removing editor "+x);
        toggleEditor(activeEditors[x])
    }
    activeEditors.length = 0;
}

// functions
function toggleEditor(id) {
	var elm = document.getElementById(id);
	if (!elm) {
		walert("no such id "+id);
		return;
	}
	if (tinyMCE.getInstanceById(id) == null){
		walert('add' + id);
		tinyMCE.execCommand('mceAddControl', false, id);
	} else {
		walert('focus' + id);
		tinyMCE.execCommand('mceFocus', false, id);
		walert('remove' + id);
		tinyMCE.execCommand('mceRemoveControl', false, id);
	}
} 

function loadDiv(id,url,params){
	showId('spinner');
	alert("'loadDiv is deprecated");
	deactivateEditors();
	/* alert('Ready to load ' + id); */
	test = document.getElementById(id);
	if (!test) walert("Cannot load id ["+id+"]");
	else $("#"+id).load("/bin/load.php?u="+url+params);
}

function lD(id,url,params){
	loadDiv(id,url,params);
}

function AjaxRequest(id,page,params,repeat){
	if (typeof params == "undefined") params = "noparams=true";
	if (typeof repeat == "undefined") repeat = "";
	noiecache = new Date().getTime();
	new Ajax.Updater(id, page+'.php?'+params+'&noiecache='+noiecache, {evalScripts:true});
	if (repeat) setTimeout("AjaxRequest('"+id+"','"+page+"','"+params+"',"+repeat+")",repeat);
}

function set_valid(nm,is_valid){
	return;
	e = document.getElementById("valid_"+nm);
	if (!e) {
		walert("Cannot find id to display error looking for valid_"+nm);
	} else {
		if (is_valid) e.innerHTML = "";
		else e.innerHTML = "<img alt='This field is required.' src='/shared/cart_warning.gif'>";
	}
}

function err_row(nm,s){
	set_valid(nm,false);
	nm = nm.replace('_',' ');
	return "<li>"+nm+" "+s+"</li>\n";
}

function validate_form(formname){
	var txt = ""; 
	e = document.getElementById(formname);
	if (!e){
		walert("Cannot validate "+formname+" no such id.");
		return true;
	}
	err = document.getElementById('validate_errors');
	if (!err) {
		alert("No DIV id validate errors - please define one.");
		return false;
	}
	check_password = false;
	fields = e.elements;
	password = confirm_password = "";
	for(i=0; i<fields.length; i++)
	{
		nm  = fields[i].name;
		id  = fields[i].id;
		val = fields[i].value;
//		txt = txt + id + " " + nm + " " + "[" + val + "]<br>";
		if (id.length<8) continue;
		s = id.slice(0,8);
		if (s!="id_valid") continue;
		set_valid(nm,true);
		fields[i].style.background='#FFFFFF';
		if (id=="id_valid_not_null" && val.length==0) {
			fields[i].style.background='#FF7777';
			txt = txt + err_row(nm,"is a mandatory field.");
		}
		if (id=="id_valid_email" && !echeck(val)){
			fields[i].style.background='#FF7777';
			txt = txt + err_row(nm,"is not a valid email address.");
		}
		if (id=="id_valid_credit_card" && !checkcreditcard(val)){
			fields[i].style.background='#FF7777';
			txt = txt + err_row(nm,"is not a valid credit card number.");
		}
		if (id=="id_valid_terms"){
			val = fields[i].checked;
			if (val==false){
				alert("You must accept our terms and conditions before you can place your order.");
				return;
			}
		}
		if (nm=="password") password = val;
		if (nm=="confirm_password") {
			check_password = true;
			confirm_password = val;
		}
	}
	if (check_password && (password != confirm_password)){
		txt = txt + err_row(nm,"your passwords do not match");
		set_valid("password",false);
		set_valid("confirm_password",false);
	}
	if (txt) {
		setTimeout("hideId('validate_errors')",5000);
		err.innerHTML=txt
//		showId('validate_errors');
		return false;
	} else err.innerHTML="";
	return true;
}

function center(id)
{
	object = document.getElementById(id);
  
	var divT = (screen.Height - object.offsetHeight)/2;
    var divL = (screen.Width - object.offsetWidth)/2;
      
    object.style.left = divL + document.body.scrollLeft;
    object.style.top  = divT + document.body.scrollTop;
	
	showId(id);
}
 
function popup(URL,w,h) {
	aWindow=window.open(URL, "thewindow", "toolbar=no, width="+w+", height="+h+", status=no, scrollbars=yes, resize=no, menubars=no");
}

function popup_full(URL,w,h) {
	aWindow=window.open(URL, "thewindow", "toolbar=no, width="+w+", height="+h+", status=yes, scrollbars=yes, resize=yes, menubars=no");
}

function popup_clean(URL,w,h) {
	aWindow=window.open(URL, "", "toolbar=no, width="+w+", height="+h+", status=no, scrollbars=no, resize=no, menubars=no");
}

function popup_scroll(URL,w,h) {
	aWindow=window.open(URL, "", "toolbar=no, width="+w+", height="+h+", status=no, scrollbars=yes, resize=no, menubars=no");
}

function putFocus(formInst, elementInst) {
	if (document.forms.length > 0) {
		if (document.forms[formInst].elements[elementInst]) {
			document.forms[formInst].elements[elementInst].focus();
		}
	}
}

function placeFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
				document.forms[0].elements[i].focus();
				break;
			}
		}
	}
}

function highlight(state) {
	element=event.srcElement;
	clr=new Array('white','white','white','silver','red','green');
	if (element.tagName=='INPUT' || element.tagName=='TEXTAREA') {
		etype=element.type;
		if (etype!='radio' && etype!='checkbox' 
		&& etype!='submit' && etype!='reset' && etype!='file') element.style.backgroundColor=clr[state];
	}
}

function clear_help(){
	window.status=' ';
}

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1){
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1){
		return false;
	}
	
	if (str.indexOf(" ")!=-1){
		return false;
	}
	return true;					
}

function _CF_checkinteger(object_value)
{
	if (object_value.length == 0)
		return true;

	var decimal_format = ".";
	var check_char = object_value.indexOf(decimal_format);

	if (check_char == -1)
		return _CF_checknumber(object_value);
	else
		return false;
}

function _CF_numberrange(object_value, min_value, max_value)
{
	if (min_value != null)
	{
		if (object_value < min_value)
			return false;
	}

	if (max_value != null)
	{
		if (object_value > max_value)
			return false;
	}

	return true;
}

function _CF_checknumber(object_value)
{
	if (object_value.length == 0)
		return true;

	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

	check_char = start_format.indexOf(object_value.charAt(0));

	if (check_char == 1)
		decimal = true;
	else if (check_char < 1)
		return false;

	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i));
		if (check_char < 0)
			return false;
		else if (check_char == 1)
		{
			if (decimal)
				return false;
			else
				decimal = true;
		}
		else if (check_char == 0)
		{
			if (decimal || digits)	
				trailing_blank = true;
		}
		else if (trailing_blank)
			return false;
		else
			digits = true;
	}	

	return true
}

function checkcreditcard(object_value)
{
	if (object_value.length == 0)
		return false;
	var white_space = " -";
	var creditcard_string="";
	var check_char;

	for (var i = 0; i < object_value.length; i++)
	{
		check_char = white_space.indexOf(object_value.charAt(i));
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	

	if (creditcard_string.length < 13 || creditcard_string.length > 19)
		return false;

	if (creditcard_string.charAt(0) == "+")
		return false;

	if (!_CF_checkinteger(creditcard_string))
		return false;

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++)
	{
		tempdigit = eval(creditcard_string.charAt(i));

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
				checkdigit++;

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var debugId = false;	

function getListElemRefs( id )	{
  var el = (document.getElementById)? document.getElementById(id): (document.all)? document.all[id]: (document.layers)? getListLyrRef(id,document): null;
  if (el) el.css = (el.style)? el.style: el;
  return el;
}

function walert(s){
	window.status = s;
}

function malert(s){
	if (debugId) alert(s);
	else window.status = s;
}

function setDebugId(v){
	alert("debugId("+v+")");
	debugId=v;
}

function getId(id) 
{
	return $('#'+id);
	/*
	if (document.getElementById) 
		var returnVar = document.getElementById(id); 
	else if (document.all) 
		var returnVar = document.all[id]; 
	else if (document.layers) 
		var returnVar = document.layers[id]; 
	return returnVar; 
	*/
}

var lastId = 0;	
function statId(id){
	if (debugId) alert("statId "+id);
	if (!id) return;
	e = document.getElementById(id);
	if (!e) {
		malert("statId : "+id+" does not exist");
		return 0;
	}
	if (e.style.display =='none') return 0;
	else return 1;
}

function hideId(id){
	$('#'+id).hide();
}

function showId(id,mode){
	if (debugId) alert("showId "+id);
	var mode = (mode == null) ? 0 : mode;
	if (!mode) lastId = id;
	e = document.getElementById(id);
	if (!e) {
		malert("showId : "+id+" does not exist");
		return 0;
	}
	$('#'+id).show();
}

function toggleId(id,mode){
	if (debugId) malert("toggleId "+id);
	var mode = (mode == null) ? 0 : mode;
	if (!mode && lastId != id) hideId(lastId);
	$('#'+id).toggle();
}

function vislayer(){    
	this.lastId = '';
} 

vislayer.prototype.stat = function (id){
	if (debugId) alert("stat "+id);
	if (!id) return;
	e = document.getElementById(id);
	if (!e) {
		malert("stat : "+id+" does not exist");
		return 0;
	}
	if (e.style.display =='none') return 0;
	else return 1;
}

vislayer.prototype.show = function (id,mode){
	if (debugId) alert("showId "+id);
	var mode = (mode == null) ? 0 : mode;
	if (!mode) this.lastId = id;
	e = document.getElementById(id);
	if (!e) {
		malert("showId : "+id+" does not exist");
		return 0;
	}
	e.style.display = 'block';
	layer = getListElemRefs(id);
	layer.css.visibility = 'visible';
}

vislayer.prototype.hide = function (id){
	if (debugId) alert("hide "+id);
	if (!id) return;
	e = document.getElementById(id);
	if (!e) {
		malert("hide : "+id+" does not exist");
		return 0;
	}
	e.style.display = 'none';
	layer = getListElemRefs(id);
	layer.css.visibility = 'hidden';
}

vislayer.prototype.toggle = function (id,mode){
	if (debugId) malert("toggle "+id);
	var mode = (mode == null) ? 0 : mode;
	if (!mode && this.lastId != id) this.hide(this.lastId);
	e = document.getElementById(id);
	if (!e) {
		malert("toggle : "+id+" does not exist");
		return 0;
	}
	if (this.stat(id)) this.hide(id); else this.show(id,mode);
}

//obj = new vislayer(); 
//obj.show();
	
//set todays date -- backward compat - old date fields
Now = new Date();
NowDay = Now.getDate();
NowMonth = Now.getMonth();
NowYear = Now.getYear();
if (NowYear < 2000) NowYear += 1900; //for Netscape
//function for returning how many days there are in a month including leap years
function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == "3" || WhichMonth == "5" || WhichMonth == "8" || WhichMonth == "10") DaysInMonth = 30;
  if (WhichMonth == "1" && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == "1" && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}
//function to change the available days in a months
function ChangeOptionDays(DaysObject,Month,Year)
{
  DaysForThisSelection = DaysInMonth(Month, Year);
  CurrentDaysInSelection = DaysObject.length;
  if (CurrentDaysInSelection > DaysForThisSelection)
  {
	for (i=1; i<=(CurrentDaysInSelection-DaysForThisSelection); i++)
	{
	  DaysObject.options[DaysObject.options.length - 1] = null
	}
  }
  if (DaysForThisSelection > CurrentDaysInSelection)
  {
	for (i=1; i<=(DaysForThisSelection-CurrentDaysInSelection); i++)
	{
	  NewOption = new Option(DaysObject.options.length + 1);
	  DaysObject.add(NewOption);
	}
  }
	if (DaysObject.selectedIndex < 1) DaysObject.selectedIndex == 1;
}
//function to write option years plus x
function WriteYearOptions(from,to)
{
  if (from==0){
	from=NowYear;
	to=NowYear + 50;
  }
  line = "";

  for (i=from; i<=to; i++)
  {
	line += "<OPTION VALUE=" + (i) + ">" + i;
  }
  return line;
}