var ua = navigator.userAgent;

var isDOM=document.getElementById?1:0; // DOM1 browser
var isOpera=/Opera/.test(ua);
var isMSIE=/MSIE/.test(ua);
var isGecko=/Gecko/.test(ua);
var isSafari=/Safari/.test(ua);

var isMSIE7=0;

if(isMSIE)
{
var re  = new RegExp("MSIE ([0-9]{1,})");
	if (re.exec(ua) != null)
	{
	var rv = parseInt( RegExp.$1 );
	if(rv==7) isMSIE7=1;
	}
}

var controllerI=0;
var controllerInterval1=null;
var controllerInterval2=null;
var controllerTimeOut=null;
var controllerWidget;

var widget=null;

var objectDragApproved=0;
var objectStartX;
var objectStartY;
var objectDragID;
var objectID="";


function $(id)
{
return document.getElementById(id); 
}


function controllerCheckDigitsType(event)
{
return controllerCheckValueType(event, /[0-9]+/);
}


function controllerCheckDigits(widget)
{
return controllerCheckValue(widget, /[0-9]+/);
}


function controllerCheckDigitsNegativePercentType(event)
{
return controllerCheckValueType(event, /[\-0-9%]+/);
}


function controllerCheckDigitsNegativePercent(widget)
{
return controllerCheckValue(widget, /\-?[0-9]+%?$/);
}


function controllerCheckFloatType(event)
{
return controllerCheckValueType(event, /[0-9.]+/);
}


function controllerCheckFloat(widget)
{
return controllerCheckValue(widget, /[0-9.]+/);
}


function controllerCheckFloatNegativeType(event)
{
return controllerCheckValueType(event, /[\-0-9.]+/);
}


function controllerCheckFloatNegative(widget)
{
return controllerCheckValue(widget, /[\-0-9.]+/);
}


function controllerCheckEmail(widget)
{
return controllerCheckValue(widget, /^\w+(\.\w|\w|\-\w)*@([a-z0-9_-]+\.)+([a-z]{2,4})$/i);
}


function controllerCheckEmailType(event)
{
return controllerCheckValueType(event, /[a-z0-9_.@\-]+/i);
}


function controllerCheckWebsite(widget)
{
return controllerCheckValue(widget, /^([a-z0-9_\-:\/]+\.)+([a-z]{2,4})(\/)/i);
}


function controllerCheckWebsiteType(event)
{
return controllerCheckValueType(event, /[a-z0-9_\.\-\:\/]+/i);
}


function controllerCheckIPType(event)
{
return controllerCheckValueType(event, /[0-9\.]+/);
}


