//$(function(){
//  if (!jQuery.browser.msie)
//  {
//    $('.messagebox').corner("3px");
//  }
//});

function SlideDiv(id) {
  var div = $('#' + id);
  if (div.attr('style') == "display: none;")
  div.slideDown(300);
  else
  div.slideUp(200);
}
  
function ResetMessageBox() {
  $(".messagebox_msg").text("Your changes have been saved.");
}
  
function HideMessageBox() {
  setTimeout(function() { $('.messagebox').fadeOut(1000); }, 30000);
}

  
function hasAnchor(str) {
  str="#"+str;
  return (location.hash==str);
}

var QueryString = new PageQuery(window.location.search);

function PageQuery(q){
  if(q.length > 1) this.q = q.substring(1, q.length);
  else this.q = null;
  this.keyValuePairs = new Array();
  if(q) 
  {
    for(var i=0; i < this.q.split("&").length; i++) 
    {
      this.keyValuePairs[i] = this.q.split("&")[i];
    }
  }
  this.getKeyValuePairs = function() { return this.keyValuePairs; }
  this.getValue = function(s) {
    for(var j=0; j < this.keyValuePairs.length; j++) {
      if(this.keyValuePairs[j].split("=")[0] == s)
      return this.keyValuePairs[j].split("=")[1];
    }
    return false;
  }
  this.getParameters = function() {
    var a = new Array(this.getLength());
    for(var j=0; j < this.keyValuePairs.length; j++) {
      a[j] = this.keyValuePairs[j].split("=")[0];
    }
    return a;
  }
  this.getLength = function() { return this.keyValuePairs.length; }
}

function ExpandCollapseDataGridRow(image, firstcoltext, index)
{
  var src = image.getAttribute("src");
  if(src.indexOf("expand")>0)
  {
      var expandrow = $(image).attr('ID') + "_Row";          
      
      image.src = src.replace("expand","collapse");
      var tr = image.parentNode.parentNode;
      var next_tr = tr.nextSibling;
      var tbody = tr.parentNode;
      
      var td = image.parentNode;
      var detailnode;
      for(var j =0; j<td.childNodes.length; j++)
      {      
          if(td.childNodes[j].nodeType == 1)
          {
              if(td.childNodes[j].nodeName.toLowerCase() == 'div')
              {
                   detailnode = td.childNodes[j].cloneNode(true);
                   td.childNodes[j].innerHTML = "";
                   //detailnode.setAttribute('style','');
                   break;
              }
          }
      }
      
      var colspan = 0;
      
      for(var j =0; j<tr.childNodes.length; j++)
      {
        if (tr.childNodes[j].tagName == "TD")
        {     
          colspan++; 
        }
      }
      
      var newtr = document.createElement('tr');
      newtr.className = tr.className + "_expandrow";
      
      var newtd = document.createElement('td');
      
      /* insert an empty cell first */
      if (firstcoltext != '')
      {
        newtd.innerHTML = firstcoltext;
        //newtd.setAttribute('valign','top');
        newtr.appendChild(newtd);
        
        colspan--;
      }
      
      newtd = document.createElement('td');
      newtd.colSpan = colspan;
      newtd.innerHTML = detailnode.innerHTML;
      newtr.appendChild(newtd);    
    
      tbody.insertBefore(newtr, next_tr);
      newtr.setAttribute('style','display:none');
      newtr.setAttribute('id', expandrow);
      tr.className = tr.className + "_selected";
      
      $('#'+expandrow).show(0,function(){
         if ($('#' + expandrow + ' :input')[0] != null)
           $('#' + expandrow + ' :input')[0].focus();
       });
  }
  else{
      image.src = src.replace("collapse","expand");
      
      var td = image.parentNode;
      var row = image.parentNode.parentNode;
      var rowsibiling = row.nextSibling;
      var parenttable = row.parentNode;
      
      
      // get the contents of the td and put it in a the div it was moved from
      var detailnode;
      for(var j =0; j<td.childNodes.length; j++)
      {      
          if(td.childNodes[j].nodeType == 1)
          {
              if(td.childNodes[j].nodeName.toLowerCase() == 'div')
              {
                   detailnode = td.childNodes[j].cloneNode(true);
                   
                   if (firstcoltext != '')
                   {
                   td.childNodes[j].innerHTML = rowsibiling.childNodes[1].innerHTML;
                   }
                   else
                   {
                   td.childNodes[j].innerHTML = rowsibiling.childNodes[0].innerHTML;
                   }
                   
                   break;
              }
          }
      }
      
      
      parenttable.removeChild(rowsibiling);
      
      if(index%2==0)
      {
          row.className = row.className.replace(/_selected/,"");// "datagriditem";
      }
      else{
          row.className = row.className.replace(/_selected/,"");// "datagriditem_alt";
      }
  }
}

