/**
*/

agent = navigator.userAgent.toLowerCase();
is_ie = ((agent.indexOf("msie") != -1) && (agent.indexOf("opera") == -1));
is_opera = agent.indexOf("opera") != -1;

function doPreview(eventTarget,dialogUrl){
    var saveReq=null;
	try{saveReq=isSaveRequired()}catch(e){};
	if (saveReq){
		showApplyChangesDialog(
		    function(useraction){
			    var canNavigate=true;
			    switch(useraction){
				    case 1:{
                        canNavigate = ValidateForms();
				        if(canNavigate) __doPostBack(eventTarget,"true");
				    }break;
				    case 0:{
                        __doPostBack(eventTarget,"false");
				    }break;
			    }
		    }
		);
	}
	else{
        __doPostBack(eventTarget,"false");
	}
}

function doNavigate(eventTarget,url,PostBackUrl){
    try{onBeforeSaveReqCheck()}catch(e){};
    var saveReq = null;
	try{saveReq=isSaveRequired();}catch(e){};
	if (saveReq){
	   showApplyChangesDialog(
            function(useraction){
                var canNavigate=true;
                switch(useraction){
	                case 1:{
                        canNavigate = ValidateForms();
	                    if(canNavigate) {
                            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(eventTarget, url+":true", true, "", PostBackUrl, false, true));
                            try{if (Page_ClientValidate && !Page_IsValid) setSaveRequired(true);}catch(e){};
                            try{if (Page_ClientValidate && !Page_IsValid) onClientValidateFailed();}catch(e){};
                        }
	                }break;
	                case 0:{
                            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(eventTarget, url+":false", false, "", PostBackUrl, false, true));
	                };break;
                }
            });
 
	}
	else{
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(eventTarget, url+":false", false, "", PostBackUrl, false, true));
	}
	return false;
}

function ValidateForms()
{
	if (typeof(Page_ClientValidate) == 'function' && typeof(Page_Forms) == 'object')
	{
		for (var cur in Page_Forms)
		{
			var vg = Page_Forms[cur];
			if (!Page_ClientValidate(vg))
				return false;
		}
	}
	
	if (typeof(CheckData) == 'function')
	{
		if (!CheckData())
			return false;
	}
	
	return true;
}

function doFormNavigate(eventTarget,PostBackUrl){
    try{onBeforeSaveReqCheck()}catch(e){};
    var saveReq=null;
	try{saveReq=isSaveRequired()}catch(e){};
	if (saveReq){
	    
		showApplyChangesDialog(
		    function(useraction){
			    var canNavigate=true;
			    switch(useraction){
				    case 1:{
						canNavigate = ValidateForms();
                        if (canNavigate) {
                            //try{setSaveRequired(false);}catch(e){};
                            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(eventTarget, true, true, "", PostBackUrl, false, true));
                            try{if (Page_ClientValidate && !Page_IsValid) setSaveRequired(true);}catch(e){};
                            try{if (Page_ClientValidate && !Page_IsValid) onClientValidateFailed();}catch(e){};
                        }
				    }break;
				    case 0: {
				            //Commented out by A.Fioletov.
                            //try{setSaveRequired(false);}catch(e){};
                            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(eventTarget, false, false, "", PostBackUrl, false, true));
				    }break;
				    case -1:{
                       try{onApplyCanceled();}catch(e){}
				    };
			    }
		    }
		);
	}
	else{
        try{setSaveRequired(false);}catch(e){};
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(eventTarget, false, false, "", PostBackUrl, false, true));
    }
    return false;
}


function openDialog(url, action, init) {
	if (typeof init == "undefined") {
		init = window;	// pass this window object by default
	}
	if (document.all) {	// here we hope that Mozilla will never support document.all
		var value = window.showModalDialog(url, init, "dialogHeight:150px; dialogWidth:320px; resizable: no; help: no; status: no; scroll: no; center: yes; ");	
		if (action) {
			action(value);
		}
	} else {
		return openDialog._geckoOpenModal(url, action, init);
	}
};
openDialog._return = null;

openDialog._modal = null;

openDialog._arguments = null;