function controllerCheckIP(widget)
{
return controllerCheckValue(widget, /(([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]{1,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])/);
}


function controllerCheckLatinOnly(widget)
{
var result;
result=controllerCheckValue(widget, /[a-z]+/i);
return result;
}


function controllerCheckLatinOnlyType(event)
{
return controllerCheckValueType(event, /[a-z]+/i);
}


function controllerCheckLatinDigits(widget)
{
var result;
result=controllerCheckValue(widget, /[a-z0-9_]+/);
return result;
}


function controllerCheckLatinDigitsType(event)
{
return controllerCheckValueType(event, /[a-z0-9_]+/);
}


function controllerCheckLatinDigitsSpace(widget)
{
var result;
result=controllerCheckValue(widget, /[a-z0-9_ \*]+/);
return result;
}


function controllerCheckLatinDigitsSpaceType(event)
{
return controllerCheckValueType(event, /[a-z0-9_ \*]+/);
}


function controllerCheckLatin(widget)
{
var result;
result=controllerCheckValue(widget, /[\x20-\x7F]+/i);
return result;
}


function controllerCheckLatinType(event)
{
return controllerCheckValueType(event, /[\x20-\x7F]+/i);
}


function controllerCheckLatinSpecial(widget)
{
var result;
result=controllerCheckValue(widget, /[\x00-\x7F]+/i);
return result;
}


function controllerCheckLatinSpecialType(event)
{
return controllerCheckValueType(event, /[\x00-\x7F]+/i);
}


function controllerCheckPassword(widget)
{
var result;
result=controllerCheckValue(widget, /[a-z0-9_]+/i);
return result;
}


function controllerCheckPasswordType(event)
{
return controllerCheckValueType(event, /[a-z0-9_]+/i);
}


function controllerCheckFill(widget)
{
var result;
result=controllerCheckValue(widget, /.+/);
return result;
}


function controllerCheckFillType(event)
{
return controllerCheckValueType(event, /.+/);
}


function controllerCheckEnterType(event)
{
var charCode;
	if(isMSIE)
	{
	charCode=window.event.keyCode;
	}
	else
	{
	charCode=event.which;
	}
var frm;
	if(charCode==13)
	{
	var widget;
	if(isMSIE)
	{
	widget=window.event.srcElement;
	}
	else
	{
	widget=event.target;
	}
	frm=widget.form;
		for(var i=0; i<frm.elements.length; i++)
		{
			if(frm.elements[i].type=="submit")
			{
			frm.elements[i].click();
			}
		}
	}
}


function controllerCheckSelected(widget)
{
	if(widget.selectedIndex==-1 || widget.options[widget.selectedIndex].text=="" || widget.options[widget.selectedIndex].value==0)
	{
	return false;
	}
return true;
}


function controllerCheckValue(widget, re)
{
var val=widget.value;
var re = new RegExp(re, "i");
	if(val.search(re)==-1)
	{
	return false;
	}
return true;
}


function controllerCheckValueType(event, re)
{
var charCode;
	if(isMSIE)
	{
	charCode=window.event.keyCode;
	}
	else
	{
	charCode=event.which;
	}
if(charCode==0 || charCode==8 || charCode==9 || charCode==13) return true;
var str=String.fromCharCode(charCode);
if(str.search(re)==-1) return false;
controllerSetColorNormalBackground(event);
return true;
}


function controllerCheckIdentity(widget1, widget2)
{
var str1=widget1.value;
var str2=widget2.value;
	if(str1!=str2)
	{
	controllerSetAlert(widget2, alertCheckWidget);
	return false;
	}
return true;
}


function controllerAutofocusForm(obj)
{
	if(obj.childNodes.length)
	{
		for(var child = obj.firstChild; child; child = child.nextSibling)
		{
			if(child.type)
			{
				switch(child.type)
				{
				case "text":
				case "textarea":
				case "password":
				case "select-one":
					if(child.getAttribute("autofocus")=="autofocus")
					{
					controllerSetFocus(child);
					}
				break;
				}
			}
		controllerAutofocusForm(child);
		}
	}
}


function controllerEnableWidget(widget)
{
	switch(widget.type)
	{
	case "checkbox":
	case "password":
	case "select-multiple":
	case "select-one":
	case "text":
	case "textarea":
	widget.disabled=false;
	break;

	default:
	break;
	}
}


function controllerDisableWidget(widget)
{
	switch(widget.type)
	{
	case "checkbox":
	widget.checked=false;
	widget.disabled=true;
	break;

	case "password":
	case "text":
	case "textarea":
	widget.value="";
	widget.disabled=true;
	break;

	case "select-multiple":
	controllerDeselectOptions(widget);
	widget.disabled=true;
	break;

	case "select-one":
	widget.selectedIndex=0;
	widget.disabled=true;
	break;

	default:
	break;
	}
}


function controllerCheckForm(obj)
{
	if(obj.childNodes.length)
	{
		for(var child = obj.firstChild; child; child = child.nextSibling)
		{
			if(child.disabled==false)
			{
				if(child.type)
				{
					switch(child.type)
					{
					case "text":
					case "textarea":
					case "hidden":
					case "password":
						if(child.getAttribute("required")=="required")
						{
							if(child.value=="")
							{
							controllerSetAlert(child, alertCheckWidget);
							return false;
							}
						}
						if(child.getAttribute("pattern")) 
						{
							if(!controllerCheckValue(child, child.getAttribute("pattern")))
							{
							controllerSetAlert(child, alertCheckWidget);
							return false; 
							}
						}
					break;

					case "select-one":
						if(child.getAttribute("required")=="required")
						{
							if(!controllerCheckSelected(child))
							{
							controllerSetAlert(child, alertCheckWidget);
							return false;
							}
						}
					break;

					default:
					break;
					}
				}
			}
			if(!controllerCheckForm(child))
			{
			return false;
			}
		}
	}
return true;
}

// not necessary whole form, could be one fieldset (section)
function controllerDisableForm(obj)
{
	if(obj.childNodes.length)
	{
		for(var child = obj.firstChild; child; child = child.nextSibling)
		{
		controllerDisableWidget(child);
		controllerDisableForm(child);
		}
	}
}


// not necessary whole form, could be one fieldset (section)
function controllerEnableForm(obj)
{
	if(obj.childNodes.length)
	{
		for(var child = obj.firstChild; child; child = child.nextSibling)
		{
		controllerEnableWidget(child);
		controllerEnableForm(child);
		}
	}
}


function controllerEmptyForm(obj)
{
	if(obj.childNodes.length)
	{
		for(var child = obj.firstChild; child; child = child.nextSibling)
		{
			if(child.type)
			{
				switch(child.type)
				{
				case "text":
				case "textarea":
				case "hidden":
				case "password":
				child.value="";
				break;

				case "select-one":
				child.selectedIndex=0;
				break;

				case "select-multiple":
					if(child)
					{
					controllerDeselectOptions(child);
					}
				break;

				case "checkbox":
				child.checked=false;
				break;

				default:
				break;
				}
			}
		controllerEmptyForm(child);
		}
	}
}

/*
function controllerFixPNG(obj)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
	var src;
		if(obj.childNodes.length)
		{
			for(var elem = obj.firstChild; elem; elem = elem.nextSibling)
			{
			src=null;
				if (elem.tagName=='IMG')
				{
					if (/\.png$/.test(elem.src))
					{
					src = elem.src;
					elem.src = "/images/ig_blank.gif";
					}
				}
				else
				{
					if(elem.currentStyle && elem.currentStyle.backgroundImage)
					{
					src = elem.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
						if (src)
						{
						src = src[1];
						elem.runtimeStyle.backgroundImage="none";
						}
					}
				}
			if (src) elem.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
			controllerFixPNG(elem);
			}
		}
	}
}
*/


function controllerFixPNG(elem)
{
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent))
	{
	var src;
		if(elem)
		{
			if (elem.tagName=='IMG')
			{
				if (/\.png$/.test(elem.src))
				{
				src = elem.src;
				elem.src = "/images/ig_blank.gif";
				}
			}
			else
			{
			src = elem.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
				if (src)
				{
				src = src[1];
				elem.runtimeStyle.backgroundImage="none";
				}
			}
		if (src) elem.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		}
  	}
}


