var jObjWindow = null;


/*
* Trim the given text
*/
function jTRIM(jParText){

	var jReturnStr = jParText;

	// cut space in the beginning
	var jFirstChar = jReturnStr.charAt(0);
	while (jFirstChar == " ")
	{
		jReturnStr = jReturnStr.substr(1, jReturnStr.length-1);
		jFirstChar = jReturnStr.charAt(0);
	}

	// cut "\n" in the beginning
	var jFirstTwo = parseInt(jReturnStr.charCodeAt(0));
	while (jFirstTwo == 13)
	{
		jReturnStr = jReturnStr.substr(2, jReturnStr.length-1);
		jFirstTwo = parseInt(jReturnStr.charCodeAt(0, 2));
	}

	// cut space in the end
	var jLastChar = jReturnStr.charAt(jReturnStr.length-1);
	while (jLastChar == " ")
	{
		jReturnStr = jReturnStr.substr(0, jReturnStr.length-1);
		jLastChar = jReturnStr.charAt(jReturnStr.length-1);
	}

	return jReturnStr;
}



/*
* Form field validation for text field
*/
function jCHECK_TEXT(jParField, jParMessage){

	if (jTRIM(jParField.value)=="")
	{
		alert(jParMessage);
		jParField.value = "";
		jParField.focus();
		return false;
	} else
	{
		return true;
	}

}



/*
* Form field validation for email
*/
function jCHECK_EMAIL(jParObj, jParMessage){

	var jSymbol = /^.+@.+\..{2,3}$/;
	if (jSymbol.test(jParObj.value))
	{
		return true;
	} else
	{
		alert(jParMessage);
		jParObj.focus();
		return false;
	}
}


/*
* Form field validation for password
*/
function jCHECK_PASSWORD(jParObj, jParMessage){

	var jSymbol = /^[A-Za-z0-9]{1,10}$/;
	if (jSymbol.test(jParObj.value))
	{
		return true;
	} else
	{
		alert(jParMessage);
		jParObj.focus();
		return false;
	}
}



