var detect = navigator.userAgent.toLowerCase();
var is_ie = detect.indexOf("msie") + 1 > 0 ? true : false;
var is_firefox = detect.indexOf("firefox") + 1 > 0 ? true : false;
var is_safari= detect.indexOf("safari") + 1 > 0 ? true : false;

pagenow = window.parent.location.toString().split('/');
pagenow = pagenow[pagenow.length-1].split('#');

//alert(window.parent.iframe + ' - ' + pagenow)
if ( pagenow[0] != 'index.php' && !window.parent.iframe)
{
	//alert('Redirect')
	window.location.href = './index.php#' + pagenow[0]
}
/*if ( pagenow != 'index.php' )
{
	window.location.href = './index.php#' + pagenow
	//window.location = './#' + file
}*/

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_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 showMe(obj) {
	if (document.getElementById(obj).style.display == 'block')
		document.getElementById(obj).style.display = 'none';
		//document.getElementById(obj).style.background-image = 'images/SubPages/but-subMenu_Down.gif';
	else document.getElementById(obj).style.display = 'block';
		//document.getElementById(obj).style.background-image = 'images/SubPages/but-subMenu_Up.gif';
}
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 YY_checkform() { //v4.71
//copyright (c)1998,2002 Yaromat.com
  var a=YY_checkform.arguments,oo=true,v='',s='',err=false,r,o,at,o1,t,i,j,ma,rx,cd,cm,cy,dte,at;
  for (i=1; i<a.length;i=i+4){
    if (a[i+1].charAt(0)=='#'){r=true; a[i+1]=a[i+1].substring(1);}else{r=false}
    o=MM_findObj(a[i].replace(/\[\d+\]/ig,""));
    o1=MM_findObj(a[i+1].replace(/\[\d+\]/ig,""));
    v=o.value;t=a[i+2];
    if (o.type=='text'||o.type=='password'||o.type=='hidden'){
      if (r&&v.length==0){err=true}
      if (v.length>0)
      if (t==1){ //fromto
        ma=a[i+1].split('_');if(isNaN(v)||v<ma[0]/1||v > ma[1]/1){err=true}
      } else if (t==2){
        rx=new RegExp("^[\\w\.=-]+@[\\w\\.-]+\\.[a-zA-Z]{2,4}$");if(!rx.test(v))err=true;
      } else if (t==3){ // date
        ma=a[i+1].split("#");at=v.match(ma[0]);
        if(at){
          cd=(at[ma[1]])?at[ma[1]]:1;cm=at[ma[2]]-1;cy=at[ma[3]];
          dte=new Date(cy,cm,cd);
          if(dte.getFullYear()!=cy||dte.getDate()!=cd||dte.getMonth()!=cm){err=true};
        }else{err=true}
      } else if (t==4){ // time
        ma=a[i+1].split("#");at=v.match(ma[0]);if(!at){err=true}
      } else if (t==5){ // check this 2
            if(o1.length)o1=o1[a[i+1].replace(/(.*\[)|(\].*)/ig,"")];
            if(!o1.checked){err=true}
      } else if (t==6){ // the same
            if(v!=MM_findObj(a[i+1]).value){err=true}
      }
    } else
    if (!o.type&&o.length>0&&o[0].type=='radio'){
          at = a[i].match(/(.*)\[(\d+)\].*/i);
          o2=(o.length>1)?o[at[2]]:o;
      if (t==1&&o2&&o2.checked&&o1&&o1.value.length/1==0){err=true}
      if (t==2){
        oo=false;
        for(j=0;j<o.length;j++){oo=oo||o[j].checked}
        if(!oo){s+='* '+a[i+3]+'\n'}
      }
    } else if (o.type=='checkbox'){
      if((t==1&&o.checked==false)||(t==2&&o.checked&&o1&&o1.value.length/1==0)){err=true}
    } else if (o.type=='select-one'||o.type=='select-multiple'){
      if(t==1&&o.selectedIndex/1==0){err=true}
    }else if (o.type=='textarea'){
      if(v.length<a[i+1]){err=true}
    }
    if (err){s+='* '+a[i+3]+'\n'; err=false}
  }
  if (s!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+s)}
  document.MM_returnValue = (s=='');
}
function GP_AdvOpenWindow(theURL,winName,features,popWidth,popHeight,winAlign,ignorelink,alwaysOnTop,autoCloseTime,borderless) { //v2.0
  var leftPos=0,topPos=0,autoCloseTimeoutHandle, ontopIntervalHandle, w = 480, h = 340;  
  if (popWidth > 0) features += (features.length > 0 ? ',' : '') + 'width=' + popWidth;
  if (popHeight > 0) features += (features.length > 0 ? ',' : '') + 'height=' + popHeight;
  if (winAlign && winAlign != "" && popWidth > 0 && popHeight > 0) {
    if (document.all || document.layers || document.getElementById) {w = screen.availWidth; h = screen.availHeight;}
		if (winAlign.indexOf("center") != -1) {topPos = (h-popHeight)/2;leftPos = (w-popWidth)/2;}
		if (winAlign.indexOf("bottom") != -1) topPos = h-popHeight; if (winAlign.indexOf("right") != -1) leftPos = w-popWidth; 
		if (winAlign.indexOf("left") != -1) leftPos = 0; if (winAlign.indexOf("top") != -1) topPos = 0; 						
    features += (features.length > 0 ? ',' : '') + 'top=' + topPos+',left='+leftPos;}
  if (document.all && borderless && borderless != "" && features.indexOf("fullscreen") != -1) features+=",fullscreen=1";
  if (window["popupWindow"] == null) window["popupWindow"] = new Array();
  var wp = popupWindow.length;
  popupWindow[wp] = window.open(theURL,winName,features);
  if (popupWindow[wp].opener == null) popupWindow[wp].opener = self;  
  if (document.all || document.layers || document.getElementById) {
    if (borderless && borderless != "") {popupWindow[wp].resizeTo(popWidth,popHeight); popupWindow[wp].moveTo(leftPos, topPos);}
    if (alwaysOnTop && alwaysOnTop != "") {
    	ontopIntervalHandle = popupWindow[wp].setInterval("window.focus();", 50);
    	popupWindow[wp].document.body.onload = function() {window.setInterval("window.focus();", 50);}; }
    if (autoCloseTime && autoCloseTime > 0) {
    	popupWindow[wp].document.body.onbeforeunload = function() {
  			if (autoCloseTimeoutHandle) window.clearInterval(autoCloseTimeoutHandle);
    		window.onbeforeunload = null;	}  
   		autoCloseTimeoutHandle = window.setTimeout("popupWindow["+wp+"].close()", autoCloseTime * 1000); }
  	window.onbeforeunload = function() {for (var i=0;i<popupWindow.length;i++) popupWindow[i].close();}; }   
  document.MM_returnValue = (ignorelink && ignorelink != "") ? false : true;
}