openDialog._geckoOpenModal = function(url, action, init) {
	var wnd =  window;
	var px = wnd.screenX + wnd.outerWidth;
	var py = wnd.screenY + wnd.outerHeight;
	var x = (px -320)/ 2;
	var y = (py-150) / 2;
	var dlg = window.open(url, "ha_dialog",
			      "location=no,directories=no,dialog=yes,modal=yes,toolbar=no,menubar=no,personalbar=no, sidebar=no,width=320,height=150," +
			      "scrollbars=no,resizable=no,status=no, outerWidth=320, outerHeight=150, left="+x+", top="+y);
	
	openDialog._modal = dlg;
	openDialog._arguments = init;

	function capwin(w) {
		w.addEventListener("click", openDialog._parentEvent, true);
		w.addEventListener("mousedown", openDialog._parentEvent, true);
		w.addEventListener("focus", openDialog._parentEvent, true);
	};

	function relwin(w) {
		w.removeEventListener("focus", openDialog._parentEvent, true);
		w.removeEventListener("mousedown", openDialog._parentEvent, true);
		w.removeEventListener("click", openDialog._parentEvent, true);
	};

	openDialog._return = function (val) {
		if ((val || val==0) && action) {
			action(val);
		}
		openDialog._modal = null;
	};
};

function sp2Detect(par){
	var mv=navigator.appMinorVersion;
	if(/sp2/i.test(mv)){
		return true;
	}else{
		return false;
	}
}

function validLink(temp){
	var str=temp;
	while(str.match(/http:([^\"]*?)\/preview/)!= null){
		str = str.replace(/http:([^\"]*?)\/preview/,".");
	}
	return str;
}

var winStl = null;
var winProp = null;

function doCloseWindowProp(){
	try{
		winStl.close();
	}catch(e){
	}
	try{
		winProp.close();
	}catch(e){
	}
}

function checkSymbol (value, symbol)
{
	  var success = /[\';]/.test(value);
	  return success;
}


function checkLengthofTextArea(obj,maxLength)
{
    var o;
	o=obj;

    if(o.value.length>maxLength)
	{
        o.value = o.value.substr(0, maxLength);
    }
    try{setSaveRequired(true);}catch(e){};
}


function doEncodeFields(arrayFields)
{
	var tempField = null;
	for(var i = 0; i < arrayFields.length; i++)
	{
		tempField = document.getElementsByName(arrayFields[i])[0];
		if(tempField) doEncodeField(tempField);
		tempField = null;
	}
}

function doEncodeField(field)
{
	var valueString = field.value;
	if(valueString == null) return false;
	field.value = doEncode(valueString);
}

function doEncode(str)
{
    toRet = str;
	while(toRet.match(/</ig) != null){
        toRet = toRet.replace(/</ig,'&lt;');
    }
    while(toRet.match(/>/ig) != null){
        toRet = toRet.replace(/>/ig,'&gt;');
    }
    return toRet;
}


function checkImageExtension(inpFile)
{     
    if(inpFile.value.substr(inpFile.value.length-4,4).toLowerCase()==".gif" 
    || inpFile.value.substr(inpFile.value.length-4,4).toLowerCase()==".png" 
    || inpFile.value.substr(inpFile.value.length-4,4).toLowerCase()==".jpg" 
    || inpFile.value.substr(inpFile.value.length-4,4).toLowerCase()==".bmp"
    || inpFile.value.substr(inpFile.value.length-5,5).toLowerCase()==".jpeg")
    {
		return true;
    }
    else
    {
		return false;
    }
}

function GridViewCheckAll(src,namePart){
	var ch=src.checked;
	var elt=document.getElementsByTagName("input");
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).type.toLowerCase()=='checkbox'  && elt.item(i).id.toLowerCase().indexOf(namePart.toLowerCase())>=0 && !elt.item(i).disabled) elt.item(i).checked=ch;
	}
}
function GridViewIsSelected(namePart){
	var elt=document.getElementsByTagName("input");
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).type.toLowerCase()=='checkbox'  && elt.item(i).id.toLowerCase().indexOf(namePart.toLowerCase())>=0 && !elt.item(i).disabled && elt.item(i).checked) {
		    return true;
		}
	}
	return false;
}
function GridViewSelectedCount(namePart){
	var elt=document.getElementsByTagName("input");
	count = 0;
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).type.toLowerCase()=='checkbox'  && elt.item(i).id.toLowerCase().indexOf(namePart.toLowerCase())>=0 && elt.item(i).checked) count++;
	}
	return count;
}