function controllerGetWidgetType(widget)
{
var type="";
type=widget.type;
return type;
}


function controllerAddOption(widget, optionValue, optionText)
{
var newElem=document.createElement("OPTION");
newElem.value=optionValue;
newElem.text=optionText;
widget.options.add(newElem, widget.options.length);
}


function controllerAddOptions(widget, optionsStr)
{
	if(isMSIE)
	{
	var selectStartStr=widget.outerHTML.replace('</SELECT>', '');
	var selectStr=selectStartStr+optionsStr+'</SELECT>';
	widget.outerHTML=selectStr;
	}
	else
	{
	widget.innerHTML=optionsStr;
	}
}


function controllerRemoveOption(widget, optionValue)
{
	for(var i=0; i<widget.options.length; i++)
	{
		if(widget.options[i].value==optionValue)
		{
		widget.remove(i);
			if(i<widget.options.length)
			{
			widget.selectedIndex=i;
			}
		break;
		}
	}
}


function controllerRemoveOptions(widget)
{
widget.innerHTML="";
}


function controllerSelectOption(widget, optionValue)
{
var i;
	for(i=0; i<widget.options.length; i++)
	{
		if(widget.options[i].value==optionValue)
		{
		widget.options[i].selected=true;
		break;
		}
	}
}


