/*
* Written by ArS 2001 
*/

/** reads value of param in URl 
* returns null in not found
*/
var isMac    = (navigator.appVersion.indexOf("Mac")!=-1);
var isLinux  = (navigator.appVersion.indexOf("inux")!=-1);

function GetSearchParam(src, name)
{
	var re = new RegExp("(\\?|&)"+name+"=([^&]*)");
	if (src.match(re))
		return RegExp.$2;
	return null;
}

/**
* replaces value of param in URl 
* or appends param if not exists
*/

function SetSearchParam(src,param_name,param_value)
{
	tsrc = src.toString();
	if (!tsrc.match(/\?/))
		return tsrc+"?"+param_name + "=" + escape(param_value);
	var re = new RegExp("(\\?|&)"+param_name+"=([^&]+)");
	//alert(re.toString()+"\n"+tsrc+"\n"+param_name+"\n"+param_value);
	if (tsrc.match(re))
		{ return tsrc.replace(re,"$1"+param_name + "=" + escape(param_value)); }	
	return tsrc +"&"+param_name + "=" + escape(param_value);
}

function RemoveSearchParam(src,param_name)
{
	var tsrc = src.toString();
	if (!tsrc.match(/\?/)) return tsrc;
	var re1 = new RegExp("\\?"+param_name+"=[^&]+","g");
	var re2 = new RegExp("&"+param_name+"=[^&]+","g");
	tsrc = tsrc.replace(re1,"?");
	tsrc = tsrc.replace(re2,"");
	return tsrc;
}

////////////
//
//
//
//
////////////

function PrepareHTMLDocumentMessage(title,body)
{
	text =  "<html><head><title>"+title+" </title><body bgcolor=\"#4c4d4c\"  leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">";
	text += "<table cellspacing=\"0\" cellspadding=\"0\" border=\"0\" width=\"300\" height=\"100\" >";
	text += "<tr><td align=\"center\" valign=\"middle\"><FONT size\"+4\" COLOR=\"#FFFFFF\">";
	text += body;
	text += "</FONT></td></tr></table></body></html>";
	//alert(text);
	return text;
}

function popupTimerWin(str,time) {
if (!time)
	time = 5000;
//windowProp(text,time);
setTimeout('windowTimerProp(text,'+time+')', 30); 		// delay 3 seconds before opening
}

function windowTimerProp(text,time) 
{
newTimerWindow = window.open('','popupWin','width=300,height=100');
newTimerWindow.document.write(text);
setTimeout('closeWin(newWindow)', time);// delay 5 seconds before closing
}

function closeTimerWin(newWindow) 
{
newTimerWindow.close();// close small window and depart
}
//// ------------------ ////

//alert(navigator.appVersion);
//var version4 = document.layers?1:0;//(navigator.appVersion.charAt(0) == "4"); 
var popupHandle;

function closePopup() {
if(popupHandle != null && !popupHandle.closed) popupHandle.close();
}

