
// dhtml.js   -  colin@verot.net  -  2005

//    var f = document.getElementById(id);
//    f.funcname1 = funcname1;
//    f.funcname2 = funcname2;
//    cmf_addEvent(f, 'keyup', 'funcname1');
//    cmf_addEvent(f, 'change', 'funcname2');
function cmf_addEvent(obj,type,fct) {
    if (obj.addEventListener) { 
        obj.addEventListener(type, function(e){ obj[fct](e);}, false);
    } else if (obj.attachEvent) {
        obj.attachEvent('on'+type,function(e){ obj[fct](e); }); 
    } else {
        var cmf_Prev=obj["on" + type];
        if (cmf_Prev) {
            obj['on'+type]=function(e){ cmf_Prev(e); obj[fct](e); };
        } else { 
            obj['on'+type]=obj[fct]; 
        }
    }
}

// toggle all checkboxes
function cmf_togglecheckboxes(obj, name) {
    var f = obj.form, z = 0;
    
    z=5;
    for(z=0; z<f.length;z++){
    //alert(f[z].name+' '+f[z].value);
    var reg = new RegExp("^"+name+"\\[(.*)\\]$", "i");

   
        if (f[z].type == 'checkbox' && reg.test(f[z].name)) {
	        f[z].checked = obj.checked;
	    }
    }
}


// togglediv
function cmf_togglediv(container, obj) {
    var e = document.getElementById(obj);
    if(e.style.display != "block") {
        e.style.display = "block";
        jscss('add', container, 'cmf-togglediv-open')
    } else {
        e.style.display = "none";
        jscss('remove', container, 'cmf-togglediv-open')
    }
}

