﻿String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

Array.prototype.contains = function(needle, ti) {
	if (!ti) return this.indexOf(needle) != -1;
	return this.some(function(item) {
		return (item == needle);
	});
}

// File: copy-code.js
// Requires: prototype.js, scriptaculous.js?load=effects

function copyToClipboard(elt) {
	var urlSwf = "/Flash/_clipboard.swf";
	var strMssgBoxId = "notifyTextCopied";
	var eltNotify = null;

	// Display Notifications
	if((eltNotify = call(strMssgBoxId)) == null){
		// Attach the notification to the DOM
		var eltBody = document.getElementsByTagName('body').item(0);

		eltNotify = document.createElement('div');
		eltNotify.setAttribute('id', strMssgBoxId);
		eltNotify.style.display = 'none';

		eltNotify.innerHTML = 'Đã copy vào clipboard';

		eltBody.appendChild(eltNotify);
	}
	elt.onblur =
		function(e){
			Element.hide(eltNotify);
			return true;
		}

	var z = Position.cumulativeOffset(elt);
	var x = z[0];
	var y = z[1];

	Element.show(eltNotify);

	if(navigator.appName == 'Microsoft Internet Explorer'){
		if(x < 100){
			eltNotify.style.left = (x + (elt.offsetWidth - 5)) + 'px';
		}
		else{
			eltNotify.style.left = (x - (eltNotify.offsetWidth + 6)) + 'px';
		}
	}
	else{
		if(x < 100){
			eltNotify.style.left = (x + (elt.offsetWidth + 3)) + 'px';
		}
		else{
			eltNotify.style.left = (x - (eltNotify.offsetWidth + 2)) + 'px';
		}
	}

	eltNotify.style.top = y + 'px';

	var xEffect = Effect.Fade(eltNotify, { fps: 120, from: 1.9, to: 0.0, duration: 1.5, queue: 'front' } );
	window.status = 'Copied text to clipboard';

	// Copy the text inside the text box to the user's clipboard
	var flashcopier = 'flashcopier';
	if(!call(flashcopier)){
		var divholder = document.createElement('div');
		divholder.id = flashcopier;
		document.body.appendChild(divholder);
	}

	call(flashcopier).innerHTML = '';
	var divinfo = '<embed src="' + urlSwf + '" FlashVars="clipboard='+escape(elt.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
	call(flashcopier).innerHTML = divinfo;

	elt.select();

	return true;
}



//onload="img_resize(this);"
function img_resize(obj)
{

     var pHeight = obj.parentNode.style.height;
     pHeight = Number (pHeight.replace("px",""));
     var pWidth = obj.parentNode.style.width;
     pWidth = Number (pWidth.replace("px",""));
     
     var myWidth = obj.width;
     var myHeight = obj.height;
     
     var maxDimension = pHeight;
     if (myWidth > myHeight) maxDimension  = pWidth;
 
    var newWidth = parseFloat (myWidth);
    var newHeight = parseFloat (myHeight);

    if (myWidth < maxDimension && myHeight < maxDimension)
    {
    //okie, donothing
    } else //resizing
    {
        var aspect = myWidth/myHeight;
        //alert (aspect);
        if (aspect <= 1)
        {
            newWidth = parseFloat (parseFloat (maxDimension) * aspect);
            newHeight = maxDimension;
        } else
        {
            newWidth = maxDimension;
            newHeight = parseFloat (parseFloat (maxDimension) / aspect);
        }
    }
    
    obj.width = newWidth;
    obj.height = newHeight;
    if (myWidth > myHeight) obj.height = pHeight;
}


function getValueFromDrop(id){
    obj = document.getElementById(id);
    return obj.options[obj.selectedIndex].value;
}

function applyCssPath (id, path)
{
    var objHead = document.getElementsByTagName("head").item(0);
    var e =  null;
    if ($(id) == null)
    {
        e = new Element('link', {'id':String (id), 'href': String(path) + '?' + String (Math.random()) ,'type':'text/css', 'rel':'stylesheet' });
        objHead.appendChild (e);
    }
    else
    { 
        e = $(id);    
        e.href = String(path) + '?' + String (Math.random());
    }
}

function GetElement(id)
{
var itm=null;
 if (document.getElementById) {
		itm = document.getElementById(id);
	  } else if (document.all){
		itm = document.all[id];
	  } else if (document.layers){
		itm = document.layers[id];
	  }
	  
return itm;
}

function HideItem(id) {
	  var itm = null;
	  itm=GetElement(id);
	
	  if (!itm) {
	   // do nothing
	  }
	  else  if (itm.style){itm.style.display = "none"; }
	        else { itm.visibility = "none"; }
	}
function ShowItem(id) {
	  var itm = null;
	  if (GetElement) {
		itm = GetElement(id);
	  } else if (document.all){
		itm = document.all[id];
	  } else if (document.layers){
		itm = document.layers[id];
	  }
	
	  if (!itm) {
	   // do nothing
	  }
	  else  if (itm.style){itm.style.display = ""; }
	        else { itm.visibility = "show"; }
	}

function showDialog(vLink, vWidth, vHeight)
{
return showWindow(vLink, false, true, true, false, false, false, true, true, vWidth, vHeight, 0, 0);
}

function showModalDialog(vLink, vWidth, vHeight)
{
return showWindow(vLink, false, false, false, false, false, false, true, true, vWidth, vHeight, 0, 0);
}

function showWindow(vLink, vStatus, vResizeable, vScrollbars, vToolbar, vLocation, vFullscreen, vTitlebar, vCentered, vWidth, vHeight, vTop, vLeft)
{
    var sLink = (typeof(vLink.href) == 'undefined') ? vLink : vLink.href;

    winDef = '';
    winDef = winDef.concat('status=').concat((vStatus) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('resizable=').concat((vResizeable) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('scrollbars=').concat((vScrollbars) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('toolbar=').concat((vToolbar) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('location=').concat((vLocation) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('fullscreen=').concat((vFullscreen) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('titlebar=').concat((vTitlebar) ? 'yes' : 'no').concat(',');
    winDef = winDef.concat('height=').concat(vHeight).concat(',');
    winDef = winDef.concat('width=').concat(vWidth).concat(',');

    if (vCentered)
    {
	    winDef = winDef.concat('top=').concat((screen.height - vHeight)/2).concat(',');
	    winDef = winDef.concat('left=').concat((screen.width - vWidth)/2);
    }
    else
    {
	    winDef = winDef.concat('top=').concat(vTop).concat(',');
	    winDef = winDef.concat('left=').concat(vLeft);
    }

    open(sLink, '_blank', winDef);

    if (typeof(vLink.href) != 'undefined')
    {
	    return false;
    }
}

function SelectAllCheckboxes(theBox,likename)
	{
		xState = theBox.checked;
		elm = theBox.form.elements;

		for (i = 0; i < elm.length; i++)
			if (elm[i].type == "checkbox" && elm[i].id != theBox.id)				
			{			    
			    if (elm[i].id.indexOf(likename) > 0)
				{
				   
				    if (elm[i].checked != xState)
						elm[i].click();
				}
				
			}
	}
	
	
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];}
}


function InsertTags(ctrl, tagOpen, tagClose, sampleText) {
	var txtarea;
	txtarea = $(ctrl);
	// IE
	if (document.selection  ) {
		var theSelection = document.selection.createRange().text;
		if (!theSelection) {
			theSelection=sampleText;
		}
		txtarea.focus();
		if (theSelection.charAt(theSelection.length - 1) == " ") { // exclude ending space char, if any
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
		} else {
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
		}

	// Mozilla
	} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
		var replaced = false;
		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		if (endPos-startPos) {
			replaced = true;
		}
		var scrollTop = txtarea.scrollTop;
		var myText = (txtarea.value).substring(startPos, endPos);
		if (!myText) {
			myText=sampleText;
		}
		var subst;
		if (myText.charAt(myText.length - 1) == " ") { // exclude ending space char, if any
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " ";
		} else {
			subst = tagOpen + myText + tagClose;
		}
		txtarea.value = txtarea.value.substring(0, startPos) + subst +
			txtarea.value.substring(endPos, txtarea.value.length);
		txtarea.focus();
		//set new selection
		if (replaced) {
			var cPos = startPos+(tagOpen.length+myText.length+tagClose.length);
			txtarea.selectionStart = cPos;
			txtarea.selectionEnd = cPos;
		} else {
			txtarea.selectionStart = startPos+tagOpen.length;
			txtarea.selectionEnd = startPos+tagOpen.length+myText.length;
		}
		txtarea.scrollTop = scrollTop;

	// All other browsers get no toolbar.
	// There was previously support for a crippled "help"
	// bar, but that caused more problems than it solved.
	}
	// reposition cursor if possible
	if (txtarea.createTextRange) {
		txtarea.caretPos = document.selection.createRange().duplicate();
	}
}