function EnableInputTextElement(namePart, valueNew){
	var elt=document.getElementsByTagName("input");
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).type.toLowerCase()=='text'  && elt.item(i).id.toLowerCase().indexOf(namePart.toLowerCase())>=0) {
		    elt.item(i).disabled = valueNew;
		}
	}
}

function GetDropDownListValue(namePart) {
	var elt=document.getElementsByTagName('select');
	for(var i=0; i<elt.length; i++)
	{
		if(elt.item(i).id.toLowerCase().indexOf(namePart.toLowerCase())>=0) {
		    return elt.item(i).value;
		}
	}
	
	return '';
}

function resizeToAvailable() {
    window.moveTo(0,0); 
    
    if (is_ie) {
        window.resizeTo(screen.availWidth, screen.availHeight);
    } else {
        window.outerHeight = screen.availHeight; 
        window.outerWidth = screen.availWidth;
    } 
}

var ScrollPositionId = "";

function SetScrollPosition(height, width)
{
	try
	{
		var el = document.getElementById(ScrollPositionId);
		if(el)
		{
			el.value = height + "," + width;
		}
	}catch(e){}
}

function resizeDivContent()
{
	SetScrollPosition();
    var options = document.getElementsByName("inputResizeOption");
    for(var i = 0; i<options.length; i++)
    {   
        var mheight;
        var name = "";
        var height = 200;
        if(/,/.test(options[i].value))
        {
            var option = options[i].value.split(",");
            name = option[0];
            height = parseInt(option[1]);
        }
        var div = document.getElementById(name);
        if(div)
        {
            if (document.body.scrollHeight > document.body.clientHeight)
                mheight = parseInt(document.body.scrollHeight);
            else 
                mheight = parseInt(document.body.clientHeight);

            if (div.scrollHeight > (mheight-height))
            {
                div.style.height=(mheight-height);
                //SetScrollPosition((mheight-height), div.scrollWidth)
            }
            else
            {
                div.style.height = div.scrollHeight;
                //SetScrollPosition(div.scrollHeight, div.scrollWidth)
            }
            SetScrollPosition(document.body.scrollHeight, div.scrollWidth)
        }
    }
}

function checkUrl(url, errorMessage){
    if (url.replace(/^\s+|\s+$/g,'')=='') return true; 
    var v = new Validator();
    try {
        var res = v.verURL(url,3,false); 
        if (!res)SetErrorMessage(errorMessage,false);
        return res;
    }
    catch(e){}
}

function trimStr(str) {
	return(str.replace(/(^\s*)|(\s*$)/g, ""));
} 

function setElementVisibilty( elementID, isVisible, width )
{
	var el = document.getElementById( elementID );
	if ( is_ie )
		el.style.display = isVisible ? "block" : "none";
	else
		el.style.display = isVisible ? "inline" : "none";
		
	el.style.width = width;
	el.width = width;
}