// add CSS classes
function jscss(a,o,c1,c2) {
  switch (a){
    case 'swap':
      o.className=!jscss('check',o,c1)?o.className.replace(c2,c1): o.className.replace(c1,c2);
    break;
    case 'add':
      if(!jscss('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

// autodiv
var last_toggle_obj = '';
function cmf_autodiv(obj) {
    var e = document.getElementById(last_toggle_obj);
    if (e) e.style.display = "none";
    var e = document.getElementById(obj);
    if(e.style.display != "block") {
        e.style.display = "block";
        last_toggle_obj = obj;
    } else {
        e.style.display = "none";
    }
}


// populink
function cmf_popuplink(link, name, height, width, location, status, scrollbars, menubars, toolbars, resizable) {
    var w = window.open(link, name, "height="+height+",  width="+width+", location="+location+", status="+status+",  scrollbars="+scrollbars+", menubars="+menubars+",  toolbars="+toolbars+", resizable="+resizable+"" );
    if (w.opener == null) w.opener = self;
    return true; 
}


// dynamic_list
function cmf_dynamic_list() { 
    var dlist = document.getElementById(this.dynamic_list_id);
    var list = eval(this.list_id);
    var sel = this.options[this.selectedIndex].value;
    if (!sel) return;
    var list = list[sel];
    dlist.options.length = 0;
    for(i=0;i<list.length;i+=1) {
    	dlist.options[i] = new Option(list[i][1],list[i][0]);
    }    
}


// text_insert
function cmf_insertAtCursor(myID, myValue) {
    var myField = document.getElementById(myID);
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
		myField.focus();
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		var scrollTop = myField.scrollTop;
		myField.value = myField.value.substring(0, startPos)
		              + myValue 
                      + myField.value.substring(endPos, myField.value.length);
		myField.focus();
		myField.selectionStart = startPos + myValue.length;
		myField.selectionEnd = startPos + myValue.length;
		myField.scrollTop = scrollTop;
	} else {
		myField.value += myValue;
		myField.focus();
	}
}



// character counter
function characterCounter_EventAdd(obj,type,fct) {
 if ( obj.addEventListener ){ obj.addEventListener(type, function(e){ obj[fct](e);}, false); }
 else if ( obj.attachEvent ){ obj.attachEvent('on'+type,function(e){ obj[fct](e); }); }
 else {
  var cmf_Prev=obj["on" + type];
  if (cmf_Prev){ obj['on'+type]=function(e){ cmf_Prev(e); obj[fct](e); }; }
  else { obj['on'+type]=obj[fct]; }
 }
}
function setCharacterCounter(field, status, max, min) {
	var f = document.getElementById(field);
	var s = document.getElementById(status);
    f.characterCounter = characterCounter;
    f.characterCounterMax = max;
    f.characterCounterMin = min;
    f.characterCounterStatus = status;
    characterCounter_EventAdd(f, 'keyup', 'characterCounter');
    characterCounter_EventAdd(f, 'change', 'characterCounter');
	f.characterCounter();
}
function characterCounter() {
	maxLength = this.characterCounterMax;
	minLength = this.characterCounterMin;
	statusSpan = document.getElementById(this.characterCounterStatus);
	var currentLength = this.value.length;
	var content = '';
	if (maxLength && minLength) {
	    content = 'Requires between '+minLength+' and '+maxLength+' characters';
	} else if (maxLength) {
	    content = 'Requires '+maxLength+' characters maximum';
	} else if (minLength) {
	    content = 'Requires '+minLength+' characters minimum';
	}	
	// can add  ('+currentLength+')
	if (maxLength && currentLength > maxLength && maxLength > 0) {
	    content = '<span class="cmf-charactercounter-warning">' + content + '</span>';
	} else if (minLength && currentLength < minLength) {
	    content = '<span class="cmf-charactercounter-warning">' + content + '</span>';
	} else {
	    content = '';
    }
    statusSpan.innerHTML = content;
}




// scroller.js
var swap_speed = 3000;
var scroll_speed = 50;
var scroll_step = 5;
var currentDiv=0;
var nextDiv=0;
var totalDivs=0;
// hides and position all divs
function hideall(){
    var cpt=0;
    while (document.getElementById("scrollerdiv"+cpt)) {
        document.getElementById("scrollerdiv"+cpt).style.display="none";
        document.getElementById("scrollerdiv"+cpt).style.position="absolute";
        document.getElementById("scrollerdiv"+cpt).style.top = "0px";
        cpt++;
    }
}
// to swap divs
function swap() {
    var currentDivObj=document.getElementById("scrollerdiv"+currentDiv);
    hideall();
    currentDivObj.style.display="block";
    currentDiv=(currentDiv<totalDivs-1)? currentDiv+1 : 0;
    setTimeout("swap()",swap_speed);
}
// to scroll divs
function scroll() {
    var currentDivObj=document.getElementById("scrollerdiv"+currentDiv);
    var nextDivObj=document.getElementById("scrollerdiv"+nextDiv);
    var scrollerDivObj=document.getElementById("cmf-scroller");
    if ((parseInt(currentDivObj.offsetHeight) + parseInt(currentDivObj.style.top) + 10) >= scrollerDivObj.offsetHeight) {
        currentDivObj.style.top=parseInt(currentDivObj.style.top)-scroll_step+"px";
        nextDivObj.style.top=parseInt(nextDivObj.style.top)-scroll_step+"px";
        setTimeout("scroll()",scroll_speed)
    } else if ((parseInt(currentDivObj.offsetHeight) + parseInt(currentDivObj.style.top) + 10) < scrollerDivObj.offsetHeight) {
        currentDivObj.style.top=parseInt(currentDivObj.style.top)-scroll_step+"px";
        if (nextDiv != currentDiv) document.getElementById("scrollerdiv"+nextDiv).style.display="none";
        nextDiv=(currentDiv<totalDivs-1)? currentDiv+1 : 0;
        document.getElementById("scrollerdiv"+nextDiv).style.top=scrollerDivObj.offsetHeight+"px";
        document.getElementById("scrollerdiv"+nextDiv).style.display="block";
        var tmpDiv = nextDiv;
        nextDiv = currentDiv;
        currentDiv = tmpDiv;
        setTimeout("scroll()",scroll_speed)
    } 
}
function cmf_scroller(type, scroll_spd, scroll_stp, swap_spd){
    scroll_speed=scroll_spd;
    scroll_step=scroll_stp;
    swap_speed=swap_spd;
    while (document.getElementById("scrollerdiv"+totalDivs)!=null)
        totalDivs++;
    hideall();
    if (totalDivs > 0) {
        if (type == 'swap') {
            document.getElementById("scrollerdiv0").style.display="block";
            swap();
        } else {
            document.getElementById("scrollerdiv0").style.display="block";
            document.getElementById("scrollerdiv0").style.top=(document.getElementById("cmf-scroller").offsetHeight - 10)+"px";
            scroll();
        }
    }
}