/*
*  position=1 POPUP: makes screen display up and/or left, down and/or right 
*  depending on where cursor falls and size of window to open
*  position=2 CENTER: makes screen fall in center
*/
function displayPopup(position,text,name,height,width,evnt) {
var properties = "toolbar=0,location=0,height=" + height;
properties = properties + ",width=" + width;
var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
if(navigator.appName == "Microsoft Internet Explorer") {
screenY = document.body.offsetHeight;
screenX = window.screen.availWidth;
}
else {
screenY = window.outerHeight
screenX = window.outerWidth
}
if(position == 1)	{ // if POPUP not CENTER
cursorX = evnt.screenX;
cursorY = evnt.screenY;
padAmtX = 10;
padAmtY = 10;
if((cursorY + height + padAmtY) > screenY) {
// make sizes a negative number to move left/up
padAmtY = (-30) + (height * -1);
// if up or to left, make 30 as padding amount
}
if((cursorX + width + padAmtX) > screenX)	{
padAmtX = (-30) + (width * -1);	
// if up or to left, make 30 as padding amount
}
if(navigator.appName == "Microsoft Internet Explorer") {
leftprop = cursorX + padAmtX;
topprop = cursorY + padAmtY;
}
else {
leftprop = (cursorX - pageXOffset + padAmtX);
topprop = (cursorY - pageYOffset + padAmtY);
   }
}
else{
leftvar = (screenX - width) / 2;
rightvar = (screenY - height) / 2;
if(navigator.appName == "Microsoft Internet Explorer") {
leftprop = leftvar;
topprop = rightvar;
}
else {
leftprop = (leftvar - pageXOffset);
topprop = (rightvar - pageYOffset);
   }
}
if(evnt != null) {
properties = properties + ",left=" + leftprop;
properties = properties + ",top=" + topprop;
}
closePopup();
popupHandle = open("",name,properties);
popupHandle.document.write(text);
setTimeout('closePopup()', 5000);
}



  
/////////////
///
////////////
/*
ONLOAD="HideWaitMessage()"

<div ID="wait_div" STYLE="position:absolute;left:0px;top:0px;width:100%;height:100%" >
<table WIDTH="100%" HEIGHT="100%"><tr>
<td BGCOLOR="Black" ALIGN="CENTER">
	<h3><font COLOR="#FFFFFF"> please wait ... </font></h3>
</td></tr></table></div>
*/

function HideWaitMessage()
{
	//setTimeout("", 200);
	if (document.layers && document.layers.wait_div)
		document.layers.wait_div.visibility="hide";
	else if (document.all && document.all.wait_div)
		{
			document.all.wait_div.style.width ="1px";
			document.all.wait_div.style.height ="1px";
	
			document.all.wait_div.style.visibility = 'hidden';
		}
	else if (document.getElementById && document.getElementById("wait_div"))
		document.getElementById("wait_div").style.visibility = 'hidden';

		
}

//*************

function GetWndSizeStr(wnd)
{
	var vs=0, hs=0;
	if (document.all)
	{
	   vs = wnd.document.body.offsetHeight;//parent.frCenter.document.body.clientHeight;
	   hs = wnd.document.body.offsetWidth; //parent.frCenter.document.body.clientWidth;
	}
	else //if (document.layers)		   //netscape
	{
	   vs = wnd.innerHeight;//parent.frCenter.innerHeight;
	   hs = wnd.innerWidth;//parent.frCenter.innerWidth;
	}
	//alert("vs=" + vs + "&hs="+ hs);
	return "vs=" + vs + "&hs="+ hs

}

function GetVS(wnd)
{
	if (document.all)
	   return wnd.document.body.offsetHeight;	
	else 
	   return wnd.innerHeight;
}

function GetHS(wnd)
{
	if (document.all)
	   return wnd.document.body.offsetWidth;	
	else 
	   return wnd.innerWidth;
}

// w-inner width 
// h-inner height
// window will be centered ;

function OpenCenteredWindow(url,name,w,h,scrollable)
{
	var w,h;
 	var s = scrollable?1:0;
	x=(screen.availWidth-w)/2;
	y=(screen.availHeight-h)/2;
    var par;
    if (document.layers)
	   par =  'scrollbars='+s+',resizable=0,left='+x+',top='+y+',screenX=0,screenY=0,innerWidth=' + w + ',innerHeight=' + h;
	else
       par =  'directories=0,location=0,menubar=0,scrollbars='+s+',status=0,toolbar=0,resizable=1,left='+x+',top='+y+',screenX=0,screenY=0,width='+w+',height='+h+',innerWidth='+w+',innerHeight='+h;
	return window.open(url, name, par);

	//showModalDialog(URL [, arguments [, features]])

}

