var isIE = document.all ? true : false;
var isNS = document.layers ? true : false;
// initialization stuff
//

if (isNS) {
		document.captureEvents(Event.KEYPRESS);
}

	
function MoveListItem(targetBox, moveUp)
{
	// Exit if there isn't a selected item
	if (targetBox.selectedIndex == -1)
	{
		return false;
	}
	
	// Save the selected item for the later swap
	targetIndex = targetBox.selectedIndex;
	selText = targetBox.options[targetBox.selectedIndex].text;
	selValue = targetBox.options[targetBox.selectedIndex].value;
	
	if (moveUp)
	{
		targetIndex--;
	}
	else
	{
		targetIndex++;
	}
	
	// Make sure that the spot to swap to is within the list box boundaries
	if (targetIndex == -1 || targetIndex == targetBox.options.length)
	{
		return false;
	}
	
	// Swap the options
	targetBox.options[targetBox.selectedIndex].text = targetBox.options[targetIndex].text;
	targetBox.options[targetBox.selectedIndex].value = targetBox.options[targetIndex].value;

	targetBox.options[targetIndex].text = selText;
	targetBox.options[targetIndex].value = selValue;
	
	// Select the swapped option
	targetBox.options[targetIndex].selected = true;
	
	return false;
}

function ToggleImage(image, contentPaneName, moduleHiddenField, contentCellName, contentRowName)
{
	contentPane = document.getElementById(contentPaneName);
	moduleState = eval('document.forms[0].' + moduleHiddenField);
	
	contentCell = document.getElementById(contentCellName);
	contentRow = document.getElementById(contentRowName);
	
	if (image.src.toLowerCase().indexOf("plus.gif") != -1)
	{
		image.src = image.src.toLowerCase().replace("plus.gif", "minus.gif");
		contentPane.style.display = "";
		contentRow.style.display = "";
		//contentCell.className = "ModuleContents";
		moduleState.value = "true";
	}
	else
	{
		image.src = image.src.toLowerCase().replace("minus.gif", "plus.gif");
		contentPane.style.display = "none";
		contentRow.style.display = "none";
		//contentCell.className = "ModuleHeader";
		moduleState.value = "false";
	}
}

function CheckMove(targetBox, moveUp)
{
	// Exit if there isn't a selected item
	if (targetBox.selectedIndex == -1)
	{
		return false;
	}
	
	targetIndex = targetBox.selectedIndex;
	
	if (moveUp)
	{
		targetIndex--;
	}
	else
	{
		targetIndex++;
	}
	
	// Make sure that the spot to swap to is within the list box boundaries
	if (targetIndex == -1 || targetIndex == targetBox.options.length)
	{
		return false;
	}
	
	return true;
}

function AddGridRow(gridName)
{
	//var grid = igtbl_getGridById(gridName);
	igtbl_addNew(gridName, 0);
}

function DeleteGridRow(gridName)
{
	igtbl_deleteSelRows(gridName);
}

function DeleteAllRows(gridName)
{
	var grid = igtbl_getGridById(gridName);
	
	while (grid.Rows.length != 0)
	{
		grid.Rows.remove(0);
	}
}

/* Spell Checker Interfaces */
function RSCustomInterface(tbElementName)
{
	this.tbName = tbElementName;
	this.getText = getText;
	this.setText = setText;
}

function getText()
{
	return eval('document.forms[0].' + this.tbName + '.value');
}

function setText(text)
{
	eval('document.forms[0].' + this.tbName + '.value = text');
	SetHTML(eval("'" + this.tbName + "Editor" + "'"));
}

function SetHTML(TBP)
{
	shrtTBP = TBP.substring(0, TBP.length -6 );
	objIFrame = eval(TBP);
	if (document.getElementById(shrtTBP + 'chk').checked == true)
	{
		objIFrame.document.body.innerText = document.getElementById(shrtTBP).value;
	}
	else
	{
		objIFrame.document.body.innerHTML = document.getElementById(shrtTBP).value;
	}
}
/* End Spell Checker Functions */

/* Function to select/unselect checkbox */
function ToggleCheckEmail(objCheck)
{
     var frm = document.forms[0];
     for (i=0; i<frm.length; i++)
     {
        if (frm.elements[i].type == "checkbox")
        {
            frm.elements[i].checked = objCheck.checked;
        }
     }
}

