﻿/************************************************************************
*	Module:         /js/js.js											
*	Description:    common JScript functions								
*	Author:         Plamen Sharapanov									
*	Company:        DAIS Software										
*	Date:           01.06.2001											
*************************************************************************
*	Revision History:
*	Name:           Date:       Description:
*	CASCADE			09.07.2001	added cascadeding window manager
*	VALIDATER		30.08.2001	added Form Validation manager
*
************************************************************************/

var isDOM = (document.getElementById ? true : false); 
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
var isDyn = (isDOM || isIE4 || isNS4);

function getRef(id)
{
 if (isDOM) return document.getElementById(id);
 if (isIE4) return document.all[id];
 if (isNS4) return document.layers[id];
}

function getSty(id)
{
 return (isNS4 ? getRef(id) : getRef(id).style);
}

function getRefEx(wnd, id)
{
 if (isDOM) return wnd.document.getElementById(id);
 if (isIE4) return wnd.document.all[id];
 if (isNS4) return wnd.document.layers[id];
}
//=======================
function getX(who)
{
	var off = 0;
	var tmpWho = who;
	//IE, opera 5+
	if (tmpWho.offsetParent!=null && tmpWho.offsetLeft !=null) while(tmpWho.offsetParent) {tmpWho = tmpWho.offsetParent; off += tmpWho.offsetLeft;}
	//must be Opera4 and NS
	if (who.x) return who.x;
	return off;
}
//=======================
function getY(who)
{
	var off = who.offsetTop;
	var tmpWho = who;
	//IE, Opera 5+
	if (tmpWho.offsetParent!=null && tmpWho.offsetTop!=null) while(tmpWho.offsetParent) {tmpWho = tmpWho.offsetParent; off+=tmpWho.offsetTop;}
	//must be Opera and NS
	if (who.y) return who.y;
	
	return off;
}

// function for edit boxes
function txtNormal(obj) {
  obj.className="edit1";
}
function txtFocus(obj) {
  obj.className="edit2";
}

// functions for buttons
function btnNormal(obj)
{
	window.status = "";
  obj.className="button1";
}
function btnOnFocus(obj)
{
	window.status = obj.value;
  obj.className="button2";
}

// functions for radio buttos
function radioNormal(obj) {
  obj.className="radio1";
}
function radioOnFocus(obj) {
  obj.className="radio2";
}



function OpenNewWindow( wndOpener, bReturnWnd, strUrl)
{//cascad
	var winW=800;
	winW=(! window.screen ) ? winW : ( window.screen.width ) ? window.screen.width : winW;
	var winH=600;
	winH=(! window.screen ) ? winH : ( window.screen.height ) ? window.screen.height : winH;
	winH -=100;//taskbar :)
	
	var w = 640
	var h = 480

	var Pos = new CWndPos;
	Pos.Serialize(false);

	var x = Pos.m_iX;
	var y = Pos.m_iY;
	
	if( (x+w>winW) || (y+h>winH) )
	{
		x=y=0;
		Pos.moveTo(0,0);
	}
	else
	{
		Pos.moveBy(20,20);
	}
	
	Pos.Serialize(true);
	
	if(strUrl)
		strUrl = strUrl.replace(/\s/g, "+");
		
	var wnd = wndOpener.open( strUrl , "_blank", "scrollbars=1,toolbar=0,location=0,directories=0,status=1,menubar=0,resizable=1,top="+y+",left="+x+",width="+w+",height="+h);

	if(bReturnWnd)
		return wnd;
	return false;
}



function OpenHelpWindow( strUrl)
{
	var wnd = top.open( strUrl , "TargetHelpWindow", "scrollbars=1,toolbar=0,location=0,directories=0,status=1,menubar=0,resizable=1,width=400,height=400");
	wnd.focus();
	return false;
}



function ReturnToOpener( wnd, bReload )
{
	if(wnd)
	{
		if( wnd.opener && (! wnd.opener.closed ) )
		{
			if(bReload)
			{
				wnd.opener.location.reload(true);
			}
			wnd.opener.focus();
		}	
		wnd.close();
	}
	return false;
}



