﻿function AddStringDelimited(sOrig,sValue,sDelimited){
    if (sOrig.length < 1) 
        return sValue;
    else return sOrig + sDelimited  + sValue;
}
function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function
function IsValidEmail(sEmail,bIsRequired){
      var email_regex = /^([a-zA-Z0-9_\.\-\u0021-\u002b])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/,
      valid = true;
      if (sEmail.length < 1){
        if (bIsRequired) 
            valid = false;
         
      }else{
          var emailParts = sEmail.split(";");
          for (var i = 0; i < emailParts.length; i++) {
              if (!email_regex.test(emailParts[i])) {
                valid = false;
              }      
          }
      }
      return valid;
      
}
function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} //
function JavaDateFormat(sDate, displayPat){
    /********************************************************
    *   Valid Masks:
    *   !mmmm = Long month (eg. January)
    *   !mmm = Short month (eg. Jan)
    *   !mm = Numeric date (eg. 07)
    *   !m = Numeric date (eg. 7)
    *   !dddd = Long day (eg. Monday)
    *   !ddd = Short day (eg. Mon)
    *   !dd = Numeric day (eg. 07)
    *   !d = Numeric day (eg. 7)
    *   !yyyy = Year (eg. 1999)
    *   !yy = Year (eg. 99)
   ********************************************************/
	var aDate	
	if (sDate == null || sDate == "") return ""		
	aDate = new Date(sDate)
    intMonth = aDate.getMonth();
    intDate = aDate.getDate();
    intDay = aDate.getDay();
    intYear = aDate.getFullYear();

    var months_long =  new Array ('January','February','March','April',
       'May','June','July','August','September','October','November','December')
    var months_short = new Array('Jan','Feb','Mar','Apr','May','Jun',
       'Jul','Aug','Sep','Oct','Nov','Dec')
    var days_long = new Array('Sunday','Monday','Tuesday','Wednesday',
       'Thursday','Friday','Saturday')
    var days_short = new Array('Sun','Mon','Tue','Wed','Thu','Fri','Sat')

    var mmmm = months_long[intMonth]
    var mmm = months_short[intMonth]
    var mm = intMonth < 9?'0'+ (1 + intMonth) + '':(1+intMonth)+'';
    var m = 1+intMonth+'';
    var dddd = days_long[intDay];
    var ddd = days_short[intDay];
    var dd = intDate < 10?'0'+intDate+'':intDate+'';
    var d = intDate+'';
    var yyyy = intYear;

    century = 0;
    while((intYear-century)>=100)
        century = century + 100;

    var yy = intYear - century
    if(yy<10)
        yy = '0' + yy + '';

    displayDate = new String(displayPat);

    displayDate = displayDate.replace(/!mmmm/i,mmmm);
    displayDate = displayDate.replace(/!mmm/i,mmm);
    displayDate = displayDate.replace(/!mm/i,mm);
    displayDate = displayDate.replace(/!m/i,m);
    displayDate = displayDate.replace(/!dddd/i,dddd);
    displayDate = displayDate.replace(/!ddd/i,ddd);
    displayDate = displayDate.replace(/!dd/i,dd);
    displayDate = displayDate.replace(/!d/i,d);
    displayDate = displayDate.replace(/!yyyy/i,yyyy);
    displayDate = displayDate.replace(/!yy/i,yy);

    return displayDate;
}
function BowkerToNumber(sNum){
    if (!isNaN(Number(sNum)) && Number(sNum) >= 0)
        return Number(sNum);
    else return 0;
}
function BowkerRoundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
function GetqueryParam(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function BowkerAjaxCall(Url,DataHandleFunc,pData,pParam,pAsynch ) {
        var x;
        var gotData = pData ? pData : gotData="{}";
        var gotParam = pParam ? "(msg, pParam)" : "(msg)";
        var gotAsynch = pAsynch ? false : true ;
        
        jQuery.ajax({
          type: "POST",
          data: gotData,              
          url: Url,
          contentType: 'application/json; charset=utf-8',
          dataType: "json",
          async: gotAsynch,
          success: function(msg) {
            eval(DataHandleFunc + gotParam );
            
          },        
          error: function(xhr, status, error) 
          { alert (xhr.responseText); }
        });     
}
function BowkerAjaxGetHTML(Url,DataHandleFunc) {
        
        jQuery.ajax({
          type: "GET",
          url: Url,
          async: true,
          dataType: "html",
          success: function(msg) {
            eval(DataHandleFunc + '(msg)');
            
          },        
          error: function(xhr, status, error) 
          { alert (xhr.responseText); }
        });     
}

function IndexOf(arr, findMe){
    var I;
    var pos = -1;
    for (I=0; I<= arr.length; I++){
        if (arr[I] == findMe){
            pos = I;
            break;
        }
    }
    return pos;
}
function BowkerAjaxCallReturn(Url,pData) {
        var x;
        var gotData = pData ? pData : gotData="{}";
        var retMSG;
        
        jQuery.ajax({
          type: "POST",
          data: gotData,              
          url: Url,
          contentType: 'application/json; charset=utf-8',
          dataType: "json",
          async: false,
          success: function(msg) {
            retMSG = msg;
          },        
          error: function(xhr, status, error) 
          { alert (xhr.responseText); }
        }); 
        return retMSG;    
}

function BowkerSetSelectOption(sel,valSel){
    var I;
    for (I=0; I<sel.options.length; I++){
        if (sel.options[I].value == valSel){
            sel.selectedIndex = I;
            break;
        }
    }
}
var oldShowModalDialog = window.showModalDialog;
window.showModalDialog = function(url, args, options)
{   
    if (navigator.appName!="Microsoft Internet Explorer") {  
    //debugger ;
      // Convert the options string into an object.
      var pairs = options.replace(/\s+/g, "").split(";");
      var option = {};
      $.each(pairs, function()
      {
        var pair = this.split(":");
        if (pair.length != 2) return true;
     
        option[pair[0]] = pair[1];
      });
     
      // Get the width and height of the document.
      var width = $(document).width();
      var height = $(document).height();
     
      // Get the width and height of the dialog.
      var dialogWidth = option.dialogWidth.replace("px", ""); 
      var dialogHeight = option.dialogHeight.replace("px", "");
     
      // Calculate where the dialog needs to be to be
      // centered.
      var dialogLeft = (width - dialogWidth) / 2;
      var dialogTop = (height - dialogHeight) / 2;
     
      // Add those settings to the options string.
      options += "dialogLeft: " + dialogLeft + "; ";
      options += "dialogTop: " + dialogTop + "; ";
  }
  // Call the original function.
  return oldShowModalDialog(url, args, options);
};