function controllerSelectOptions(widget)
{
var i;
	for(i=0; i<widget.options.length; i++)
	{
	widget.options[i].selected=true;
	}
}


function controllerDeselectOptions(widget)
{
var i;
	for(i=0; i<widget.options.length; i++)
	{
	widget.options[i].selected=false;
	}
}


function controllerSetAlert(widget, alertMessage)
{
alert(alertMessage);
controllerAlertWidget(widget);
}


function controllerSetColorNormalBackground(event)
{
var widget;
	if(isMSIE)
	{
	widget=window.event.srcElement;
	}
	else
	{
	widget=event.target;
	}
}


function controllerShowHideSelects(vis)
{
var type;
var widget;
	if(isMSIE && !isMSIE7)
	{
		for(i=0; i<document.forms.length; i++)
		{
			for(j=0; j<document.forms[i].elements.length; j++)
			{
				if(document.forms[i].elements[j].id>"")
				{
				widget=document.forms[i].elements[j].id;
				type=controllerGetWidgetType(widget);
					if(type=="select-one" || type=="select-multiple")
					{
					eval('document.all.' + widget + '.style.visibility="' + vis + '";');
					}
				}
			}
		}
	}
}


function controllerSetEmail(box, srv, subject, cls, text)
{
var email=box +  '@' + srv;
var str='<a href="mailto:' + email;
if(subject) str+='?subject=' + subject;
str+='"';
	if(cls)
	{
	str+=' class="' + cls + '"';
	}
str+='>';
	if(text>"")
	{
	str+=text;
	}
	else
	{
	str+=email;
	}
str+='</a>';
document.write(str);
}


function controllerSetFocus(widget)
{
var type;
var range;
type=controllerGetWidgetType(widget);
	switch(type)
	{
	case "text":
		if(isMSIE || isOpera)
		{
		range=widget.createTextRange();
		range.moveEnd("textedit");
		range.collapse(false);
		range.select();
		}
	break;
	}
widget.focus();
}


function controllerHideWidget()
{
controllerWidget.style.visibility='hidden';
}


function controllerShowWidget()
{
controllerWidget.style.visibility='visible';
	if(controllerI>1)
	{
	if(controllerInterval1) clearInterval(controllerInterval1);
	if(controllerInterval2) clearInterval(controllerInterval2);
	controllerI=0;
	controllerSetFocus(controllerWidget);
	}
	else
	{
	controllerI++;
	}
}


function controllerPhase()
{
	if(controllerTimeOut)
	{
	clearTimeout(controllerTimeOut);
	}
controllerHideWidget();
controllerInterval2=setInterval(controllerHideWidget, 500);
}


function controllerAlertWidget(widget)
{
controllerWidget=widget;
controllerSetFocus(widget);
controllerInterval1=setInterval(controllerShowWidget, 500);
controllerTimeOut=setTimeout(controllerPhase, 250);
}


function controllerWindow(winID, winName, winURL, winWidth, winHeight, winParams)
{
var winLeft=(screen.width-winWidth)*.5;
var winTop=(screen.height-winHeight)*.5;
var winStr='width=' + winWidth + ', height=' + winHeight + ', left=' + winLeft + ', top=' + winTop;
	if(winParams>"")
	{
	winStr+=", "+winParams;
	}
	if(!winID || winID.closed)
	{
	winID=window.open(winURL, winName, winStr);
	}
	else
	{
	winID.location.href=winURL;
	winID.focus();
	}
return winID;
}


