﻿// getElementById helper function
function element(id) {
    if (id && id != "") return document.getElementById(id);
}

// selectedValue helper function
function selectedValue(ddl) {
    if (ddl && ddl.options) return ddl.options[ddl.selectedIndex].value;
}

// selectedText helper function
function selectedText(ddl) {
    if (ddl && ddl.options) return ddl.options[ddl.selectedIndex].text;
}

// return the value of the selected item in a radio button list
function getSelectedRadioButtonItemValue(radioButtonListControlName) {
	var listElements = document.getElementsByName(radioButtonListControlName);
	for (var i = 0; i < listElements.length; i++) {
		if (listElements[i].checked) {
			return listElements[i].value;
		}
	}
	return null;
}

// setFocus helper function - bNoSelect=true prevents hilighting of control contents
function setFocus(id, bNoSelect) {
      var ctrl = element(id);
      if (ctrl) {
        if (ctrl.focus) ctrl.focus();
        if (ctrl.select && !bNoSelect) ctrl.select();
      }
}

// string trim function
function trim(str,ch) {
    if (ch) 
        return String(str).trim(ch);   // must escape ch with \\ if special char, e.g. \\$
    else
        return String(str).trim();
}
 
// trim method on string
String.prototype.trim = function(ch) { 
    if (ch) 
        return this.replace(new RegExp('^' + ch + '+|' + ch + '+$', 'g'), '');     // must escape ch with \\ if special char, e.g. \\$
    else
        return this.replace(/^\s+|\s+$/g, '');
}

// clear initial text value from a text box
function clearInitialTextValue(initialTextValue, el) {
	if (el.value == initialTextValue) el.value = '';
}	

// check that the __EVENTVALIDATION element has rendered - form submission is not allowed until this is available!
function check__EVENTVALIDATION() {
    return !(element('__EVENTVALIDATION')==null);
}

// clickOnce handlers for buttons
function clickOnce(ctrl,message,disable) {
    ctrl.blur();
    var ctrl_stopclick = ctrl.stopclick;
    var ctrl_disabled = ctrl.disabled;
    if (typeof(ctrl_disabled) == "undefined") {
        ctrl_disabled = (ctrl.getAttribute && ctrl.getAttribute("disabled") == "disabled");
    } 
    if (!ctrl_stopclick && !ctrl_disabled && check__EVENTVALIDATION()) {
        ctrl.stopclick = true;
        if (typeof(message) == "undefined") {
            message = "";
        } else if (message == "@") {
            message = "Wait...&nbsp;";
        } else {
            message = message.replace(/'/g,"\\'");
        }
        if (typeof(disable) == "undefined") disable = true;
        setTimeout("_delayed_clickOnce('"+ctrl.id+"','"+message+"',"+disable+")",100);
        return true;
    }
    return false;

}

function _delayed_clickOnce(id,message,disable) {
    var ctrl = document.getElementById(id);
    if (ctrl) {
        if (typeof(Page_ClientValidate) != "function" || Page_IsValid) {
            if (typeof(ctrl.value) != "undefined" && message != "") {
                ctrl.value = message;
            } else if (typeof(ctrl.innerHTML) != "undefined" && message != "") {
                ctrl.innerHTML = message;
            }
            if (message != "") ctrl.title = message.replace(/&.+;/g,"");
            ctrl.style.cursor = 'wait';
            document.body.style.cursor = 'wait';
            ctrl.disabled = disable;
        } else {
            ctrl.stopclick = false;
        }
    }
}

// utility for setting unique radio buttons in asp repeater controls
function setUniqueRadioButton(nameregex, current) {
   var re = new RegExp(nameregex);
   for (var i=0, el; i < document.forms[0].elements.length; i++) {
      el = document.forms[0].elements[i];
      if (el.type == 'radio' && re.test(el.name)) el.checked = false;
   }
   current.checked = true;
}

// utility for checking the set unique radio button in asp repeater control
function checkSetRadioButton(nameregex) {
   var re = new RegExp(nameregex);
   for (var i=0, j=0, el; i < document.forms[0].elements.length; i++) {
      el = document.forms[0].elements[i];
      if (el.type == 'radio' && re.test(el.name) && ++j && el.checked) return j;
   }
   return false;
}

function ConfirmUserPropertyUpdates(updatesRequired, updatesRemaining, isUpdateAll, hdnActionClientId) {
	var confirmMessage = "This update will use "
									+ updatesRequired
									+ " of your "
									+ updatesRemaining
									+ " remaining updates for this 30 day period, do you wish to continue?";
									
	if (confirm(confirmMessage))
	{
		if (isUpdateAll.toLowerCase() == "true")
		{
			element(hdnActionClientId).value = "UPDATE_ALL";
		}
		else
		{
			element(hdnActionClientId).value = "UPDATE_PROP";
		}
		
		document.forms[0].submit();
	}
}

function ConfirmUserPropertyDelete() {
	return result = confirm("You are about to delete a property from your selection, are you sure?");
}

function OpenPopupWindow(url,size) {
    var width = 800, height = 500;
    if (size) {
        if (size.width) width = size.width;
        if (size.height) height = size.height;
    }
    var features = "status=yes,toolbar=no,top=50,left=50,scrollbars=yes,resizable=yes,menubar=yes,location=no";
    features += ",width=" + width;
    features += ",height=" + height;
    window.open(url,'_blank',features);
    return false;
}

function DisplayCalendarControl(calendarScreenUrl, calendarLinkCtrl, dateInputControlName)
{
	var formId = document.forms[0].id;
	var index = calendarLinkCtrl.id.lastIndexOf("_");
	var ctrlIdPrefix = calendarLinkCtrl.id.substring(0, index);
	var dateControlId = ctrlIdPrefix + "_" + dateInputControlName;
	var dateValue = element(dateControlId).value;
	
	var calendarWindow = window.open(calendarScreenUrl + "?datecontrolref=theForm." + dateControlId + "&inputDate=" + dateValue, "calendar_window", "width=250, height=220");
	calendarWindow.focus();
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{	
	if (getCookie(name))
    {	
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* Image swappers */

function mm_swapImgRestore() { //v3.0
  var i,x,a=document.mm_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function mm_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.mm_p) d.mm_p=new Array();
    var i,j=d.mm_p.length,a=mm_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.mm_p[j]=new Image; d.mm_p[j++].src=a[i];}}
}

function mm_swapImage() { //v3.0
  var i,j=0,x,a=mm_swapImage.arguments; document.mm_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=mm_findObj(a[i]))!=null){document.mm_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function mm_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
