﻿function ToggleDisplay(item, img)
{
    if (document.getElementById(item).style.display=="none")
        show(item,img);
    else
        hide(item,img);                
}
        
function hide(item,img)
{
	var element = document.getElementById(item);
	if (element)
	{
		element.style.display = "none";
		element.style.visibility = "hidden";	
		document.getElementById(img).src = "../images/scrolldown.gif";
	}
}

function show(item,img)
{
	var element = document.getElementById(item);
	if (element)
	{
		element.style.display = "block";
		element.style.visibility = "visible";		
		document.getElementById(img).src = "../images/scrollup.gif";
	}
}

function Copy(from,to) 
{  
    var eFrom = document.getElementById(from);
    var eTo = document.getElementById(to);
    
    eTo.value = eFrom.value; 
}

function TextCount(obj, displayId)
{
    var viewer = document.getElementById(displayId);
    if (obj)
    {
        var showstr = obj.value.length +1;
        if (viewer)
        {
            viewer.innerHTML = showstr;
        }
    }        
}

function TrapKBDEnter(btn, e){
 if (document.all){
  if (e.keyCode == 13){
   e.returnValue=false;
   e.cancel = true;
   btn.click();
  }
 }
 else if (document.getElementById){
  if (e.which == 13){
   e.returnValue=false;
   e.cancel = true;
   btn.click();
  }
 }
 else if(document.layers){
  if(event.which == 13){
   e.returnValue=false;
   e.cancel = true;
   btn.click();
  }
 }
}

function isNumeric(elem){
	var numericExpression = /^[0-9]+$/;
	if(elem.match(numericExpression)){
		return true;
	}
	return false;
}

function numKeyPress(cur, e)
{
 if(e.keyCode < 47 || e.keyCode > 58){
  e.returnValue = false;
 }
}

function numKeyDown(cur, prev, key)
{
 if(key == 8){
  if(cur.value.length < 1){
   prev.focus();
   if (prev.createTextRange)
   {
    var fieldRange = prev.createTextRange();
    fieldRange.moveStart('character', prev.value.length);
    fieldRange.collapse();
    fieldRange.select();
   }
  }
 }
}

function numKeyUp(cur, fut, max)
{
 if(cur.value.length >= max){
  fut.focus();
 }
}

 //This function works for IE and Firefox but there is no grantee for the other browsers
    function NumericOnlyMask(e)
    {
	    if (e.keyCode && (e.keyCode < 48 || e.keyCode > 57))
	    { 
	        e.keyCode=0;
	    }
	    else if (e.which && (e.which < 48 || e.which > 57))
	    { 
	        e.preventDefault();
	    }
	}
	
	//This function works for IE and Firefox but there is no grantee for the other browsers
    function AlphaNumericMask(e)
    {
	    if (e.keyCode && (e.keyCode < 48 || (e.keyCode > 57 && e.keyCode < 65) 
	        || (e.keyCode > 90 && e.keyCode < 97) || e.keyCode >122))
	    { 
	        e.keyCode=0;
	    }
	    else if (e.which && (e.which < 48 || (e.which > 57 && e.which < 65) 
	            || (e.which > 90 && e.which < 97) || e.which >122))
	    { 
	        e.preventDefault();
	    }
	}
	
function preDelete() 
{
    return confirm('Are you sure?');
}

function Picker_OnSelectionChanged(picker)
{
  picker.AssociatedCalendar.SetSelectedDate(picker.GetSelectedDate());
}

function Calendar_OnSelectionChanged(calendar)
{
  calendar.AssociatedPicker.SetSelectedDate(calendar.GetSelectedDate());
}

function Button_OnClick(alignElement, calendar)
{
  if (calendar.PopUpObjectShowing)
  {
    calendar.Hide();
  }
  else
  {
    calendar.SetSelectedDate(calendar.AssociatedPicker.GetSelectedDate());
    calendar.Show(alignElement);
  }
}

function GetQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    
    for (var i=0;i<vars.length;i++)
    {
        var pair = vars[i].split("=");
        if (pair[0] == variable)
        {
            return pair[1];
        }
    }
}

/* CategoryMenu Specific Javascript
-----------------------------------------------------------------------*/
function NewCategoryAlert()
{
  alert('Changes to this "New Category" will be lost if you do not click the "Save" button below.\r\rPublishing will become available once you click the "Save Button".');
}

