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 SetAddress(enabled, controlPrefix, isMandatory)
{
	var controlName = "document.forms[0]." + controlPrefix + "_adrAddress_";

	// Enable/disable the validators if this control is mandatory
	if (isIE) 
	{
		if (isMandatory)
		{
			eval('ValidatorEnable(document.getElementById("' + controlPrefix + '_adrAddress_addressLine1Validator"), ' + !enabled + ');');
			eval('ValidatorEnable(document.getElementById("' + controlPrefix + '_adrAddress_cityValidator"), ' + !enabled + ');');
			eval('ValidatorEnable(document.getElementById("' + controlPrefix + '_adrAddress_postalCodeValidator"), ' + !enabled + ');');
		}
	}	
	eval(controlName + 'addressLine1.disabled = ' + enabled);
	eval(controlName + 'addressLine2.disabled = ' + enabled);
	eval(controlName + 'addressLine3.disabled = ' + enabled);
	eval(controlName + 'txtcity.disabled = ' + enabled);
	eval(controlName + 'ddState.disabled = ' + enabled);
	eval(controlName + 'txtpostalCode.disabled = ' + enabled);
	eval(controlName + 'ddCountry.disabled = ' + enabled);
	if(eval(controlName + 'IsWithinCityLimits') != null)
		eval(controlName + 'IsWithinCityLimits.disabled = ' + enabled);
	if(eval(controlName + 'tbCounty') != null)
		eval(controlName + 'tbCounty.disabled = ' + enabled);
}

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 getWindowHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getWindowWidth() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}