/*
* Form field validation for filename
*/
function jCHECK_FILENAME(jParObj, jParMessage){

	var jSymbol = /[:<>%\#\\\/\|\*\?"]/;
	if (jSymbol.test(jParObj.value))
	{
		alert(jParMessage);
		jParObj.focus();
		return false;
	} else
	{
		return true;
	}
}



/*
* Set all checkbox within the given group to be checked
*/
function jSET_CHECKED(jParVal, jParObj, jParElementName){

	var jSize = jParObj.elements.length;
	var i = 0;

	for (i=0; i<jSize; i++)
	{
		if (jParObj.elements[i].name==jParElementName)
		{
			jParObj.elements[i].checked = jParVal;
		}
	}

}



/*
* Count the number of items checked among given item group
*/
function jCOUNT_CHECKED(jParObj, jParElementName){

	var jSize = jParObj.elements.length;
	var i=0;
	var j=0;

	for (i=0; i<jSize; i++)
	{
		if (jParObj.elements[i].name==jParElementName && jParObj.elements[i].checked)
		{
			j = j+1;
		}
	}

	return j;

}



/*
* Count the number of items checked among given item group
*/
function jPICK_SUMBIT(jParObj, jParElement, jParPage, jMessageNone, jMessageConfirm){

	if (jCOUNT_CHECKED(jParObj, jParElement)==0)
	{
		alert(jMessageNone)
	} else
	{
		if (confirm(jMessageConfirm))
		{
			jParObj.action = jParPage;
			jParObj.submit();
		}
	}
}



/*
* Count the number of items checked among given item group
* only one item selection is allowed
*/
function jPICK_SUBMIT_ONE(jParObj, jParElement, jParPage, jMessageNotOne){

	if (jCOUNT_CHECKED(jParObj, jParElement)!=1)
	{
		alert(jMessageNotOne);
	} else
	{
		jParObj.action = jParPage;
		jParObj.submit();
	}
}



/*
* Parse selection options
*/
function jPARSE_OPTION(jParObj){

	var i;

	for(i=0; i<jParObj.length; i++)
	{
		if(jParObj.options[i].value=='')
		{
			jParObj.options[i] = null;
		}
	}
}



/*
* Add selected options from one select box to another one
*/
function jSELECT_ADDTO(jParObjFrom, jParObjTo){

	var i;

	jPARSE_OPTION(jParObjFrom);
	jPARSE_OPTION(jParObjTo);
	i = jParObjFrom.selectedIndex;

	while (i!=-1)
	{
		jParObjTo.options[jParObjTo.length] = new Option(jParObjFrom.options[i].text, jParObjFrom.options[i].value, false, false);
		jParObjFrom.options[i] = null;
		i = jParObjFrom.selectedIndex;
	}

}



/*
* Select all options available in select box
*/
function jSELECT_ALL_OPTION(jParObj){

	var i;

	jPARSE_OPTION(jParObj);

	for (i=0; i<jParObj.length; i++)
	{
		jParObj.options[i].selected = true;
	}
}



/*
* Display or hide contents
*/
function jDISPLAY_ITEM(jParItemID, jStyle){

	//block, none, inline
	var jCurrentStyle = document.all[jParItemID].style.display;
	var jStyleNew = "none";
	if (typeof(jStyle)!="undefined")
	{
		jStyleNew = jStyle;
	} else
	{
		jStyleNew = (jCurrentStyle=="none") ? "block" : "none";
	}

	document.all[jParItemID].style.display = jStyleNew;

	return;
}



/*
* Popup a new window
*/
function jPOP_WIN(jParURL, jWinType){

	var jWindowName = "popup_name_" + jWinType;

	var jWindowSize = "";

	if (jWinType=="xs")
	{
		jWindowSize = "resizable,scrollbars,status=0,top=40,left=40,width=200,height=200";
	} else if (jWinType=="s")
	{
		jWindowSize = "resizable,scrollbars,status=0,top=40,left=40,width=350,height=350";
	} else if (jWinType=="m")
	{
		jWindowSize = "resizable,scrollbars,status=0,top=40,left=40,width=500,height=500";
	} else if (jWinType=="l")
	{
		jWindowSize = "resizable,scrollbars,status=0,top=40,left=40,width=650,height=550";
	} else if (jWinType=="xl")
	{
		jWindowSize = "resizable,scrollbars,status=0,top=40,left=40,width=800,height=600";
	} else if (jWinType=="print")
	{
		jWindowSize = "menubar,resizable,scrollbars,top=40,left=40,width=640,height=480";
	} else
	{
		jWindowSize = "resizable,scrollbars,status=0,top=40,left=40,width=400,height=400";
	}

	if (navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion >="4")

	if (jObjWindow)
	{
		jObjWindow.close();
	}

	jObjWindow = window.open(jParURL, jWindowName, jWindowSize);

	return;
}



/*
* Rename the folder
*/
function jRENAME_FOLDER(jParObj, jParPage){

	jParObj.action = jParPage;
	jParObj.submit();
}


/*
* Delete the folder
*/
function jDELETE_FOLDER(jParObj, jParPage, jMessageConfirm){

	if (confirm(jMessageConfirm))
		{
			jParObj.action = jParPage;
			jParObj.submit();
		}
}

/*
* Delete the Scheme
*/
function jDELETE_SCHEME(jParObj, jParPage, jMessageConfirm){

	if (confirm(jMessageConfirm))
		{
			jParObj.action = jParPage;
			jParObj.submit();
		}
}


/*
* Confirm then submit
*/
function jCOMFIRM_SUBMIT(jParObj, jParPage, jMessageConfirm){

	if (confirm(jMessageConfirm))
		{
			jParObj.action = jParPage;
			jParObj.submit();
		}
}


/*
* Return the file path to the text field
*/
function jSELECT_FILE(jParURL, jParChangeField){
	window.opener.document.getElementById(jParChangeField).value = jParURL;
	window.close();
}

/*
* functions to change images
*/

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 EvalSound(soundobj) {
  var thissound= eval("document."+soundobj);
  thissound.Play();
}

function StopSound(soundobj) {
  var thissound= eval("document."+soundobj);
  thissound.Stop();
}


var __isIE =  navigator.appVersion.match(/MSIE/);  
var __userAgent = navigator.userAgent;  
var __isFireFox = __userAgent.match(/firefox/i);  
var __isFireFoxOld = __isFireFox && (__userAgent.match(/firefox\/2./i) || __userAgent.match(/firefox\/1./i));  
var __isFireFoxNew = __isFireFox && !__isFireFoxOld;  
   
   
function __parseBorderWidth(width) {  
    var res = 0;  
    if (typeof(width) == "string" && width != null && width != "" ) {  
        var p = width.indexOf("px");  
        if (p >= 0) {  
            res = parseInt(width.substring(0, p));  
        }  
        else {  
            //do not know how to calculate other values (such as 0.5em or 0.1cm) correctly now  
            //so just set the width to 1 pixel  
            res = 1;   
        }  
    }  
    return res;  
}  
  
   
//returns border width for some element  
function __getBorderWidth(element) {  
    var res = new Object();  
    res.left = 0; res.top = 0; res.right = 0; res.bottom = 0;  
    if (window.getComputedStyle) {  
        //for Firefox  
        var elStyle = window.getComputedStyle(element, null);  
        res.left = parseInt(elStyle.borderLeftWidth.slice(0, -2));    
        res.top = parseInt(elStyle.borderTopWidth.slice(0, -2));    
        res.right = parseInt(elStyle.borderRightWidth.slice(0, -2));    
        res.bottom = parseInt(elStyle.borderBottomWidth.slice(0, -2));    
    }  
    else {  
        //for other browsers  
        res.left = __parseBorderWidth(element.style.borderLeftWidth);  
        res.top = __parseBorderWidth(element.style.borderTopWidth);  
        res.right = __parseBorderWidth(element.style.borderRightWidth);  
        res.bottom = __parseBorderWidth(element.style.borderBottomWidth);  
    }  
     
    return res;  
}  
  
//returns absolute position of some element within document  
function getElementAbsolutePos(element) {  
    var res = new Object();  
    res.x = 0; res.y = 0;  
    if (element !== null) {  
        res.x = element.offsetLeft;  
        res.y = element.offsetTop;  
          
        var offsetParent = element.offsetParent;  
        var parentNode = element.parentNode;  
        var borderWidth = null;  
  
        while (offsetParent != null) {  
            res.x += offsetParent.offsetLeft;  
            res.y += offsetParent.offsetTop;  
              
            var parentTagName = offsetParent.tagName.toLowerCase();   
  
            if ((__isIE && parentTagName != "table") || (__isFireFoxNew && parentTagName == "td")) {              
                borderWidth = __getBorderWidth(offsetParent);  
                res.x += borderWidth.left;  
                res.y += borderWidth.top;  
            }  
              
            if (offsetParent != document.body && offsetParent != document.documentElement) {  
                res.x -= offsetParent.scrollLeft;  
                res.y -= offsetParent.scrollTop;  
            }  
  
            //next lines are necessary to support FireFox problem with offsetParent  
            if (!__isIE) {  
                while (offsetParent != parentNode && parentNode !== null) {  
                    res.x -= parentNode.scrollLeft;  
                    res.y -= parentNode.scrollTop;  
                      
                    if (__isFireFoxOld) {  
                        borderWidth = kGetBorderWidth(parentNode);  
                        res.x += borderWidth.left;  
                        res.y += borderWidth.top;  
                    }  
                    parentNode = parentNode.parentNode;  
                }      
            }  
  
            parentNode = offsetParent.parentNode;  
            offsetParent = offsetParent.offsetParent;  
        }  
    }  
    return res;  
} 


function innerSize() {
	var res = new Object();  
    res.Width = 0; res.Height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		res.Width = window.innerWidth; res.Height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth ||document.documentElement.clientHeight ) ) {
		res.Width = document.documentElement.clientWidth; res.Height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		res.Width = document.body.clientWidth; res.Height = document.body.clientHeight;
	}
	return res;
}

function hideMidiStops(n, obj)
{
	var y = document.getElementsByName("midcontent");
  	for (var i = 0; i < y.length; i++)
  	{
	  	y[i].style.display = 'inline';
	}
	
	var x = document.getElementsByName("stopmid");
  	for (var i = 0; i < x.length; i++)
  	{
	  	x[i].style.display = (i == n) ? 'inline' : 'none';
	}
	obj.style.display = 'none';
}

function stopMidi(obj)
{
	var x = document.getElementsByName("midcontent");
  	for (var i = 0; i < x.length; i++)
  	{
	  	x[i].style.display = 'inline';
	}
	obj.style.display = 'none';
}
