/*Default Javascript*/
document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
	var classes = elem[i].className;
	if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
}; 
$(document).ready(function() {    
  
    //select all the a tag with name equal to modal  
    $('a[name=modal]').click(function(e) {  
        //Cancel the link behavior  
        e.preventDefault();  
        //Get the A tag  
        var id = $(this).attr('href');  
      
        //Get the screen height and width  
        var maskHeight = $(document).height()-20;  
        var maskWidth = $(window).width()-15;  
      
        //Set heigth and width to mask to fill up the whole screen  
        $('#mask').css({'width':maskWidth,'height':maskHeight});  
          
        //transition effect       
        $('#mask').fadeIn(1000);      
        $('#mask').fadeTo("slow",0.8);    
       
        //Get the window height and width  
        var winH = $(window).height();  
        var winW = $(window).width();  
                
        //Set the popup window to center  
        $(id).css('top',  winH/2-$(id).height()/2);  
        $(id).css('left', winW/2-$(id).width()/2);  
      
        //transition effect  
        $(id).fadeIn(2000);   
      
    });  
      
    //if close button is clicked  
    $('.window .close').click(function (e) {  
        //Cancel the link behavior  
        e.preventDefault();  
        $('#mask, .window').hide();  
    });       
      
    //if mask is clicked  
    $('#mask').click(function () {  
        $(this).hide();  
        $('.window').hide();  
    });           
      
});   

function numbersonly(e) {
	var unicode=e.charCode? e.charCode : e.keyCode;
	if (unicode!=8&&unicode!=46&&unicode!=9){if (unicode<48||unicode>57){return false;}}
}

function switch_divs(id,col,color){
	var colony;
	colony=col;
	coll=colony.split(",")
	for(i in coll){if(coll[i]!=id){if(document.getElementById(coll[i])){document.getElementById(coll[i]).style.outline='';}}}
	document.getElementById(id).style.outline='solid 2px '+color;
}

function highlight_class(classi,h){
	var css=document.getElementsByClassName(String(classi));
	for (i=0;i<css.length;i++){
		var c=css[i];
		if(h=="un"){c.style.outline="";}else{c.style.outline="thick solid #990000";}
	}
}

function setup_mce(editor){
	tinyMCE.init({
		mode : "specific_textareas",
		theme : "advanced",
		plugins : "safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,imagemanager,filemanager",
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code",
		theme_advanced_buttons3 : "insertdate,inserttime,preview,|,forecolor,backcolor,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr",
		theme_advanced_buttons4 : "tablecontrols,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		editor_selector : editor.valueOf()
	});
}

function occ_div(divid){	
	document.getElementById(divid).innerHTML='<img src="img/loading.gif" border="0" align="center" valgin="center" width="30px" height="30px"/>';
}

function run(divid,func,transaction_id,value,idd){
	if(!document.getElementById(divid)){
		document.write("<div id=\""+divid+"\">&nbsp;</div>");
	}
	occ_div(divid);
	AjaxRequest.get(
	  {
		'url':'go.php?_func='+func+"&transaction="+transaction_id+"&value="+value+"&id="+idd
		,'onSuccess':function(req){ document.getElementById(divid).innerHTML=req.responseText; }
		,'onError':function(req){ alert('Error!\nStatusText='+req.statusText+'\nContents='+req.responseText);}
	  }
	);
}

function set_el_value(id,val){
	div=document.getElementById(id);
	div.value=val;
}

function idfieldlimit(e) {
	var unicode=e.charCode? e.charCode : e.keyCode;
	if (unicode!=8&&unicode!=9&&unicode!=45&&unicode!=95){if (unicode<97||unicode>123){if(unicode<48||unicode>57){return false;}}}
}
function switch_image(id,im1,im2){
	div=document.getElementById(id);
	idsr=div.src;
	if(idsr==im1){div.src=im2;}
	if(idsr==im2){div.src=im1;}
	if(idsr!=im1&&idsr!=im2){div.src=im1;}
}