function DataGridClearCheckBoxes(GridID, CtrlID) {
    $('#' + GridID + ' tr').find("td:first input:checkbox:first").each(function() {
        if ($(this).attr('id') != CtrlID) {
            $(this).attr('checked', '');
        }
    });
}

function DataGridCheckParentCheckBox(GridID)
{
  // uncheck the checkbox in the header
  $('#' + GridID + ' .datagridheader').find("input:checkbox:first").attr('checked','');
  
  var checkparent = true;
  
  $('#' + GridID + ' tr:not(.datagridheader)').find("td:first input:checkbox:first").each( function() {
    if (!$(this).attr('checked'))
    {
      checkparent = false;
    }
  });
    
  if (checkparent)
  {
    $('#' + GridID + ' .datagridheader').find("input:checkbox:first").attr('checked','checked');
  }
}

function DataGridCheckAllCheckBoxes(GridID)
{
  if ( $('#' + GridID + ' .datagridheader').find("input:checkbox:first").attr('checked') )
  {
    $('#' + GridID + ' tr:not(.datagridheader)').find("input:checkbox:first").each(function(){ if (!$(this).attr('disabled')) { $(this).attr('checked','checked'); } });
  }
  else
  {
    $('#' + GridID + ' tr:not(.datagridheader)').find("input:checkbox:first").each(function(){ $(this).attr('checked',''); });
  }
}

function GridWithCheckedAction(ActionDD, name)
{
  var SelectedAction = $('#' + ActionDD.id + ' option:selected');
  
  if (SelectedAction.val() != "")
  {
    var ok = confirm('Are you sure you want to perform \'' + SelectedAction.html() + '\' on the selected ' + name +'?');
    
    if (ok)
    {
      __doPostBack(ActionDD.name,'');
    }
  }
  
  SelectedAction.removeAttr('selected');      
  $('#' + ActionDD.id + ' option:first').attr('selected','selected');
}


function ShowUploadProgress(_upload, _target, _width, _height)
{
  var upload = _upload;
  var inputs = upload.getFileInputs();
  
  var showModal = false;
  
  for (i = 0; i < inputs.length; i++)
  {
    if (inputs[i].value!="")
    {
      showModal = true;
      break;
    }
  }
  
  if (showModal) {
      $('#' + _target).npModal({
      isModal:true, enableDrag:false, width:_width, height:_height	      
    });
  }
}

function ShowUploadProgressWithResize(_upload, _target, _titleClass, _filesTarget, _width, _height)
{
  var upload = _upload;
  var inputs = upload.getFileInputs();
  
  var showModal = false;
  
  for (i = 0; i < inputs.length; i++)
  {
    if (inputs[i].value!="")
    {
      showModal = true;
      break;
    }
  }
  
  if (showModal) 
  {
    var _left = (getWidth() - _width) / 2;
    _left = _left + 'px';
    
    //var _top = (getHeight() - _height) / 2;
    //_top = _top + 'px';

    $('#' + _target).animate({ 
      width: _width, height:_height, left:_left
    }, 1500 );

    $('#' + _filesTarget).hide();
    $('.' + _titleClass).hide();
  }
}

function getHeight() {
  var h = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    h = window.innerHeight;
  } else if( document.documentElement && document.documentElement.clientHeight ) {
    h = document.documentElement.clientHeight;
  } else if( document.body && document.body.clientHeight ) {
    h = document.body.clientHeight;
  }
  return h;
}