function newRoot(tvTarget, newNodeID)
{
  tvTarget.beginUpdate();
  var newNode = new ComponentArt.Web.UI.TreeViewNode(); 
  newNode.set_text('New Category'); 
  newNode.set_imageUrl('folders.gif');
  newNode.set_id(newNodeID);
  tvTarget.get_nodes().add(newNode);
  tvTarget.endUpdate();
  NewCategoryAlert();
  tvTarget.selectNodeById(newNodeID);
}

function newSubNode(tvTarget, newNodeID, parentNode)
{
  tvTarget.beginUpdate();
  var newNode = new ComponentArt.Web.UI.TreeViewNode(); 
  newNode.set_text('New Category'); 
  newNode.set_imageUrl('folders.gif');
  newNode.set_id(newNodeID);
  parentNode.get_nodes().add(newNode);
  tvTarget.endUpdate();
  tvTarget.selectNodeById(newNodeID);
  NewCategoryAlert();
}


/* Credit card validation
-----------------------------------------------------------------------*/
function mod10( cardNumber ) 
{ // LUHN Formula for validation of credit card numbers.
  var ar = new Array( cardNumber.length );
  var i = 0,sum = 0;

  for( i = 0; i < cardNumber.length; ++i )
  {
    ar[i] = parseInt(cardNumber.charAt(i));
  }
  for( i = ar.length -2; i >= 0; i-=2 ) 
  {                           // you have to start from the right, and work back.
    ar[i] *= 2;               // every second digit starting with the right most (check digit)

    if( ar[i] > 9 ) ar[i]-=9;   // will be doubled, and summed with the skipped digits.
  }          // if the double digit is > 9, ADD those individual digits together 

  for( i = 0; i < ar.length; ++i ) 
  {
    sum += ar[i]; // if the sum is divisible by 10 mod10 succeeds
  }
  return (((sum%10)==0)?true:false);                   
}

function validateCard(cardNumber,cardType) 
{
  var length = cardNumber.length;  

  switch( cardType ) 
  {
        case 'Amex':
          if( length != 15 ) 
          {
            alert("Please enter a valid American Express Card number.");
            return false;
          }

          var prefix = parseInt( cardNumber.substring(0,2));
          if( prefix != 34 && prefix != 37 )
          {
            alert("Please enter a valid American Express Card number.");
            return false;
          }
		    break;
        case 'Discover':
          if( length != 16 ) 
          {
            alert("Please enter a valid Discover Card number.");
            return false;
          }
          var prefix = parseInt( cardNumber.substring(0,4));
          if( prefix != 6011 ) 
          {
            alert("Please enter a valid Discover Card number.");
            return false;
          }
          break;
        case 'Master':
          if( length != 16 ) 
          {
            alert("Please enter a valid MasterCard number.");
            return false;
          }
          var prefix = parseInt( cardNumber.substring(0,2));
          if( prefix < 51 || prefix > 55) 
          {
            alert("Please enter a valid MasterCard Card number.");
            return false;
          }
          break;
          case 'Visa':
            if( length != 16 && length != 13 ) 
            {
              alert("Please enter a valid Visa Card number.");
              return false;
            }
            var prefix = parseInt( cardNumber.substring(0,1));
            if( prefix != 4 ) 
            {
              alert("Please enter a valid Visa Card number.");
              return false;
            }
          break;
      }
      if( !mod10( cardNumber ) ) 
      { // run the check digit algorithm
        alert("Sorry! this is not a valid credit card number.");
        return false;
      }
  return true; // at this point card has not been proven to be invalid
  
}
/*--------------------------------------------------------------------*/

function WaitForCallback(ctrl, wait, js)// ctrl - CallBack.ClientID, wait - milliseconds, js - function to call
{
   if (ctrl.CallbackInProgress)
   {
    //j = 
    setTimeout('WaitForCallback('+ctrl.get_id()+','+wait+',\''+js+'\')', wait); // polling if callback is in progress
   }
   else
    eval(js);
}