function check_password(val,im,off,on){
	pass=false;
	if(val.length>=6){pass=true;}
	if(pass==true){document.getElementById(im).src=on;}
	else{document.getElementById(im).src=off;}
}

function check_confirm_password(val,im,id,off,on){
	if(document.getElementById(id).value==val){document.getElementById(im).src=on;}
	else{document.getElementById(im).src=off;}
}

function confirm_msg(message,url){var r=confirm(message);if(r==true){redirect(url);}else{return false;}}

function update_exp(div){
	divid=document.getElementById(div);
	mo=document.getElementById("expmo").value;
	yr=document.getElementById("expyr").value;
	divid.value=mo+"-"+yr;
}


function redirect(url){
	var browser_type=navigator.appName
	var browser_version=parseInt(navigator.appVersion)	
	
	if(browser_type=="Netscape"&&browser_version>=5){
		window.location.replace(url)
	}else if (browser_type=="Microsoft Internet Explorer"&&browser_version>=4){
		window.location.replace(url)
	}else if(browser_type=="Netscape"&&browser_version>=4){
		window.location.replace(url)
	}else{
		window.location=url
	}
}

function show_div(divid){
     ID = document.getElementById(divid);
     if(ID.style.display == ""){ID.style.display = "none";}else{ID.style.display = "";}
}

function display_div(divid){
	d = document.getElementById(divid);
	if(d.style.visibility=="hidden"){d.style.visibility = "visible";}else{d.style.visibility = "hidden";}
}


function show_aux(parent, child){
  var p = document.getElementById(parent);
  var c = document.getElementById(child );
  var top  = (c["at_position"] == "y") ? p.offsetHeight+1 : 0;
  var left = (c["at_position"] == "x") ? p.offsetWidth +1 : 0;
  for (; p; p = p.offsetParent){
    top  += p.offsetTop;
    left += p.offsetLeft;
  }
  c.style.position   = "absolute";
  c.style.top        = top +'px';
  c.style.left       = left+'px';
  c.style.visibility = "visible";
}

function show_d(){
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);
  show_aux(p.id, c.id);
  clearTimeout(c["at_timeout"]);
}

function highlight(field) {
       field.focus();
       field.select();
}

function hide_d(){
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);
  c["at_timeout"] = setTimeout("document.getElementById('"+c.id+"').style.visibility = 'hidden'", 333);
}
function click_d(){
  var p = document.getElementById(this["at_parent"]);
  var c = document.getElementById(this["at_child" ]);
  if (c.style.visibility != "visible") show_aux(p.id, c.id); else c.style.visibility = "hidden";
  return false;
}

// PARAMETERS:
// parent   - id of the parent html element
// child    - id of the child  html element that should be droped down
// showtype - "click" = drop down child html element on mouse click
//            "hover" = drop down child html element on mouse over
// position - "x" = display the child html element to the right
//            "y" = display the child html element below
// cursor   - omit to use default cursor or specify CSS cursor name
function attach_div(parent, child, showtype, position, cursor){
  var p = document.getElementById(parent);
  var c = document.getElementById(child);
  p["at_parent"]     = p.id;
  c["at_parent"]     = p.id;
  p["at_child"]      = c.id;
  c["at_child"]      = c.id;
  p["at_position"]   = "x";
  c["at_position"]   = "y";
  c.style.position   = "absolute";
  c.style.visibility = "hidden";
  if (cursor != undefined) p.style.cursor = cursor;
  switch (showtype){
    case "click":
      p.onclick     = click_d;
      p.onmouseout  = hide_d;
      c.onmouseover = show_d;
      //c.onmouseout  = hide_d;
      break;
    case "hover":
      p.onmouseover = show_d;
      p.onmouseout  = hide_d;
      c.onmouseover = show_d;
      c.onmouseout  = hide_d;
      break;
  }
}
//MODAL WINDOW
function foverlay($id) {
	el = document.getElementById($id);
	el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}