function getWidth() {
  var w = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    w = window.innerWidth;
  } else if( document.documentElement && document.documentElement.clientWidth ) {
    w = document.documentElement.clientWidth;
  } else if( document.body && document.body.clientWidth ) {
    w = document.body.clientWidth;
  }
  return w;
}

function getScroll() {
  var s = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    s = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    s = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    s = document.documentElement.scrollTop;
  }
  return s;
}



function tabIt(from, to) {
    if (from.getAttribute && from.value.length == from.getAttribute("maxlength"))
        to.focus()
}

function ShowHideDiv(id) {
    var displayed = document.getElementById(id);
    if (displayed.className == '') displayed.className = 'on';
    else if (displayed.className == 'on') displayed.className = 'off';
    else displayed.className = 'on';
}

function ShowDiv(id) {
    var displayed = document.getElementById(id).style;
    if (displayed.display == '') displayed.display = 'none';
    else if (displayed.display == 'block') displayed.display = 'none';
    else displayed.display = 'block';
}

function HideDiv(id) {
    var displayed = document.getElementById(id).style;
    displayed.display = 'none';
}


/*counter = 0;
function ShowButtonAsProcessing(btn)
{
counter++;
	
btn.value = "...processing";
	
if(counter >= 1) 
{		
alert(counter);
		
if(counter >= 2) 
{ 
alert(counter);
btn.disabled = true;
}	
}
}*/

function ShowButtonAsProcessing(btn) {
    btn.value = "...processing";
    __doPostBack(btn.name, '');
    btn.disabled = true;
}

// Pop up a new window
function popup(url, width, height, scrollbars) {
    var popwidth = width;
    var popheight = height;
    var leftoffset = (screen.width - popwidth - 10) / 2;
    var topoffset = (screen.height - popheight - 40) / 2;
    window.open(url, '_blank', 'titlebar=no,resizable=no,scrollbars=' + scrollbars + ',width=' + popwidth + ',height=' + popheight + ',left=' + leftoffset + ',top=' + topoffset);
}


// Flash detection
var MM_contentVersion = 6;
var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
if (plugin) {
    var words = navigator.plugins["Shockwave Flash"].description.split(" ");
    for (var i = 0; i < words.length; ++i) {
        if (isNaN(parseInt(words[i]))) continue;
        var MM_PluginVersion = words[i];
    }
    var MM_FlashCanPlay = MM_PluginVersion >= MM_contentVersion;
}
else if (navigator.userAgent && navigator.userAgent.indexOf("MSIE") >= 0 && (navigator.appVersion.indexOf("Win") != -1)) {
    document.write('<scr' + 'ipt language=VBScript\> \n'); //FS hide this from IE4.5 Mac by splitting the tag
    document.write('on error resume next \n');
    document.write('MM_FlashCanPlay = ( IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash." & MM_contentVersion)))\n');
    document.write('</scr' + 'ipt\> \n');
}



/** Rotation **/
var width1 //image width1
var height1 //image height1
var seconds1 = 4 //number of seconds1 before next image
var pause1 = seconds1 * 1000

var fadeimages1 = new Array()

var ie41 = document.all
var dom1 = document.getElementById
var curpos1 = 3
//var degree1 = 10
var curcanvasB = "canvasA"
var curimageindex1
var nextimageindex1

function SubpageRotate(paths) {
    fadeimages1 = paths.split(',')
    width1 = '257px'
    height1 = '386px'
    curimageindex1 = Math.floor(Math.random() * fadeimages1.length)
    nextimageindex1 = Math.floor(Math.random() * fadeimages1.length)

    DoRotate()
}