function AdminPanelCheckToResize() {
    
    if (is_ie) {
        window.attachEvent('onresize', checkAdminPanelSize);
        window.attachEvent('onload', checkAdminPanelSize);
    } 
    else {
        window.addEventListener('resize', checkAdminPanelSize, false);
        window.addEventListener('load', checkAdminPanelSize, false);
    }
}
function checkAdminPanelSize(){
    updateDivSize("DivLeftMenu",true);
    updateDivSize("DivAdminPanel",true);
}
function updateDivSize(id, changeWidth){
		
        var div = document.getElementById(id);
        var mheight,mwidth;
        if(div)
        {
			if( is_opera )
			{
				div.style.height = "100%";
				return;
			}

            var iWidth=parseInt(div.scrollWidth);
            var iHeight=parseInt(div.scrollHeight);
            div.style.height=10;
            if (changeWidth) div.style.width=10;

            if(document.all)
            {
                mheight = parseInt(div.parentNode.offsetHeight)-
                          parseInt(div.parentNode.currentStyle.paddingTop)-
                          parseInt(div.parentNode.currentStyle.paddingBottom);
                mwidth = parseInt(div.parentNode.offsetWidth)-
                          parseInt(div.parentNode.currentStyle.paddingLeft)-
                          parseInt(div.parentNode.currentStyle.paddingRight);
            }
            else
            {
                mheight = parseInt(div.parentNode.offsetHeight);
                mwidth = parseInt(div.parentNode.offsetWidth);
                var p=div.parentNode;
                while (true){
                    if (!isNaN(parseInt(p.style.paddingBottom))){
                        mheight-=parseInt(p.style.paddingBottom);
                    }
                    if (!isNaN(parseInt(p.style.paddingTop))){
                        mheight-=parseInt(p.style.paddingTop);
                    }
                    if (!isNaN(parseInt(p.style.paddingLeft))){
                        mwidth-=parseInt(p.style.paddingLeft);
                    }
                    if (!isNaN(parseInt(p.style.paddingRight))){
                        mwidth-=parseInt(p.style.paddingRight);
                    }
                    if (!isNaN(parseInt(p.cellPadding))){
                        mheight-=2* parseInt(p.cellPadding);
                        mwidth-=2* parseInt(p.cellPadding);
                    }
                    if(p=='[object HTMLTableElement]') break;
                    else p=p.parentNode;
                }
                
                          
            }
            if (iHeight> mheight)
            {
                div.style.height=mheight;
            }
            else
            {
                div.style.height = "100%";//div.scrollHeight;
            }
            
            if (changeWidth){
                //alert(div.scrollWidth);
                //alert(mwidth);
                if (iWidth> mwidth)
                {
                    div.style.width=mwidth;
                }
                else{
                    div.style.width="100%";
                }
                //alert(div.style.width);
            }
            
        }
}


function addEvent(obj, evType, fn){
    if (obj.addEventListener){
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent){
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}

function removeEvent(obj, evType, fn){
    if (obj.removeEventListener){
        obj.removeEventListener(evType, fn, false);
        return true;
    } else if (obj.detachEvent){
        var r = obj.detachEvent("on"+evType, fn);
        return r;
    } else {
        return false;
    }
}


function showElelement(element, show)
{
    if(show) 
        element.style.display = "";
    else
        element.style.display = "none";
}


function SetContextHelp(ctl){
    if (!ctl) return;
    if (ctl.getAttribute && ctl.getAttribute("ContextHelp")) {
        //SetShortHelp defined in ~/Controls/LeftMenu.ascx
        if(typeof(SetShortHelp)=="function") {
            SetShortHelp(ctl.getAttribute("ContextHelp"));
        }
    } else {
        SetContextHelp(ctl.parentNode);
    }
}
function SetContextHelpOnBody(aEvent) {
    if (aEvent && aEvent.target) {
        elem = aEvent.target;
    } else if(window.event) {
        elem = window.event.srcElement;
    }           
    if(elem) {
        SetContextHelp(elem);
    }  
    
}


function RadTabStripFixTabWidth(sender) {
    var tabStrip = sender;
    maxWidth = 120;
    for (var i=0; i<tabStrip.Tabs.length; i++) {
        if (tabStrip.Tabs[i].DomElement.clientWidth > maxWidth) {
            maxWidth = tabStrip.Tabs[i].DomElement.clientWidth ;
        }
    }      
    
    for (var i=0; i<tabStrip.Tabs.length; i++) {   
        tabStrip.Tabs[i].DomElement.firstChild.firstChild.style.width = maxWidth - 20 +"px";
    }
}
    

function getEditorContent(editorId) {
    var editor = GetRadEditor(editorId);
    var content = editor.GetHtml(true);
    
    return content;
}

function isContentChanged(prevContentHiddenId, editorId){
    var prevContent = document.getElementById(prevContentHiddenId).value;
    var newContent = getEditorContent(editorId);
    return (newContent != prevContent);
}
	
	

function enterSubmit(ev, submitBtn) {
    try{
        if(ev.keyCode==13){
            var ctl = document.getElementById(submitBtn); 
            if(ctl) {
                ctl.focus();
            }
        }  
    } 
    catch(e){}
}
