﻿var formSubmit = false;
function anyChkBoxDeleted(reg_exp, checkOnly)
{
   if (checkOnly == null) checkOnly = false;

   var parentDiv = document.getElementById(reg_exp);
   
   var objectInput = parentDiv.getElementsByTagName('input');
   
   var objectDeleted = false;
   
   for (var i = 0; i < objectInput.length; i++)
   {
     var obj = objectInput[i];
     if (obj.type == "checkbox")
     {
       if (obj.checked)
       {
         objectDeleted = true;
         break;
       }
     }
   }
   
   if (objectDeleted && checkOnly) 
        return true;
   
   if (objectDeleted)
   {
     return confirm("Are you sure you want to delete these items?");
   }
   else
   {
     if (checkOnly) return false;
     
     alert("Please select items to be deleted from the list");
     return false;
   }
}

function stringTrim(strToTrim) {
	return(strToTrim.replace(/^\s+|\s+$/g, ''));
}

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}


//Set the default button, default button must have ID = btnSubmit
var defButton = null;
function setDefaultSubmitButton()
{
    var regExp = /\w*btnSubmit$/;
    
    var inputElms = document.getElementsByTagName("input");
    for (var i = 0; i < inputElms.length; i++)
    {
        var elm = inputElms[i];
        if (elm.type != "button" && elm.type != "submit") continue;
        if (elm.id.search(regExp) != -1)
            defButton = elm;
    }
    
    var anchorElms = document.getElementsByTagName("a");
    for (var i = 0; i < anchorElms.length; i++)
    {
        var elm = anchorElms[i];
        if (elm.id.search(regExp) != -1)
            defButton = elm;
    }
    
    var e = (document.addEventListener) ? 'keypress' : 'keydown';
    
    if (document.addEventListener) {
        document.addEventListener(e, KeyEventHandler, false);
    } else if (document.attachEvent) {
        var r = document.attachEvent("on" + e, KeyEventHandler);
    } else {
        alert(e + " handler could not be attached");
    }
    
//    if (defButton != null && defButton.href != "")
//    {
//        if (document.addEventListener) {
//            defButton.addEventListener(e, defButtonHandler, false);
//        } else if (document.attachEvent) {
//            var r = defButton.attachEvent("on" + e, defButtonHandler);
//        } else {
//            alert(e + " handler could not be attached");
//        }
//    }
    
}

function KeyEventHandler(e)
{
 
    if (doesSearchHeaderInFocus() || formSubmit) return;   
    
    var targetElm = e.target || e.srcElement;
    var key = e.keyCode || e.which;
    
    if (defButton == null) return;
    
    if (key == 13 && targetElm.type != 'textarea' && targetElm != "undefined" && targetElm != null) 
    {
        if (defButton.type == 'submit' || defButton.type == 'button')
        {
            formSubmit = true;
            defButton.click();
            
        }   
        else
        {   
            formSubmit = true;
            eval(unescape(defButton.href));
        }
    }
    setTimeout("formSubmit = false;", 1000); //a trick that i need to use to avoid double submission that is caused by OnTextChanged in txtUserName
    
}

//use to set onclick function on all the anchor with "button" as their class, need some tweak to make it work
function setButtonFunction()
{
    var anchorElms = document.getElementsByTagName("a");
    for (var i = 0; i < anchorElms.length; i++)
    {
        var elm = anchorElms[i];
        if (elm.className == "Button" && unescape(elm.href) != "")
        {
            var elm1 = elm;
            elm.onkeypress = function()
            {
               linkButtonHandler(event, elm1);
            }
        }
    }
}

function linkButtonHandler(e, elm)
{
    var key = e.keyCode || e.which;
    //alert(key + " " + elm.href);
    if (key == 13)
    {
        eval(unescape(elm.href));
    }
}

function copyToClipboard(s)
{
    if( window.clipboardData && clipboardData.setData )
         clipboardData.setData("Text", s);
}

//Case #3068
function inlineTextEditImageClick(textboxid, lblid, default_text)
{
    var inlineedit_textbox = document.getElementById(textboxid);
    var inlineedit_label = document.getElementById(lblid);
    if (inlineedit_textbox == null || inlineedit_label == null) return;
    if (inlineedit_label.innerHTML.toLowerCase().indexOf('(enter text)') != -1 
        && '(enter text)' != '')
        inlineedit_textbox.value = '';
    else
    {
        var newValue = inlineedit_label.innerHTML;
        while((newValue.indexOf('<br />') > -1) || (newValue.indexOf('<br>') > -1) || (newValue.indexOf('<BR>') > -1) || (newValue.indexOf('<BR />') > -1)){ newValue = newValue.replace('<br />','\n').replace('<br>','\n').replace('<BR />','\n').replace('<BR>','\n'); }
        inlineedit_textbox.value = newValue;
    }
    inlineedit_textbox.style.height = Sys.UI.DomElement.getBounds(inlineedit_label).height + 'px';
    inlineedit_textbox.style.width = Sys.UI.DomElement.getBounds(inlineedit_label).width + 'px';
    inlineedit_label.style.display = 'none';
    inlineedit_textbox.style.display = '';
    inlineedit_textbox.focus();
}

//Case #2980
function addHiddenInputField(formElement, timerId, timerVal) {

   var inputElement = document.createElement("input");
   inputElement.setAttributeNode(createHtmlAttribute("type", "hidden"));
   inputElement.setAttributeNode(createHtmlAttribute("id", timerId));
   inputElement.setAttributeNode(createHtmlAttribute("value", timerVal));
   document.getElementById(formElement).appendChild(inputElement);
   return;
}

function createHtmlAttribute(name, value) {
   var attribute = document.createAttribute(name);
   attribute.nodeValue = value;
   return attribute;
}

function mouseOverEventCalendarDate(timerId, timerSec)
{
    var timer = document.getElementById(timerId);
    if (timer == null) return;
    
    timer.value = '1';
    setTimeout("document.getElementById('" + timerId + "').value = '0';", timerSec);
}


function TrimSpace(obj)
{
  obj.value = (obj.value).replace(/^\s*|\s*$/g,'');
}