function print_popup(url) { 
 window.open(url,'Print', 'width=650, height=300 ,menubar=no,scrollbars=yes,toolbar=no,location=no,directories=no,resizable=yes,top=10,left=100'); 
}

function open_popup (url, width, height, name) {
  if(!name) name='Popup';
  window.open(url, name, 'width='+width+',height='+height+', menubar=no, scrollbars=yes, toolbar=no, location=no, directories=no, resizable=yes, top=100, left=100');
}

//exbcode javacript
var D=document;
function getSelectionInput(F) {
	return (D.selection?D.selection.createRange().text:F.value.substring(F.selectionEnd||0,F.selectionStart||0))
}

var p=document;
function exbcode_URL (u, d, what, p){
	add_content("[url="+(u=prompt("URL","http://"))+"]"+((s=getSelectionInput(what))?s:((d=prompt("Anchor",""))?d:""))+"[/url]", what, p)
}

function exbcode_URLS (u, d, what, p){
	add_content("[urls="+(u=prompt("URL","http://"))+"]"+((s=getSelectionInput(what))?s:((d=prompt("Anchor",""))?d:""))+"[/urls]", what, p)
}

function exbcode_B (what, p){
	add_content("[b]"+(getSelectionInput(what)?getSelectionInput(what):((d=prompt("Text",""))?d:""))+"[/b]", what, p)
}

function exbcode_U (what, p){
	add_content("[u]"+(getSelectionInput(what)?getSelectionInput(what):((d=prompt("Text",""))?d:""))+"[/u]", what, p)
}

function exbcode_I (what, p){
	add_content("[i]"+(getSelectionInput(what)?getSelectionInput(what):((d=prompt("Text",""))?d:""))+"[/i]", what, p)
}

function exbcode_SIZE (what, size, p){
	add_content("[size="+((size=prompt("Size","14"))?size:"14")+"]"+((s=getSelectionInput(what))?s:((d=prompt("Text",""))?d:""))+"[/size]", what, p);
}

function exbcode_CODE(what, code, p){
	add_content("[code="+((code=prompt("Code language","html"))?code:"html")+"]"+((s=getSelectionInput(what))?s:((d=prompt("Code Text",""))?d:""))+"[/code]", what, p);
}

function exbcode_COLOR(what, color, p){
	add_content("[color="+((color=prompt("Color","red"))?color:"red")+"]"+((s=getSelectionInput(what))?s:((d=prompt("Text",""))?d:""))+"[/color]", what, p);
}

function exbcode_QUOTE(what, user, p){
	add_content("[quote="+((user=prompt("User","Guest"))?user:"Guest")+"]"+((s=getSelectionInput(what))?s:((d=prompt("Text to Quote", ""))?d:""))+"[/quote]", what, p);
}

function exbcode_IMGA (what, align, p){
	add_content("[img="+((align=prompt("Align (left|right|center)","left"))?align:"left")+"]"+((s=getSelectionInput(what))?s:((d=prompt("Image source:","http://"))?d:"http://"))+"[/img]", what, p);
}

function exbcode_IMG (what, p){
	add_content("[img]"+((s=getSelectionInput(what))?s:((d=prompt("Image source:","http://"))?d:"http://"))+"[/img]", what, p);
}

function exbcode_ALIGN (what, align, p){
	add_content("[align="+((align=prompt("Align (left|right|center)","left"))?align:"left")+"]"+((s=getSelectionInput(what))?s:((d=prompt("Text to align:","Example text"))?d:""))+"[/align]", what, p);
}
//exbcode end

function add_content(txt, what, p){
	myQuery = what || document._form.text;
	myParent = p || document;

   //IE support
	if (myParent.selection) {
	    
 	   	myQuery.focugetSelectionInput(what);
        sel = myParent.selection.createRange();
        sel.text = txt;
        //myParent._form.insert.focugetSelectionInput(what);
		myQuery.focugetSelectionInput(what);
  }
  //MOZILLA/NETSCAPE support
  else if (myQuery.selectionStart || myQuery.selectionStart == "0") {
		var startPos = myQuery.selectionStart;
		var endPos = myQuery.selectionEnd;
		var chaineSql = myQuery.value;
 
		myQuery.value = chaineSql.substring(0, startPos) + txt + chaineSql.substring(endPos, chaineSql.length);
		sPos = startPos+(txt.length);
		myQuery.selectionStart = sPos; 
		myQuery.selectionEnd = sPos;
		myQuery.focus();
	}
	else {
        myQuery.value += txt;
        myQuery.focus();
    }  
}

function quoteSelection(author, what) {
	var txt = '';

	if (window.getSelection)
		txt = window.getSelection();
	else if (document.getSelection)
		txt = document.getSelection();
	else if (document.selection)
		txt = document.selection.createRange().text;
  
	if(txt != '') {
		theSelection='[quote='+author+']' + txt + '[/quote]';
		add_content(theSelection, what);	
	}
}