// Sends Email to selected consultants. gridName is Infragistic webgrid
function SendEmail(gridName,method)
{
	var myGrid = igtbl_getGridById(gridName);
	var emailAddress = "";
	for (i = 0; i < myGrid.Rows.length; i++)
	{
	if(myGrid.Rows.getRow(i).getCellFromKey("ShowEmail").getValue() == true)
	{
		if(myGrid.Rows.getRow(i).getCellFromKey("Email").getValue() != "" && myGrid.Rows.getRow(i).getCellFromKey("Email").getValue() != null)
		{
			emailAddress = emailAddress + myGrid.Rows.getRow(i).getCellFromKey("Email").getValue() + ";";
		}
	}	  
	}
	// if the method is mailto then use it, otherwise use the prowess email editor
	if (method==10)
	{
		window.open("mailto:" + emailAddress);
	}
	else
	{
		EmailEditor(emailAddress, '', '');
	}
}

// Textbox accepts only numbers
function OnlyDigits(e, objText, isInt)
{
		var ret = true;
		var whichCode = (window.event) ? window.event.keyCode : e.which;

		var key = String.fromCharCode(whichCode);  // Get key value from key code 
		var limit;
		
		// Check if Integer, else allow decimals
		if(isInt == true)
			limit = 47
		else
			limit = 46		
		
		if (window.event) {
				if (whichCode < limit || whichCode > 57)
				{
						window.event.keyCode = 0;
						ret = false;
				}
		}
		else if (isNS) {
				if (whichCode < limit || whichCode > 57 ) 
				{
						e.which = 0;
						ret = false;
				}
		}
		else
		{
			//always allow tab, backspace, and delete
			if(whichCode==9 || whichCode==8 || whichCode==0)
			{
				return ret;
			}
			//ignore keys like delete, backspace, and everything that isn't a number
			if (whichCode < limit || whichCode > 57 )
			{
				ret = false;
			}
		}
		
		if(objText.indexOf('.') >= 0)
		{
			if(key == '.' || objText.indexOf('.') == objText.length - 3)
			{
				if(window.event)
				{
					window.event.keyCode = 0;
				}
				ret = false;//The string already contains a decimal point one more should not be allowed
			}			
		}
		return (ret);
}


function calcHeight(controlname)
{
	
	
  //find the height of the internal page
  var the_height=
    document.getElementById(controlname).contentWindow.document.body.scrollHeight;

  //change the height of the iframe
  document.getElementById(controlname).height=the_height;
}


function CCValidation(cardNumber, cardType)
{
  var isValid = false;
  var ccCheckRegExp = /[^\d ]/;
  isValid = !ccCheckRegExp.test(cardNumber);

  if (isValid)
  {
    var cardNumbersOnly = cardNumber.replace(/ /g,"");
    var cardNumberLength = cardNumbersOnly.length;
    var lengthIsValid = false;
    var prefixIsValid = false;
    var prefixRegExp;

    switch(cardType)
    {
	  case "MASTERCARD":
      case "MC":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^5[1-5]/;
        break;

      case "VISA":
        lengthIsValid = (cardNumberLength == 16 || cardNumberLength == 13);
        prefixRegExp = /^4/;
        break;

      case "AMEX":
      case "AMERICANEXPRESS":
        lengthIsValid = (cardNumberLength == 15);
        prefixRegExp = /^3(4|7)/;
        break;
        
      case "DISCOVER":
      case "DISC":
        lengthIsValid = (cardNumberLength == 16);
        prefixRegExp = /^6/;
        break;

      default:
        return true;
    }

    prefixIsValid = prefixRegExp.test(cardNumbersOnly);
    isValid = prefixIsValid && lengthIsValid;
  }

  if (isValid)
  {
    var numberProduct;
    var numberProductDigitIndex;
    var checkSumTotal = 0;

    for (digitCounter = cardNumberLength - 1; 
      digitCounter >= 0; 
      digitCounter--)
    {
      checkSumTotal += parseInt (cardNumbersOnly.charAt(digitCounter));
      digitCounter--;
      numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
      for (var productDigitCounter = 0;
        productDigitCounter < numberProduct.length; 
        productDigitCounter++)
      {
        checkSumTotal += 
          parseInt(numberProduct.charAt(productDigitCounter));
      }
    }

    isValid = (checkSumTotal % 10 == 0);
  }

  return isValid;
}

function querySt(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 RecordFees(portalGuid, userGuid, feeGuid, secureUrl) {
    var queryParams = "?portal=" + portalGuid + "&user=" + userGuid + "&GenFeeTypeGuid=" + feeGuid;
    var winTop = (screen.height - 700) / 2;
    var winLeft = (screen.width - 950) / 2;
    var winParams = "modal,width=950,height=700,scrollbars=yes,resizable=yes,top=" + winTop + ",left=" + winLeft;
    xWindow = window.open(secureUrl + "/desktopmodules/fees/fee.aspx" + queryParams, "FeesEditor", winParams);
    if (window.focus) {
        xWindow.opener = self;
        xWindow.window.focus();
    }
}