function DoRotate() {
    var preloadedimages1 = new Array()

    for (c = 0; c < fadeimages1.length; c++) {
        preloadedimages1[c] = new Image()
        preloadedimages1[c].src = fadeimages1[c]
    }

    if (ie41 || dom1)
        document.write('<div style="position:relative;width:' + width1 + '; height:' + height1 + '; overflow:hidden;"><div id="canvasA" style="position:absolute; width:' + width1 + '; height:' + height1 + '; top:0; left:0; filter:alpha(opacity=10); -moz-opacity:10"></div><div id="canvasB" style="position:absolute; width:' + width1 + '; height:' + height1 + '; top:0; left:0; filter:alpha(opacity=10); -moz-opacity:10; visibility:hidden"></div></div>')
    else
        document.write('<img name="defaultslide" src="' + fadeimages1[Math.random() * fadeimages1.length - 1] + '">')

    if (ie41 || dom1)
        window.onload = startit2
    else
        setInterval("rotateimage2()", pause1)
}

/*
this method produces the fading effect of the image.
the curpos1 variable is set to the opacity level of the fading.
*/
function fadepic1() {
    if (curpos1 < 100) {
        curpos1 += 3
        if (temp1obj1.filters)
            temp1obj1.filters.alpha.opacity = curpos1
        else if (temp1obj1.style.MozOpacity)
            temp1obj1.style.MozOpacity = curpos1 / 101
    }
    else {
        clearInterval(dropslide1)
        nextcanvas1 = (curcanvasB == "canvasA") ? "canvasA" : "canvasB"
        temp1obj1 = ie41 ? eval("document.all." + nextcanvas1) : document.getElementById(nextcanvas1)
        temp1obj1.innerHTML = insertimage2(nextimageindex1)
        nextimageindex1 = (nextimageindex1 < fadeimages1.length - 1) ? nextimageindex1 + 1 : 0
        var temp1obj12 = ie41 ? eval("document.all." + nextcanvas1) : document.getElementById(nextcanvas1)
        temp1obj12.style.visibility = "hidden"

        setTimeout("rotateimage2()", pause1)
    }
}

function insertimage2(i) {
    var temp1container1 = fadeimages1[i] != "" ? '<img src="' + fadeimages1[i] + '" border="0">' : ""
    return temp1container1
}

function rotateimage2() {
    if (ie41 || dom1) {
        resetit1(curcanvasB)
        var crossobj1 = temp1obj1 = ie41 ? eval("document.all." + curcanvasB) : document.getElementById(curcanvasB)
        crossobj1.style.zIndex++
        temp1obj1.style.visibility = "visible"
        var temp1 = 'setInterval("fadepic1()",50)'
        dropslide1 = eval(temp1)
        curcanvasB = (curcanvasB == "canvasA") ? "canvasB" : "canvasA"
    }
    else
        document.images.defaultslide.src = fadeimages1[curimageindex1]
    curimageindex1 = (curimageindex1 < fadeimages1.length - 1) ? curimageindex1 + Math.floor(Math.random() * fadeimages1.length - 1) : 0
}

/*
this allows for a smoth transition when the new 
image is loaded, so the image doesn't blink.
*/
function resetit1(what1) {
    curpos1 = 4
    var crossobj1 = ie41 ? eval("document.all." + what1) : document.getElementById(what1)
    if (crossobj1.filters)
        crossobj1.filters.alpha.opacity = curpos1
    else if (crossobj1.style.MozOpacity)
        crossobj1.style.MozOpacity = curpos1 / 101
}

function startit2() {
    var crossobj1 = ie41 ? eval("document.all." + curcanvasB) : document.getElementById(curcanvasB)
    crossobj1.innerHTML = insertimage2(curimageindex1)
    rotateimage2()
}


/** Address Scrambler **/
var offset = 7;

function encode(str) {
    var encoded = "";
    for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i) + offset;
        encoded += c;
        if (i < str.length - 1) encoded += ",";
    }
    document.write(encoded);
}

function decode() {
    var str = "";
    for (var i = 0; i < arguments.length; i++) {
        var p = arguments[i] - offset;
        str += String.fromCharCode(p)
    }
    document.write('<a href="mailto:' + str + '">' + str + '</a>');
}

function decodeWithCustomDisplay(email, display) {
    var str = "";
    email = email.split(",");

    for (var i = 0; i < email.length; i++) {
        var p = email[i] - offset;
        str += String.fromCharCode(p)
    }
    document.write('<a href="mailto:' + str + '">' + display + '</a>');
}
