﻿      function keyCode2Char(keyCode, shift){
            /*
            this function converts a keycode to a correct character.
            the function can deal with shiftkeys aswell, but caps-lock state will be ignored, since the only way (I know of) to check caps-lock state is in a keypress event, and I write this for use in keydown/keyup events.
            */      
            var myChar
            myChar = shift ? shiftChars[keyCode] : myChar = chars[keyCode]
            myChar= (typeof myChar== 'undefined') ? '' : myChar
            return myChar
      }
      
      var chars = new Array
            chars[32]=' '
            chars[48]='0'
            chars[49]='1'
            chars[50]='2'
            chars[51]='3'
            chars[52]='4'
            chars[53]='5'
            chars[54]='6'
            chars[55]='7'
            chars[56]='8'
            chars[57]='9'
            chars[65]='a'
            chars[66]='b'
            chars[67]='c'
            chars[68]='d'
            chars[69]='e'
            chars[70]='f'
            chars[71]='g'
            chars[72]='h'
            chars[73]='i'
            chars[74]='j'
            chars[75]='k'
            chars[76]='l'
            chars[77]='m'
            chars[78]='n'
            chars[79]='o'
            chars[80]='p'
            chars[81]='q'
            chars[82]='r'
            chars[83]='s'
            chars[84]='t'
            chars[85]='u'
            chars[86]='v'
            chars[87]='w'
            chars[88]='x'
            chars[89]='y'
            chars[90]='z'
            chars[96]='0'
            chars[97]='1'
            chars[98]='2'
            chars[99]='3'
            chars[100]='4'
            chars[101]='5'
            chars[102]='6'
            chars[103]='7'
            chars[104]='8'
            chars[105]='9'
            chars[106]='*'
            chars[107]='+'
            chars[109]='-'
            chars[110]=','
            chars[111]='/'
            chars[186]=';'
            chars[187]='='
            chars[188]=','
            chars[189]='-'
            chars[190]='.'
            chars[191]='/'
            chars[192]='`'
            chars[219]='['
            chars[220]='\\'
            chars[221]=']'
            chars[222]='\''

      var shiftChars = new Array
            shiftChars[32]=' '
            shiftChars[48]=')'
            shiftChars[49]='!'
            shiftChars[50]='@'
            shiftChars[51]='#'
            shiftChars[52]='$'
            shiftChars[53]='%'
            shiftChars[54]='^'
            shiftChars[55]='&'
            shiftChars[56]='*'
            shiftChars[57]='('
            shiftChars[65]='A'
            shiftChars[66]='B'
            shiftChars[67]='C'
            shiftChars[68]='D'
            shiftChars[69]='E'
            shiftChars[70]='F'
            shiftChars[71]='G'
            shiftChars[72]='H'
            shiftChars[73]='I'
            shiftChars[74]='J'
            shiftChars[75]='K'
            shiftChars[76]='L'
            shiftChars[77]='M'
            shiftChars[78]='N'
            shiftChars[79]='O'
            shiftChars[80]='P'
            shiftChars[81]='Q'
            shiftChars[82]='R'
            shiftChars[83]='S'
            shiftChars[84]='T'
            shiftChars[85]='U'
            shiftChars[86]='V'
            shiftChars[87]='W'
            shiftChars[88]='X'
            shiftChars[89]='Y'
            shiftChars[90]='Z'
            shiftChars[96]='0'
            shiftChars[97]='1'
            shiftChars[98]='2'
            shiftChars[99]='3'
            shiftChars[100]='4'
            shiftChars[101]='5'
            shiftChars[102]='6'
            shiftChars[103]='7'
            shiftChars[104]='8'
            shiftChars[105]='9'
            shiftChars[106]='*'
            shiftChars[107]='+'
            shiftChars[109]='-'
            shiftChars[110]=','
            shiftChars[111]='/'
            shiftChars[186]=':'
            shiftChars[187]='+'
            shiftChars[188]='<'
            shiftChars[189]='_'
            shiftChars[190]='>'
            shiftChars[191]='?'
            shiftChars[192]='~'
            shiftChars[219]='{'
            shiftChars[220]='|'
            shiftChars[221]='}'
            shiftChars[222]='"'




	var strUserAgent = navigator.userAgent.toLowerCase(); 
	var isIE = strUserAgent.indexOf("msie") > -1; 
	var controlChars = /[\x00\x08\x09\x0D]/; //Mozilla tries to apply rules to invisible characters like delete, insert, home, etc. So test for these.
	var oldStyles = new Array(1);
	var valOwners = new Array(1);

	function checkEnteredValue(regexFullMatchPattern, valMessage, compulsory, compMessage, elementID, valElementsID, retVal)
	{
		var Valrow = "row_Val_" + valElementsID;
		var Vallabel = "lab_Val_" + valElementsID;
		var Valcomp = "comp_" + valElementsID;
		var fullString = "" + document.getElementById(elementID).value;
		var rowexists = document.getElementById(Valrow) != null;
		var valid = true;
		var canChangeValidation = (valOwners[Valrow] == null || valOwners[Valrow] == elementID);

		if(fullString.length == 0)
		{
			if(compulsory)
			{
				valid = false;
				if(canChangeValidation){
					document.getElementById(Valcomp).innerHTML = "** ";
					if(oldStyles[valElementsID] == null)
						oldStyles[valElementsID] = document.getElementById(Valcomp).parentNode.style.fontWeight
					document.getElementById(Valcomp).parentNode.style.fontWeight = "bold";
					if(rowexists)
					{
						document.getElementById(Valrow).style.display = "";
						if(compMessage != "")
							document.getElementById(Vallabel).innerHTML = compMessage;
					}
					valOwners[Valrow] = elementID;
				}
			} else {
				if(canChangeValidation){
					if(rowexists)
						document.getElementById(Valrow).style.display = "none";
					if(oldStyles[valElementsID] != null)
							document.getElementById(Valcomp).parentNode.style.fontWeight = oldStyles[valElementsID];
					if(document.getElementById(Valcomp).innerHTML != "")
							document.getElementById(Valcomp).innerHTML = "";
					valOwners[Valrow] = null;
				}
			}
		} else {
			if( regexFullMatchPattern != "")
			{	
				var regEx = new RegExp( regexFullMatchPattern );
				var matched = fullString.match( regEx );
				if(!matched){
					valid = false;
					if(canChangeValidation){
						document.getElementById(Valcomp).innerHTML = "** ";
						if(oldStyles[valElementsID] == null)
							oldStyles[valElementsID] = document.getElementById(Valcomp).parentNode.style.fontWeight
						document.getElementById(Valcomp).parentNode.style.fontWeight = "bold";
						if(rowexists && valMessage != ""){
							document.getElementById(Valrow).style.display = "";
							document.getElementById(Vallabel).innerHTML = valMessage;
						}
						valOwners[Valrow] = elementID;
					}
				} else {
					if(canChangeValidation){
						if(compulsory)
							document.getElementById(Valcomp).innerHTML = "* ";
						else if(document.getElementById(Valcomp).innerHTML != "")
							document.getElementById(Valcomp).innerHTML = "";
						if(oldStyles[valElementsID] != null)
							document.getElementById(Valcomp).parentNode.style.fontWeight = oldStyles[valElementsID];
						if(rowexists)
							document.getElementById(Valrow).style.display = "none";
						valOwners[Valrow] = null;
					}
				}
			} else {
				if(canChangeValidation){
					if(compulsory)
						document.getElementById(Valcomp).innerHTML = "* ";
					else if(document.getElementById(Valcomp).innerHTML != "")
						document.getElementById(Valcomp).innerHTML = "";
					if(oldStyles[valElementsID] != null)
						document.getElementById(Valcomp).parentNode.style.fontWeight = oldStyles[valElementsID];
					if(rowexists)
						document.getElementById(Valrow).style.display = "none";
					valOwners[Valrow] = null;
				}
			}
		}
		if(retVal)
			return valid;
	}
	
	function checkList(compMessage, elementID, valElementsID, listType, retVal)
	{
		var Valrow = "row_Val_" + valElementsID;
		var Vallabel = "lab_Val_" + valElementsID;
		var Valcomp = "comp_" + valElementsID;
		var group = elementID + "$" + elementID;
		var valid = false;
		var canChangeValidation = (valOwners[Valrow] == null || valOwners[Valrow] == elementID);
		
		if(listType == 0)
		{
			var list = document.getElementsByName(group);
			for(el = 0; el < list.length; el++)
			{
				if(list[el].checked)
				{
					valid = true;
					break;
				}
			}
		} else {
			var item = 1;
			var itemID = elementID + "_" + valElementsID + "ctl";
			var el = document.getElementById(itemID + item);
			while(el != null)
			{
				if(el.checked)
				{
					valid = true;
					break;
				}
				item += 1;
				el = document.getElementById(itemID + item);
			}
		}
		if(valid && canChangeValidation)
		{
			document.getElementById(Valcomp).innerHTML = "* ";
			if(oldStyles[valElementsID] != null)
				document.getElementById(Valcomp).parentNode.style.fontWeight = oldStyles[valElementsID];
			if(document.getElementById(Valrow) != null)
				document.getElementById(Valrow).style.display = "none";
			valOwners[Valrow] = null;
		} else if(canChangeValidation){
			document.getElementById(Valcomp).innerHTML = "** ";
			if(oldStyles[valElementsID] == null)
				oldStyles[valElementsID] = document.getElementById(Valcomp).parentNode.style.fontWeight
			document.getElementById(Valcomp).parentNode.style.fontWeight = "bold";
			
			if(document.getElementById(Valrow) != null)
			{
				document.getElementById(Valrow).style.display = "";
				if(compMessage != "")
					document.getElementById(Vallabel).innerHTML = compMessage;
			}
			valOwners[Valrow] = elementID;
		}
		if(retVal)
			return valid;
	}
	
	function checkDropDown(compMessage, elementID, valElementsID, retVal)
	{
		var Valrow = "row_Val_" + valElementsID;
		var Vallabel = "lab_Val_" + valElementsID;
		var Valcomp = "comp_" + valElementsID;
		var valid = true;
		var canChangeValidation = (valOwners[Valrow] == null || valOwners[Valrow] == elementID);
		
		var list = document.getElementById(elementID);
		var opt = list.options[list.selectedIndex];
		if(list.selectedIndex == 0 && (opt.text.toLowerCase() == "please select" || opt.text.toLowerCase() == "select" || opt.text.toLowerCase() == "-"))
		{
			valid = false;
			if(canChangeValidation){
				document.getElementById(Valcomp).innerHTML = "** ";
				if(oldStyles[valElementsID] == null)
					oldStyles[valElementsID] = document.getElementById(Valcomp).parentNode.style.fontWeight
				document.getElementById(Valcomp).parentNode.style.fontWeight = "bold";
				if(document.getElementById(Valrow) != null)
				{
					document.getElementById(Valrow).style.display = "";
					if(compMessage != "")
						document.getElementById(Vallabel).innerHTML = compMessage;
				}
				valOwners[Valrow] = elementID;
			}
		}else{
			if(canChangeValidation){
				document.getElementById(Valcomp).innerHTML = "* ";
				if(oldStyles[valElementsID] != null)
					document.getElementById(Valcomp).parentNode.style.fontWeight = oldStyles[valElementsID];
				if(document.getElementById(Valrow) != null)
					document.getElementById(Valrow).style.display = "none";
				valOwners[Valrow] = null;
			}
		}
		if(retVal)
			return valid;
	}

	function checkEntryValue(ev, regexCharMatchPattern, charLimit, elementID)
	{
		if ( !ev ) var ev = window.event;
		var asciiCode;

		if ( isIE )
			asciiCode = ev.keyCode;
		else
			asciiCode = ev.which;
		
		var asciiLetter = keyCode2Char(asciiCode,ev.shiftKey); //we use this function because the checkEntryValue function is assigned to onkeydown, which uses the keyboard scan codes and NOT the ascii codes.
		if ( asciiLetter != "" )
		{
			if(!controlChars.test(asciiLetter))
			{
				
				if ( regexCharMatchPattern != "")
				{

					var regEx = new RegExp( regexCharMatchPattern );
					var matched = asciiLetter.match( regEx );
					if ( !matched )
					{
						return false;
					}
				}
				if( charLimit != 0)
				{
					var fullString = "" + document.getElementById(elementID).value + asciiLetter;
					if(fullString.length > charLimit)
					{
						return false;
					}
				}
			}
		}
	}
	
	function trim(str)
	{
		return str.replace(/^\s+|\s+$/g,"");
	}
	
	function StringEndsWith(string,endsWith)
	{
	  var idx = string.lastIndexOf(endsWith);
	  return ((idx != -1) && ((idx + endsWith.length) == string.length));
	}

  var didPiece = new Array(false,false,false);	
	function SanitizeAddressLines(Lines, Components, Part){
	  var Piece = Components[Part];
	  
	  for(i = Lines.length-1; i >= 0; i--)
	  {
	    var Line = trim(Lines[i]);
	    if(Piece != null && Piece != "" && StringEndsWith(Line,Piece) && !didPiece[Part])
	    {
	      var idx = Line.lastIndexOf(Piece);
	      Line = Line.substring(0,idx);
	      Line = trim(Line);
	      didPiece[Part] = true
	    }
	    Lines[i] = Line
	  }
	  
	  return Lines
	}
	
	var __nonMSBrowser = (window.navigator.appName.toLowerCase().indexOf('explorer') == -1);
	function FireDefaultButton(event, target) {
	  var srcEl = event.srcElement? event.srcElement : event.target;
    if (event.keyCode == 13 && !(srcEl && (srcEl.tagName.toLowerCase() == "textarea" || srcEl.tagName.toLowerCase() == "select"))) {
        var defaultButton;
        if (__nonMSBrowser) {
            defaultButton = document.getElementById(target);
        }
        else {
            defaultButton = document.all[target];
        }
        if (defaultButton && typeof(defaultButton.click) != "undefined") {
            defaultButton.click();
            event.cancelBubble = true;
            if (event.stopPropagation) event.stopPropagation();
            return false;
        }
    }
    return true;
}