function deselect_all(id) {
  count = document.getElementById(id).length;
  for (i=0; i < count; i++)  document.getElementById(id).options[i].selected = 0;

}

function showhide(id, expand, collapse) {
  sw = document.getElementById( 'switch_'+id );
  chngds( 'multi_'+id );

  if(sw.innerHTML.indexOf(collapse) != -1)
  {
    sw.innerHTML = '<img src="'+expand+'" alt="" />';
  } else {
    sw.innerHTML = '<img src="'+collapse+'" alt="" />';
  }
}

function chngds(id) {
  var style = document.getElementById(id).style;
  style.display = style.display == "none"? "inline" : "none";
}

function mail (prefix, suffix) {
  document.location.href='mailto:' + prefix + '@' + suffix;
}


function screenWH (what) {
  var screenW = 640, screenH = 480;
  if (parseInt(navigator.appVersion)>3) {
    screenW = screen.width;
    screenH = screen.height;
  }
  else if (navigator.appName == "Netscape"  && parseInt(navigator.appVersion)==3   && navigator.javaEnabled()) {
    var jToolkit = java.awt.Toolkit.getDefaultToolkit();
    var jScreenSize = jToolkit.getScreenSize();
    screenW = jScreenSize.width;
    screenH = jScreenSize.height;
  }
  
  if(what=='H')
    return screenH;
  else
    return screenW;
}

var maxW=0;
function scaleImage(im, maxWidth, maxHeight) {
   if(!maxWidth)
     var maxWidth=500;
   if(!maxHeight)
     var maxHeight=0;
   
   maxW=maxWidth;
   
   if (typeof im.naturalWidth == 'undefined') {
     im.naturalWidth = im.width;
	 im.naturalHeight = im.height;
   }
   
   if(maxHeight<=0) {
      if (im.naturalWidth > maxWidth) {
         im.width = maxWidth;
         im.style.maxWidth = im.naturalWidth + 'px';
         im.onclick = unscaleImage;
	  }
   }
   else {
      if (im.naturalHeight > maxHeight) {
         im.height = maxHeight;
         im.style.maxHeight = im.naturalHeight + 'px';
         im.onclick = unscaleImage;
	  }      
   }
}

function unscaleImage() {
  if (this.width == maxW) {
    this.width = this.naturalWidth;
    this.style.borderWidth = '0px';
  }
  else {
    this.width = maxW;
    this.style.borderWidth = '1px';
  }
}

//select box
var currSelect=new Array(new Array(), new Array(), new Array(), new Array());
var defaultSelected=new Array();
function findInSelect(what, where, iter){
	if(currSelect[iter].length==0) {
		defaultSelected[iter]=where.selectedIndex;
		x=0;
		y=0;
		for (i=0;i<where.length;i++) {
			currSelect[iter][x]=where.options[i];
			
			if(where.options[i].text.toLowerCase().indexOf(what.toLowerCase())=='-1'){
				where.options[i] = null;
				i--;
				y++;
			} else { if(what) where.selectedIndex=0; }

			x++;
		}
		/*if there is no options to display, reset select*/
		if(y==x) resetSelect(where, iter);

	} else {
		resetSelect(where, iter);
		findInSelect(what, where, iter);
	}
}

//select box
function resetSelect(where, iter){
	
	if(currSelect[iter].length>0) {
		for (i=0; i<where.length;)	where.options[i] = null;
		for (i=0; i<currSelect[iter].length;i++) where.options[i] = new Option(currSelect[iter][i].text, currSelect[iter][i].value);
		where.selectedIndex=defaultSelected[iter];
		
		currSelect[iter]=new Array();
	}

	return true;
}

//check all checkbox items
function checkAll (id) {
	var el = document.getElementById(id);
	for (var i = 0; i < el.elements.length; i++)
	  el.elements[i].checked = !el.elements[i].checked;
}

//disable button
function disableIt(id) {
	if(document.getElementById(id).disabled==false) document.getElementById(id).disabled = true;
	else document.getElementById(id).disabled = false;
}	

//ajax
function submitFormCode(codeId, formId) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			document.getElementById(codeId).value=escape(http_request.responseText);
			document.getElementById(formId).submit();
		}
	}	
}

function makeRequest(url, callback) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Couldn\'t create http_request...');
        return false;
	}
	
	http_request.onreadystatechange = callback;
    http_request.open('GET', url, true);
	http_request.send(null);
}
	
function getCode(id, formId, submitId){
		if(submitId) disableIt(submitId);
		makeRequest('index.php?function=get_code', function() { submitFormCode(id, formId); } );
}