function controllerObjectStartDrag(evt)
{
var elem;
var parent;
	if(evt) // Gecko, Opera
	{
	elem=evt.target;
	parent=elem.parentNode;
	}
	else
	{
	evt=window.event;
	elem=evt.srcElement;
	parent=elem.parentElement;
	}
	if(elem.id>"")
	{
	objectDragID=elem.id;
		if(isMSIE)
		{
		objectID=$(objectDragID).parentElement.id;
		}
		else
		{
		objectID=$(objectDragID).parentNode.id;
		}
	var x=parseInt($(objectID).style.left);
	var y=parseInt($(objectID).style.top);
		if(x && y)
		{
		$(objectID).style.cursor="move";
		objectDragApproved=1;
		objectStartX=evt.clientX-x;
		objectStartY=evt.clientY-y;
		document.body.onmousedown=new Function("return false;");
		}
	}
}


function controllerObjectDrag(evt)
{
var minX=1;
var minY=1;
var scrollShiftX;
var scrollShiftY;
	if(isGecko)
	{
	scrollShiftX=window.scrollX;
	scrollShiftY=window.scrollY;
	}
	else if(!isMSIE7)
	{
	scrollShiftX=document.body.scrollLeft;
	scrollShiftY=document.body.scrollTop;
	}
	var maxX=document.body.offsetWidth+scrollShiftX-20;
	var maxY=document.body.offsetHeight+scrollShiftY-5;
	if(evt) // Gecko, Opera
	{
	;
	}
	else
	{
	evt=window.event;
	}
	if(objectDragApproved)
	{
	$(objectID).style.cursor="move";
	var newLeft=evt.clientX-objectStartX;
		if(newLeft<=minX) newLeft=minX;
	var newRight=newLeft+parseInt($(objectID).style.width);
		if(newRight>=maxX) newLeft=maxX-parseInt($(objectID).style.width);
	var newTop=evt.clientY-objectStartY;
		if(newTop<=minY) newTop=minY;
	var newBottom=newTop+parseInt($(objectID).style.height);
		if(newBottom>=maxY) newTop=maxY-parseInt($(objectID).style.height);
	$(objectID).style.left=newLeft+"px";
	$(objectID).style.top=newTop+"px";
	$(objectID).setAttribute("left", (newLeft-scrollShiftX));
	$(objectID).setAttribute("top", (newTop-scrollShiftY));
	return false;
	}
return true;
}


function controllerObjectStopDrag()
{
objectDragApproved=0;
	if(objectID>"")
	{
	$(objectID).style.cursor="default";
	document.body.onmousedown=null;
	}
objectID="";
}


function controllerCheckFlash()
{
var i;
var shock = false;
var flash = false;
	if(navigator.plugins.length)
	{
		for (i=0; i < navigator.plugins.length; i++)
		{
		if (navigator.plugins[i].name.indexOf("Shockwave Flash") != -1)
		shock = true;
		}
	}
	else
	{
		for(i=8;i>0;i--)
		{
		eval('try{flash=new ActiveXObject("ShockwaveFlash.ShockwaveFlash."+i);}catch(e){}');
			if(flash)
			{
			shock = true;
			break;
			}
		}
	}
return shock;
}


function controllerCenterPanel(panelID, panelWidth, panelHeight)
{
var windowHeight, windowWidth;
var scrollShiftY=0;
var scrollShiftX=0;
if(isGecko)
{
scrollShiftX=window.scrollX;
scrollShiftY=window.scrollY;
}
else if(document.body.scrollTop)
{
scrollShiftX=document.body.scrollLeft;
scrollShiftY=document.body.scrollTop;
}
else if(document.documentElement)
{
scrollShiftX=document.documentElement.scrollLeft;
scrollShiftY=document.documentElement.scrollTop;
}
	if(window.innerHeight)
	{
	windowWidth=window.innerWidth;
	windowHeight=window.innerHeight;
	}
	else
	{
		if (document.documentElement && document.documentElement.clientHeight)
		{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
		}
		else
		{
		windowWidth=document.body.clientWidth;
		windowHeight=document.body.clientHeight;
		}
	}
var leftPos=scrollShiftX+(windowWidth-panelWidth)*.5+'px';
var topPos=scrollShiftY+(windowHeight-panelHeight)*.5+'px';
$(panelID).style.left=leftPos;
$(panelID).style.top=topPos;
}