function CCookies()
{
	this.saveCookie = function(name,value,days)
	{
		if (days) {
			var date=new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires="; expires="+date.toGMTString();
		} 
		else expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	};
	
	this.readCookie = function(name)
	{
		var nameEQ=name+"=";
		var ca=document.cookie.split(';');
		for(var i=0;i<ca.length;i++) {
			var c=ca[i];
			while (c.charAt(0)==' ') c=c.substring(1,c.length);
			if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	};
	
	this.deleteCookie = function(name)
	{
		this.saveCookie(name,"",-1);
	}
};//CCookies



function CWndPos( iX, iY )
{
	this.m_iX = parseInt( iX?iX:0 );
	this.m_iY = parseInt( iY?iY:0 );
	
	this.Serialize = function( bSave )
	{
		var Store = new CCookies();
		if(bSave)
		{//save position
			Store.saveCookie("WndPos", this.toString() );
		}
		else
		{//load position
			this.fromString( Store.readCookie("WndPos") );
		};//if
	};//Serialize

	this.toString = function()
	{
		return this.m_iX + "," + this.m_iY;
	};//toString

	this.fromString = function(str)
	{
		str = ( typeof(str) == "string" ) ? str : "0,0";
		var ca=str.split(',');
		this.m_iX = parseInt( (ca.length > 0)? ca[0] : 0 );
		this.m_iY = parseInt( (ca.length > 1)? ca[1] : 0 );
	};//toString

	this.moveBy = function(iDX, iDY)
	{
		this.m_iX = parseInt(this.m_iX) + parseInt(iDX);
		this.m_iY = parseInt(this.m_iY) + parseInt(iDY);
	}	

	this.moveTo = function(iX, iY)
	{
		this.m_iX = parseInt(iX);
		this.m_iY = parseInt(iY);
	}	
};//CWndPos

function DisableSomeHeaders(This,strGenType)
{
	switch(strGenType)
	{
		case "current":
			{
				EnableDateOnClient(This,"at", false);
				EnableDateOnClient(This,"from", false);
				EnableDateOnClient(This,"to", false);
				break;
			}
		case "date":
			{
				EnableDateOnClient(This,"at", true);
				EnableDateOnClient(This,"from", false);
				EnableDateOnClient(This,"to", false);
				break;
			}
		case "period":
			{
				EnableDateOnClient(This,"at", false);
				EnableDateOnClient(This,"from", true);
				EnableDateOnClient(This,"to", true);
				break;
			}
	}
}


function ShowDateOnClient(This,strLng)
{
	var arrWeekDays = new Array();
	arrWeekDays[0] = strLng="bg-BG"?"Понеделник":"Monday";
	arrWeekDays[1] = strLng="bg-BG"?"Вторник":"Tuesday";
	arrWeekDays[2] = strLng="bg-BG"?"Сряда":"Wednesday";
	arrWeekDays[3] = strLng="bg-BG"?"Четвъртък":"Thursday";
	arrWeekDays[4] = strLng="bg-BG"?"Петък":"Friday";
	arrWeekDays[5] = strLng="bg-BG"?"Събота":"Saturday";
	arrWeekDays[6] = strLng="bg-BG"?"Неделя":"Sunday";
	
	var strName = This.id.substr(This.id.indexOf("_")+1);
	eval("var lbD = This.form.day_"+strName )
	var D = lbD.options[lbD.selectedIndex].value;
	eval("var lbM = This.form.month_"+strName )
	M = lbM.options[lbM.selectedIndex].value;
	eval("var Y = This.form.year_"+strName+".value" )

	var TestDate = new Date(Y,M-1,D);
	

	eval("var edWD = This.form.wd_"+strName )
	edWD.value = arrWeekDays[ TestDate.getUTCDay() ];
	//edWD.value = TestDate.getUTCDay();
}


function EnableDateOnClient(This,strName,bEnable)
{
	var strStatus = String(!bEnable);
	eval("This.form.day_"+strName+".disabled="+strStatus );
	eval("This.form.month_"+strName+".disabled="+strStatus );
	eval("This.form.year_"+strName+".disabled="+strStatus );
}


function MySubmit(MyForm)
{
	var wnd = OpenNewWindow( window, true, "about:blank");
	wnd.name = "dais_" + (new Date()).toUTCString() +"_"+Math.random( );
	MyForm.target = wnd.name;
	MyForm.submit();
	return false;
};//MySubmit




function StringValidater(str, strRegExpr)
{
	eval("var re = " + strRegExpr)
	var bRes = re.test(str);
	if(bRes)
	{//so strRegExpr a substring of the str but is it the whole string
		var strRes = str.replace(re, "");
		bRes = (strRes == "")
	}
	return bRes;
};//StringValidater


function FormValidater(MyForm)
{
	var arrInputs = MyForm.__all.value.split(" ;");
	for(var i=1; i < arrInputs.length; i++)
	{
		eval("var obj = MyForm." + arrInputs[i] );
		eval("var objRegExpr = MyForm._" + arrInputs[i] );
		if(! StringValidater(obj.value, objRegExpr.value) )
		{
			alert("Not correct format !");
			obj.focus();
			return false;
		};//if
	};//for i
	
	return true;
};//FormValidater

function ChangeLang()
{
	var cb = document.formLang.cbLang;
	var strNewLangID = cb.options[cb.selectedIndex].value;
	var strURL = self.location.href;
	var sPos = strURL.indexOf("lang=");
	if (sPos == -1)
	{//there is no language
		if(self.location.search=="")
		{//there is no request
			strURL += "?lang=" + strNewLangID;
		}
		else
		{//add to existing request
			strURL += "&lang=" + strNewLangID;
		}
	}
	else
	{//there is a language
		sPos = sPos + 4/* "lang" */ + 1/* "=" */;
		var ePos = strURL.indexOf("&", sPos);
		if (ePos == -1)
		{//it is last argument
			strURL = strURL.substring(0,sPos) + strNewLangID
		}
		else
		{// there are more args in right
			strURL = strURL.substr(0,sPos) + strNewLangID + strURL.substr(ePos);
		};//if (sPos == -1)
	};//if (sPos == -1)
	window.open(strURL, "_self");
	return false;
};//ChangeLang
function myMail(em, Dom){ window.location.href = "m"+"a"+"il"+"t"+"o:"+em+"@"+Dom;}