function ResizeWindow(rx,ry,loc)
{
//	reloadPage(true);
	if (!loc) loc= window.location.href.toString();
	var dx=0,dy=0;
     if (window.innerWidth) // ns 6	,4
	{
		dx = rx - window.innerWidth;
		dy = ry - window.innerHeight;
		if (dx!=0 || dy!=0)
        {   
			if (document.layers)
				window.setResizable(true);
			window.resizeBy(dx,dy); 

            if (document.layers)//ns 4
            {
                window.location = loc;
		return false;
            }
        }
		else if (document.layers)
			window.setResizable(false);//lock window size
	}
    else if (document.all) //explorer
	{
    	dx = rx - document.body.clientWidth;//offsetWidth;
		dy = ry - document.body.clientHeight;//offsetHeight;
		window.resizeBy(dx,dy);//lock window size
	}
	return true;
}

function ResizeWindowAndGo(rx,ry,loc)
{
	ResizeWindow(rx,ry,loc)
	if (!document.layers) window.location = loc;
}



function SubmitFormIntoWindowArr(win,action,target,arr)
{
	if (!CreateNewLayer)
	{
		
		alert("it is necessary to load layers.js before use SubmitFormIntoWindow()");
		return;
	}
	var lay ;
	//win.auto_submit_layer 
	//lay = GetLayer("auto_submit_layer",win.document);

	if (!win.auto_submit_lay || document.layers)
	{
		lay = CreateNewLayer(-10,-10,10,10,1,0,win.document);
		win.auto_submit_lay = lay;	
	}
	else
	{  
		lay = win.auto_submit_lay;
	}
	
	if (!lay)
	{
		alert('Can not create layer!');
		return;
	}

	var str="<form enctype='multipart/form-data' name='auto_submit' method='POST' "+ (target!=""?"target='" + target+"'":"") + " action='" + action + "'>";
//	alert( arr.length);
	for (var i = 0 ;i< arr.length; i+=2)
	{
	   //alert(i+" "+ arr[i] + " "+arr[i+1]);	
	   if (typeof (arr[i+1]) != 'number' && typeof (arr[i+1]) != 'string' )
	       for (var j=0;j<arr[i+1].length;j++)
		    	str += " <input type='hidden' name='"+arr[i]+"["+j+"]' >";
	   else
	   	   str+=" <input type='hidden' name='"+arr[i]+"' >";

	}
	str +="</form>";
	//alert(str);
	FillLayer(lay,str);
	
	var form = FindObj("auto_submit",win.document);
		
	if (document.layers) form = FindObj("auto_submit",lay.document);
	
	if (!form)
	{
		alert("Can't find auto_submit form");
		return;
	}
	for (var i = 0 ;i< arr.length; i+=2)
	{
	   //alert(i);	
	   if (typeof (arr[i+1]) != 'number' && typeof (arr[i+1]) != 'string')
	       for (var j=0;j<arr[i+1].length;j++)
		    	form[arr[i]+"["+j+"]"].value=arr[i+1][j];
	   else
    		{
				form[arr[i]].value= arr[i+1];
				//alert(form[arr[i]].value);
			}
	}
	//alert (form);
	//form.action = action;
	//var a; for (a in form)  alert(a);
	//win.setTimeout("document.forms.auto_submit.submit()",5000);
	form.submit();
	
}

function SubmitFormIntoNewWindowArr(action,name,params,arr)
{
	
	window._new_form_data = arr;
	window._new_form_action = action;
	var win = window.open("form_include.html",name,params);
	//SubmitFormIntoWindowArr(win,action,name,arr);
	return win;
}

function SubmitFormIntoNewWindow(action,name,params)
{
	var arr = new Array();
	for (var i = 3 ;i< arguments.length; i++)
		arr[i-3]=arguments[i];
	window._new_form_data = arr;
	window._new_form_action = action;
	var win = window.open("form_include.html",name,params);
	//SubmitFormIntoWindowArr(win,action,name,arr);
	return win;
}

function SubmitFormIntoWindow(win,action,target)
{
	var arr = new Array();

	for (var i = 3 ;i< arguments.length; i++)
		arr[i-3]=arguments[i];
	
	SubmitFormIntoWindowArr(win,action,target,arr);
}