/*CA Grid Helpers
/*---------------------------------------------------------------------*/
function setGridCheck( grid, columnNumber, checked ) //checks all boxes in a column
{ 
var gridItem; 
var itemIndex = 0; 

while(gridItem = grid.Table.GetRow(itemIndex)) 
{ 
gridItem.SetValue( columnNumber, checked ); 
itemIndex++; 
}//end while 

grid.Render(); 
} 
function getGridCheck( grid, idCol, checkboxCol ) //gets a comma sep list of all IDs from rows w/checked boxes
{//grid = ClientObjectID of the Grid to search
 //idCol = index of the ID Column of the table
 //checkboxCol = index of the column with the checkboxes
 var gridItem; 
 var itemIndex = 0;
 var checkedIDs = ''; 

 while(gridItem = grid.Table.GetRow(itemIndex)) 
 {
  if(gridItem.getMember(checkboxCol).get_value())
  { 
   if(checkedIDs.length > 0)
   {
    checkedIDs += ',';
   }
   checkedIDs += gridItem.getMember(idCol).get_value();
  }
  itemIndex++; 
 }
 return checkedIDs 
} 

/* Timeout funtion and settings */
// global variables
var timeoutValue= 3600000; var timeoutWarning=3000000;
var timeoutLogoffId;var timeoutId=null; 
var timeoutWarningWindow;

function setWindowTimeOut(functString,timeOutVal)
{
    clearWindowTimeOut();
    timeoutId=setTimeout(functString,timeOutVal);
}

function clearWindowTimeOut()
{
    if(timeoutId !=null)
    {
        clearTimeout(timeoutId);
        timeoutId=null;
    }
}

function showTimeOutWarning()
{
    setWindowTimeOut('timeOutLogoff()',timeoutWarning);
    alert("Your session will time out in " + (timeoutValue - timeoutWarning)/60000 + " minutes. Click OK to continue your current session.");
}

/* Mover scripts
***********************************************/
function moveOption(srcList, targetList)
{
 var len = srcList.options.length;
 if(srcList.selectedIndex >= 0)
 {
  for (var i = len - 1; i > -1; i--) 
  {
   if (srcList.options[i].selected) 
   {
    addOption(targetList,srcList.options[i].text,srcList.options[i].value);
    deleteOption(srcList,i);
   }
  }
 }
}
    
function addOption(cntrl,optionText,optionValue) 
{
 if(isExist(cntrl, optionValue) == 0)  // check if option is already added to the target
 {
  var _option = new Option(optionText,optionValue);
  var optionRank = cntrl.options.length;
  cntrl.options[optionRank] = _option;
 }
}

function deleteOption(cntrl,optionRank) 
{
 if (cntrl.options.length > 0) 
  cntrl.options[optionRank] = null;
}

function isExist(cntrl, value)
{
 var Exist = 0;
 for (var i = 0; i < cntrl.options.length; i++)
  if (cntrl.options[i].value == value)
   Exist = 1;

 return Exist;
}

function GetOptionSelectedValue(cntrl)
{
    if(cntrl.selectedIndex >= 0)
    {
        for(var i = 0; i < cntrl.options.length; i++)
        {
            if (cntrl.options[i].selected) 
            {
                return cntrl.options[i].value; 
            }
        }
    }
    return -1;
}
function GetOptionSelectedText(cntrl)
{
    if(cntrl.selectedIndex >= 0)
    {
        for(var i = 0; i < cntrl.options.length; i++)
        {
            if (cntrl.options[i].selected) 
            {
                return cntrl.options[i].text; 
            }
        }
    }
    return -1;
}
    
/* End Mover Scripts
************************************************************/

function safeText(unsafe)
{
    var safe;
    safe = unsafe.replace("<", "&gt;");
    return safe;
}

/* Popup helper */
var g_childWindow = null;
function OpenChildWindow(childURL, childName, childFeatures){
	
	if (!FindChildWindow(childName))
		g_childWindow = window.open(childURL, childName, childFeatures);

	g_childWindow.focus();
	g_childWindow.document.location.href=childURL;	

}

function CloseChildWindow(){
	if (g_childWindow != null){
		if (!g_childWindow.closed)
			g_childWindow.close();
		g_childWindow = null;
	}
}

function FindChildWindow(childName){
	if ((g_childWindow != null) && (g_childWindow.closed != true)){
		g_childWindow = window.open("", childName);
		return true;
	}
	else
		return false;
}