function getTop(obj) {
	var cur = 0;
	if(obj.offsetParent) {		
		while(obj.offsetParent) {
			cur+=obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	return cur;
}

function getLeft(obj) {
	var cur = 0;
	if(obj.offsetParent) {		
		while(obj.offsetParent) {
			cur+=obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	return cur;
}

var $ = function ( id ){ return document.getElementById(id) }

/* Scroll */
function Scroll(content, up_id, down_id, height, knob_id, scrollbar_id)
{
	if ( typeof(content) != 'object' )
	{
		content = document.getElementById(content)	
	}
	
	if ( typeof(window.scroller) != 'undefined' )
	{
		window.scroller.content = content
		window.scroller.content.style.top = "0px"
		window.scroller.knob.style.top = window.scroller.knob.minY + "px"
	}
	else
	{
		this.content = content
		this.up = document.getElementById(up_id)
		this.down = document.getElementById(down_id)
		this.knob = document.getElementById(knob_id)
		this.scrollbar = document.getElementById(scrollbar_id)
		this.height = height
		window.scroller = this
		
		if ( !this.content || !this.up || !this.down)
		{
			//return false
		}

		if ( this.content.style.top == '' )
		{
			this.content.style.top = '0px'	
		}
		
		// Assign actions
		if (this.up.attachEvent) 
		{ // IE
			this.up.attachEvent("onmousedown", function(){window.scroller.scrollStart(2)});
			this.down.attachEvent("onmousedown", function(){window.scroller.scrollStart(-2)});
			
			this.up.attachEvent("onmouseup", function(){window.scroller.scrollStop()});
			this.down.attachEvent("onmouseup", function(){window.scroller.scrollStop()});
			this.up.attachEvent("onmouseout", function(){window.scroller.scrollStop()});
			this.down.attachEvent("onmouseout", function(){window.scroller.scrollStop()});

			if ( this.scrollbar )
			{
				this.scrollbar.attachEvent("onmousedown", function(e){window.scroller.scrollbarJumpStart(e)});
				this.scrollbar.attachEvent("onmouseup", function(){window.scroller.scrollStop()});
				this.scrollbar.attachEvent("onmouseout", function(){window.scroller.scrollStop()});
			}
			

		} 
		else
		{ // Gecko / W3C
			this.up.addEventListener("mousedown", function(){window.scroller.scrollStart(2)}, false);
			this.down.addEventListener("mousedown", function(){window.scroller.scrollStart(-2)}, false);
	
			this.up.addEventListener("mouseup", function(){window.scroller.scrollStop()}, false);
			this.down.addEventListener("mouseup", function(){window.scroller.scrollStop()}, false);
			this.up.addEventListener("mouseout", function(){window.scroller.scrollStop()}, false);
			this.down.addEventListener("mouseout", function(){window.scroller.scrollStop()}, false);
			
			if ( this.scrollbar )
			{
				this.scrollbar.addEventListener("mousedown", function(e){window.scroller.scrollbarJumpStart(e)}, false);
				this.scrollbar.addEventListener("mouseup", function(){window.scroller.scrollStop()}, false);
				this.scrollbar.addEventListener("mouseout", function(){window.scroller.scrollStop()}, false);
			}
		}
		
		if ( this.knob )
		{
			Drag.init(this.knob, null, 6, 6, -320, -80)
			this.knob.ychange = (Math.abs(this.knob.maxY) - Math.abs(this.knob.minY))
			this.knob.onDrag = function ( x, y )
				{
					if ( window.scroller.content.offsetHeight <= window.scroller.height )
					{
						return false;	
					}
					
					var ratio = (window.scroller.content.offsetHeight-window.scroller.height)/window.scroller.knob.ychange
					window.scroller.content.style.top = Math.round((y - window.scroller.knob.minY) * ratio) + "px"
				}
		}
	}
}

Scroll.prototype.reload = function (content_id, up_id, down_id, height, knob_id, scrollbar_id)
{
	delete this.content
	delete this.up 
	delete this.down 
	delete this.knob 
	delete this.scrollbar
	delete window.scroller
	
	this.content = document.getElementById(content_id)
	this.up = document.getElementById(up_id)
	this.down = document.getElementById(down_id)
	this.knob = document.getElementById(knob_id)
	this.scrollbar = document.getElementById(scrollbar_id)
	this.height = height
	this.toslide = this.content.offsetHeight - this.height
	window.scroller = this
	
	//alert(this.content.readyState)
}

Scroll.prototype.scrollStart = function ( step )
{
	window.scrollerInterval = setInterval(function(){window.scroller.scroll(step)},10)
}

Scroll.prototype.scroll = function ( step )
{
	this.toslide = this.content.offsetHeight - this.height
	
	if ( isUndefined(step) )
	{
		step = 2	
	}
	
	if ( this.content.offsetHeight <= this.height )
	{
		this.scrollStop();
		return false
	}
	
	var nt = parseInt(this.content.style.top)+step; // next position

	if ( parseInt(this.content.style.top) >= 0 && step > 0 )
	{
		this.scrollStop();
		this.knob.style.top = this.knob.minY + "px"
		return false;
	}

	if ( step < 0 && this.height + Math.abs(parseInt(this.content.style.top)) >= this.content.offsetHeight )
	{
		this.scrollStop();
		this.knob.style.top = this.knob.maxY + "px"
		return false;
	}
	
	if ( nt > 0 )
	{
		nt = 0	
	}
	else if ( Math.abs(nt) > this.toslide )
	{
		nt = 0-this.toslide
	}
	
	this.content.style.top = nt + "px";

	if ( this.knob )
	{
		var ratio = (Math.abs(parseInt(this.content.style.top)) / this.toslide)*100
		this.knob.style.top = this.knob.minY - (this.knob.ychange / (100/ratio)) + "px"
	}
}

Scroll.prototype.scrollStop = function ()
{
	clearInterval(window.scrollerInterval);
}


Scroll.prototype.scrollbarJumpStart = function (e)
{
	e = fixEvent(e)
	knobTopY = Math.abs(this.knob.minY) - Math.abs(parseInt( this.knob.style.top ))
	knobBotY = knobTopY + 30
	
	if( e.clickY > knobTopY && e.clickY > knobBotY )
	{
		this.scroll(-60)
	}
	else if ( e.clickY < knobTopY && e.clickY < knobBotY )
	{
		this.scroll(60)
	}
}

function scrollToTop( zero )
{
	if ( typeof(zero) == 'undefined' )
	{
		zero = 0	
	}
		
	window.scroller.content.style.top = zero+"px"
	window.scroller.knob.style.top = window.scroller.knob.minY + "px"
}

function fixEvent(e)
{
	if ( is_ie )
	{
		// IE
		e = window.event
		e.clickX = e.offsetX
		e.clickY = e.offsetY
	}
	else if ( is_firefox )
	{
		// Firefox
		e.clickX = e.pageX - getLeft(e.target)
		e.clickY = e.pageY - getTop(e.target)
	}
	
	return e;
}

Array.prototype.append = function (obj)
{
	for( i=0; i<this.length; i++ )
	{
		if ( isUndefined(this[i]) )
		{
			this[i]=obj
			break;
		}	
	}
}

Array.prototype.indexOf = function (obj)
{
	for( i=0; i<this.length; i++ )
	{
		if ( this[i] == obj )
		{
			return i
		}	
	}
	
	return -1;
}

function isUndefined(a) 
{
    return typeof a == 'undefined';
} 

function processTextForm(FormName)
{
	xajax_processTextForm(xajax.getFormValues(FormName), FormName);
}

function resetScrollFromIframe()
{
	if ( window.parent.Scroll )
	{
		new window.parent.Scroll(document.getElementById('content'))
	}
}