function controllerCreateXMLHTTPObject()
{
var request = null;
	if(!request)
	try
	{
	request=new ActiveXObject('Msxml2.XMLHTTP');
	} catch(e){}
	if(!request)
	try
	{
	request=new ActiveXObject('Microsoft.XMLHTTP');
	} catch (e){}
	if(!request)
	try
	{
	request=new XMLHttpRequest();
	} catch (e){}
return request;
}


function controllerXMLHTTPRequest(method, url, data, callback)
{
var request = controllerCreateXMLHTTPObject();
if(!request) return false;
request.onreadystatechange = function()
{
if(request.readyState == 4 && callback) callback(request);
};
request.open(method, url, true);
request.send(data);
return true;
}


function controllerRender(obj)
{
eval(obj.responseText);
}


function controllerCreateIFrame(url)
{
var bodyElem;
var iframeElem;
	if($("controller_iframe"))
	{
	iframeElem=$("controller_iframe");
	iframeElem.setAttribute("src", url);
	}
	else
	{
	iframeElem=document.createElement("IFRAME");
	iframeElem.setAttribute("id", "controller_iframe");
	iframeElem.setAttribute("width", "0");
	iframeElem.setAttribute("height", "0");
	iframeElem.setAttribute("scrolling", "no");
	iframeElem.setAttribute("frameborder", "0");
	iframeElem.setAttribute("src", url);
	bodyElem=document.getElementsByTagName("body")[0];
	bodyElem.appendChild(iframeElem);
	}
}


function controllerAssignFunctionsByClass(elemClassName, evt, func)
{
re=new RegExp(elemClassName+'$');
var elems = document.getElementsByTagName('*');
	for(var i=0; i<elems.length; i++)
	{
		if(elems[i].className)
		{
			if(re.test(elems[i].className))
			{
			eval('elems[i].' + evt + '=' + func + ';');
			}
		}
	}
}


function controllerAssignFunctionsByID(elemID, evt, func)
{
re=new RegExp(elemID);
var elems = document.getElementsByTagName('*');
	for(var i=0; i<elems.length; i++)
	{
		if(elems[i].id)
		{
			if(re.test(elems[i].id))
			{
			eval('elems[i].' + evt + '=' + func + ';');
			}
		}
	}
}


function controllerCountElementsOfClass(elemClassName)
{
re=new RegExp(elemClassName+'$');
var elems = document.getElementsByTagName('*');
var num=0;
	for(var i=0; i<elems.length; i++)
	{
		if(elems[i].className)
		{
			if(re.test(elems[i].className))
			{
			num++;
			}
		}
	}
return num;
}


function controllerGetElementByClassByPosition(elemClassName, pos)
{
re=new RegExp(elemClassName+'$');
var current_pos=0;
var elems = document.getElementsByTagName('*');
	for(var i=0; i<elems.length; i++)
	{
		if(elems[i].className)
		{
			if(re.test(elems[i].className))
			{
				if(current_pos==pos)
				{
				return elems[i];
				}
			current_pos++;
			}
		}
	}
return null;
}


function controllerGetPositionByClassByElement(elemClassName, elem)
{
re=new RegExp(elemClassName+'$');
var pos=0;
var elems = document.getElementsByTagName('*');
	for(var i=0; i<elems.length; i++)
	{
		if(elems[i].className)
		{
			if(re.test(elems[i].className))
			{
				if(elems[i]==elem)
				{
				return pos;
				}
			pos++;
			}
		}
	}
return pos;
}