function JavaScriptFramework()
{
	// Properties
	this.IsIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
	this.IsMozilla = navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
	
	// Functions
	this.fnGetElementById = JSFW_fnGetElementById;
	this.fnValidateForm = JSFW_fnValidateForm;
	this.fnAlert = JSFW_fnAlert;	
	this.fnHideElement = JSFW_fnHideElement;
	this.fnShowElement = JSFW_fnShowElement;
	this.fnTrim = JSFW_fnTrim;
	this.fnRTrim = JSFW_fnRTrim;
	this.fnLTrim = JSFW_fnLTrim;
	this.fnSubmitForm = JSFW_fnSubmitForm;
	this.fnProcessSearchText = JSFW_fnProcessSearchText;
	this.fnCheckPasswordForm = JSFW_fnCheckPasswordForm;
	this.fnPrintDivContent = JSFW_fnPrintDivContent;
	this.fnGetElementsByName = JSFW_fnGetElementsByName;
	this.fnValidateDownload = JSFW_fnValidateDownload;
	this.fnDownloadSelectedDocuments = JSFW_fnDownloadSelectedDocuments;
	this.fnDownloadAllDocuments = JSFW_fnDownloadAllDocuments;
	this.fnValidateOnKeyPress = JSFW_fnValidateOnKeyPress;
	this.fnValidateEmail = JSFW_fnValidateEmail;
	this.fnGetFormByElement = JSFW_fnGetFormByElement;
	this.fnCheckInterestProfileNode = JSFW_fnCheckInterestProfileNode;
	this.fnCheckUncheckParentNode = JSFW_fnCheckUncheckParentNode;
	this.fnValidateMultipleEmailAddress = JSFW_fnValidateMultipleEmailAddress;
	
	// fnAlert : This function is used to alert message
	function JSFW_fnAlert(strMessage)
	{
		alert(strMessage);
	}
	
	// fnGetElementById : This function is used to get Element by provided id
	function JSFW_fnGetElementById(strId)
	{
		try{		
			return document.getElementById(strId);
		}catch(ex)
		{
			fnHandleException(ex,'JSFW_fnGetElementById');
		}
	}

	
	// fnGetElementById : This function is used to get Element by provided id
	function JSFW_fnGetElementsByName(strName)
	{
		try{		
			return document.getElementsByName(strName);
		}catch(ex)
		{
			fnHandleException(ex,'JSFW_fnGetElementsByName');
		}
	}
	// fnShowElement : This function is used to hide a div element
	function JSFW_fnHideElement(oDivElement)
	{
		try
		{
			if(typeof(oDivElement) != 'undefined' && oDivElement != null)
				oDivElement.style.display = "none";
			else
				this.fnAlert("fnHideElement: No Element defined");
		}
		catch(ex)
		{
			fnHandleException(ex,'fnHideElement');
		}
	}
	
	// fnShowElement : This function is used to show a div element
	function JSFW_fnShowElement(oDivElement)
	{
		try {
		    if (typeof (oDivElement) != 'undefined' && oDivElement != null) 
			{
			    oDivElement.style.display = "block";
			}
			else
				this.fnAlert("fnHideElement: No Element defined");
		}
		catch(ex)
		{
			fnHandleException(ex,'fnHideElement');
		}
	}
	
	function JSFW_fnValidateEmail(strString)
	{
		try
		{
			var re = new RegExp("^([a-zA-Z0-9~!#\$%\^\&\*_\.\=-])+\@(([a-zA-Z0-9~!#\$%\^\&\*_\.\=-])+\.)+([a-zA-Z0-9]{2,4})+$");
			return re.test(strString); 
		}
		catch(ex)
		{
			fnHandleException(ex,'fnValidateEmail');
		}
	}

function JSFW_fnValidateMultipleEmailAddress(strAddress)
{
    var isvalid = true;
    if (strAddress && strAddress.length > 0) {
        var emailids = strAddress.split(';');
        if (emailids && emailids.length > 0) {
            for (i = 0; i < emailids.length; i++) {
                if (this.fnValidateEmail(emailids[i])) {
                    isvalid = true;
                }
                else {
                    return false;
                }
            }
        }
        }
        return isvalid;
}
	// fnValidateForm : This function is used to validate form elements with specific type
	// 'd' -> Date. 's' -> String, 'f' -> float, 'i' -> int, 'e' -> email
	function JSFW_fnValidateForm(oForm,oElementArray,fnCallback,blnReplaceBR,blnMatchExact)
	{
		try
		{
		  // Spin Each Element
		  var c_elem = oForm.elements.length;
		  var c_desc = oElementArray.length;
		  
		  var i = 0;
		  var result;
		  var strDescription;
		  var strType;
		  for (i=0;i<c_elem;i++)
		  {
		    var elem = oForm.elements[i];
		    var j = 0;
		    for (j=0;j<c_desc;j++)
		    {
		      strType = oElementArray[j][1];
		      strDescription = oElementArray[j][2];
			  
		      if(blnMatchExact)
		         result=(oElementArray[j][0] == elem.name)?true:false;
		      else
		         result=(elem.name.indexOf(oElementArray[j][0])>-1)?true:false;
		      
		      
		      if (result)//oElementArray[j][0] == elem.name)
		      {
			    j = c_desc+1;
				
				if (this.fnTrim(elem.value).length==0)
		        {
		            this.fnAlert('Fout: Verplicht veld\nHet veld ' + strDescription + ' heeft een ongeldige waarde.');
		            elem.focus();
					elem.select();
		            return false;
		        }
				  
		        if (strType.length==2)
		        {
		            strType = strType.substr(0,1);
		        }
		        if (elem.value != '') 
				{
					if (strType=='d') 
					{
						var dt = new Date(elem.value);
						if (dt.toString() == 'Invalid Date')
						{
							if (fnCallback)
							{
								this.fnAlert('Fout: Ongeldige datum\nHet veld ' + strDescription + ' heeft een ongeldige waarde.');
								elem.focus();
								elem.select();
								return false;
							}
							else
							{
								this.fnAlert('Het veld ' + strDescription + ' heeft een ongeldige waarde.');
								elem.focus();
								elem.select();
								return false;
							}
						}
					}
					else
					{
						var re = null;
			          
						if (strType=='e') re = new RegExp("^([a-zA-Z0-9~!#\$%\^\&\*_\.\=-])+\@(([a-zA-Z0-9~!#\$%\^\&\*_\.\=-])+\.)+([a-zA-Z0-9]{2,4})+$");
						else if (strType=='f') re = new RegExp('^([0-9]+\.?[0-9]*|\.[0-9]+)$');
						else if (strType == 'i') re = new RegExp('^[0-9]+$');
						else if (strType == 's')
						{
							try
							{
								if (oElementArray[j][3])
								{
								re = new RegExp(oElementArray[j][3]);
								}
								else 
								{
									re = new RegExp("^[ \u00c0-\u00ffa-zA-Z0-9\-\.\"',_,~,!,@,#,\$,%,\^,&,\*,\(,\),\+,\|,=,:,;,\{,\},/,\[,\?,\<,\>,\\\\,\\\]]+$");
								}
							}
							catch(e)
							{
								re = new RegExp("^[ \u00c0-\u00ffa-zA-Z0-9\-\.\"',_,~,!,@,#,\$,%,\^,&,\*,\(,\),\+,\|,=,:,;,\{,\},/,\[,\?,\<,\>,\\\\,\\\]]+$");
							}            
						}
					
						if (strType != 'o')
						{
							var blnReturn = false;
							
							if(typeof(blnReplaceBR) != 'unidentified' && blnReplaceBR == true)
								blnReturn = re.test(elem.value.replace(/(\r\n|[\r\n])/g, "<br />"));
							else
								blnReturn = re.test(elem.value);
						
							if (!blnReturn)
							{
								if (fnCallback)
								{
									fnCallback(strDescription,'Ongeldige waarde');
									elem.focus();
									elem.select();
									return false;
								}
								else
								{
									this.fnAlert('Het veld ' + strDescription + ' heeft een ongeldige waarde.');
									elem.focus();
									elem.select();
									return false;
								}
							}
						}
					}
		        }
		      }
		    }
		  }
		  return true;
		}
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnValidateForm');
		}		
	}	

	function JSFW_fnTrim(strInputString)
	{
		try
		{
			if(strInputString.length < 1){
			return"";
			}
			
			strInputString = this.fnRTrim(strInputString);
			strInputString = this.fnLTrim(strInputString);
			if(strInputString=="")
			{
				return "";
			}
			else
			{
				return strInputString;
			}
		}
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnTrim');
		}
	}

	function JSFW_fnRTrim(strInputString)
	{
		try
		{
			var w_space = String.fromCharCode(32);
			var v_length = strInputString.length;
			var strTemp = "";
			if(v_length < 0)
			{
				return"";
			}
			var iTemp = v_length -1;

			while(iTemp > -1)
			{
				if(strInputString.charAt(iTemp) == w_space)
				{}
				else
				{
					strTemp = strInputString.substring(0,iTemp +1);
					break;
				}
				iTemp = iTemp-1;
			} 
			return strTemp;
		}
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnRTrim');
		}
	}

	function JSFW_fnLTrim(strInputString)
	{
		try
		{
			var w_space = String.fromCharCode(32);
			if(v_length < 1)
			{
				return"";
			}
			var v_length = strInputString.length;
			var strTemp = "";
			var iTemp = 0;

			while(iTemp < v_length)
			{
				if(strInputString.charAt(iTemp) == w_space){
				}
				else
				{
					strTemp = strInputString.substring(iTemp,v_length);
					break;
				}
				iTemp = iTemp + 1;
			} 
			return strTemp;
		}
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnLTrim');
		}
	} 

	function JSFW_fnSubmitForm(oForm)
	{
		try
		{			
			if(oForm)
			{				
				oForm.submit();
			}
		}
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnSubmitForm');
		}
		
	}
	
	function JSFW_fnValidateOnKeyPress(oEvent,oElement,strAction)
	{
		try
		{
			var intCharCode = (oEvent.which) ? oEvent.which : event.keyCode;
			if(String(strAction).toLowerCase() == 'i')
			{
				if(intCharCode > 47 && intCharCode < 58)
					return true;
				else 
					return false;
			}
			
			return true;
		}
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnValidateOnKeyPress');
		}
		
	}
	
	function JSFW_fnProcessSearchText(oForm)
	{
		try
		{
			var strSearchText;
			if(oForm.searchtext)
				strSearchText= oForm.searchtext.value;
			else if(oForm.sitesearchtext)
				strSearchText= oForm.sitesearchtext.value;
			else if(oForm.advancedsearchtext)
				strSearchText= oForm.advancedsearchtext.value;
			
			
			if(oForm.hsearchtext)
				oForm.hsearchtext.value = '';
			
			strSearchText = strSearchText.replace( new RegExp(" and ", "i" ) , " AND ");
			strSearchText = strSearchText.replace(new RegExp(" or ", "i" ), " OR ");
			strSearchText = strSearchText.replace(" AND ", " ");
			
			if(strSearchText.indexOf('"') != -1)
			{
				oForm.hsearchtext.value = "\"" + strSearchText.substring(1,strSearchText.length - 1) + "\"";
			}
			else if(strSearchText.indexOf(" OR ") != -1 )
			{
				var index = strSearchText.indexOf(" OR ");
				var searchitem = strSearchText.substring(0,index)
				while(index != -1)
				{
					if (oForm.hsearchtext.value == '')
					{
						oForm.hsearchtext.value += "\"*" + searchitem + "*\"";
					}
					else
					{
						oForm.hsearchtext.value += " OR \"*" + searchitem + "*\"";
					}
					strSearchText = strSearchText.substring(index + 4);
					index = strSearchText.indexOf(" OR ");
					searchitem = strSearchText.substring(0,index);
				}
				if (oForm.hsearchtext.value == '')
				{
					oForm.hsearchtext.value += "\"*" + strSearchText + "*\"";
				}
				else
				{
					oForm.hsearchtext.value += " OR \"*" + strSearchText + "*\"";
				}
			}
			else if(strSearchText.indexOf(" ") != -1 )
			{
	      
				var index = strSearchText.indexOf(" ");
				var searchitem = strSearchText.substring(0,index)
				while(index != -1)
				{
					if (oForm.hsearchtext.value == '')
					{
						oForm.hsearchtext.value += "\"*" + searchitem + "*\"";
					}
					else
					{
						oForm.hsearchtext.value += " AND \"*" + searchitem + "*\"";
				
					}
				strSearchText = strSearchText.substring(index + 1);
				index = strSearchText.indexOf(" ");
				searchitem = strSearchText.substring(0,index);
				}
				if (oForm.hsearchtext.value == '')
				{
					oForm.hsearchtext.value += "\"*" + strSearchText + "*\"";
				}
				else
				{
					oForm.hsearchtext.value += " AND \"*" + strSearchText + "*\"";
				}
			}
			else
			{
				if (oForm.hsearchtext.value == '')
				{
					oForm.hsearchtext.value += "\"*" + strSearchText + "*\"";
				}
			}	
	    }
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnProcessSearchText');
		}
	}

	
	function JSFW_fnCheckPasswordForm(elem1, elem2, frm, descriptor)
	{
		try
		{
			
			var ele1 = document.getElementById(elem1);
			var ele2 = document.getElementById(elem2);
		
			if (this.fnValidateForm(frm, descriptor))
			{
				if ( ele1 != null && ele2 != null)
					if (this.fnTrim(ele1.value) != this.fnTrim(ele2.value))
					{
						alert('Fout: De invoer was niet gelijk, probeer het opnieuw.');
						ele1.focus();
						ele1.select();
						return false;
					}
				return true;
			}
			else
				return false;
		
		}
		catch(ex)
		{
			fnHandleException(ex,'JSFW_fnCheckPasswordForm');
		}
	}

	
	function JSFW_fnPrintDivContent(divID, pageHeading)
	{
		try
		{
			//Hide Print Button
			this.fnGetElementById('imgPrint').style.display='none';

			var id = divID
			str=this.fnGetElementById(id).innerHTML
			newwin=window.open('','printwin','left=35,top=35,width=780,height=450,scrollbars=yes')
			newwin.document.write('<HTML>\n')
			newwin.document.write('<LINK href="css/nvmbr_extranet.css" type="text/css" rel="stylesheet">\n')

			/*
			newwin.document.write('<LINK href="/Content/_template/css/layout2_setup.css" type="text/css" rel="stylesheet">\n')
			newwin.document.write('<LINK href="/Content/_template/css/layout2_text.css" type="text/css" rel="stylesheet">\n')
			newwin.document.write('<LINK href="/Content/_template/css/layout2_setup_secure.css" type="text/css" rel="stylesheet">\n')
			*/
			
			newwin.document.write('<HEAD>\n')
			newwin.document.write('<TITLE>Print Page</TITLE>\n')
			newwin.document.write('<script>\n')
			newwin.document.write('function chkstate(){\n')
			newwin.document.write('if(document.readyState=="complete"){\n')
			newwin.document.write('window.close()\n')
			newwin.document.write('}\n')
			newwin.document.write('else{\n')
			newwin.document.write('setTimeout("chkstate()",2000)\n')
			newwin.document.write('}\n')
			newwin.document.write('}\n')
			newwin.document.write('function print_win(){\n')
			newwin.document.write('window.print();\n')
			newwin.document.write('chkstate();\n')
			newwin.document.write('}\n')
			newwin.document.write('<\/script>\n')
			newwin.document.write('</HEAD>\n')
			newwin.document.write('<BODY onload="print_win()">\n')


			newwin.document.write('<table cellpadding=2 cellspacing=2 border=0 class=content width=100%>\n')
			newwin.document.write('<tr><td  class=pageHeading>'+ pageHeading +'</td>\n')
			newwin.document.write('<tr><td width=100% colspan=2 BACKGROUNDCOLOR= black  height=1></td></tr>\n')
			newwin.document.write('<tr><td>\n')
				
			newwin.document.write(str)
			
			newwin.document.write('</td></tr>\n')
			newwin.document.write('</table>\n')
			newwin.document.write('</BODY>\n')
			newwin.document.write('</HTML>\n')
			newwin.document.close()

			this.fnGetElementById('imgPrint').style.display='';
	    }
	    catch(err)
	    {}
		return false;
	}


	function JSFW_fnValidateDownload(strCheckBoxName,strAlternatCheckBoxName) 
	{
	    try
	    {
	        var contArray = this.fnGetElementsByName(strCheckBoxName);
	        if (contArray)
	        {
	            for (var intcounter = 0; intcounter < contArray.length; intcounter++) 
	            {
	                if (eval("contArray[" + intcounter + "].checked"))
	                {	             
	                    return true;
	                }
	            }

	            contArray = this.fnGetElementsByName(strAlternatCheckBoxName);
	            if (contArray) 
	            {
	                for (var intcounter = 0; intcounter < contArray.length; intcounter++) 
	                {
	                    if (eval("contArray[" + intcounter + "].checked")) 
	                    {
	                        return true;
	                    }
	                }

	            }
	        }
	        
	        return false;
	    }
	    catch(err)
	    {}
	}

	function JSFW_fnDownloadSelectedDocuments(strFormName, strCheckBoxName,strAlternateCheckBoxName, strAction, blnAutoSubmit)
	{
		try
		{
		    var contArray = this.fnGetElementsByName(strCheckBoxName);
		    var alterContArray = this.fnGetElementsByName(strAlternateCheckBoxName);
			var oForm;

			if (contArray || alterContArray) 
			{
			
			    if (strFormName && strFormName != '') {
			        oForm = oJSFW.fnGetElementById(strFormName);
			    }
			    else {
			        oForm = this.fnGetFormByElement(contArray[0]);
			    }
			    
				if ((typeof contArray == "object" && typeof contArray.length == 'number' && isFinite(contArray.length)) ||
				    (typeof alterContArray == "object" && typeof alterContArray.length == 'number' && isFinite(alterContArray.length)))
				{
					
					var total = 0;				
					for(var intcounter = 0; intcounter < contArray.length ; intcounter++) 
					{
						if (eval("contArray[" + intcounter + "].checked")) 
						{
							total += 1;
							break;
						}
		            }
		            
		            for (var intcounter = 0; intcounter < alterContArray.length; intcounter++) {
		                if (eval("alterContArray[" + intcounter + "].checked")) 
		                {
		                    total += 1;
		                    break;
		                }
		            }
					
					if(total > 0 && oForm)
					{
						oForm.action = strAction;
						this.fnSubmitForm(oForm);						
					} 
					else
					{
					    return false;
					}
				}
				else
				{
					if (contArray.checked) {
					    if (strAction) {
					        oForm.action = strAction;
					    }
						oJSFW.fnSubmitForm(oForm);
					}
					else
					{
						alert("Geen documenten geselecteerd.");
					}
				}
            }            
            
		}
		catch(ex)
		{
				fnHandleException(ex,'JSFW_fnDownloadSelectedDocuments');
		}
	}

	function JSFW_fnGetFormByElement(oElement) 
	{
	    while (oElement.nodeName.toLowerCase() != 'body') {

	        if (oElement.nodeName.toLowerCase() == 'form')
	            return oElement;
	        else
	            oElement = oElement.parentNode;
	    }
	}
	
	function JSFW_fnDownloadAllDocuments(FormName, FieldName, strAction,blnAutoSubmit)
	{
	    try 
		{
		    var oForm;		    
            var objCheckBoxes;

            if (FormName && FormName != ''){
                oForm = document.forms[FormName];               
			}
			else{
			    var oElements = document.getElementsByName(FieldName);
			    oForm = this.fnGetFormByElement(oElements[0]);
			}
			
			if(oForm)
			    objCheckBoxes = oForm.elements[FieldName];
				
			var countCheckBoxes = objCheckBoxes.length;
			if(!countCheckBoxes){
				objCheckBoxes.checked = true;
			}
			else{
				for(var i = 0; i < countCheckBoxes; i++) 
					objCheckBoxes[i].checked = true;
			}

			if (blnAutoSubmit == true && oForm && objCheckBoxes) {
			    if (strAction) {
			        oForm.action = strAction;
			    }
			    this.fnSubmitForm(oForm);
			}
			else {
			    return false;
			}		
		}
		catch(ex)
		{
				fnHandleException(ex,'JSFW_fnDownloadAllDocuments');
		}
	}
	
	function fnHandleException(oEx,strCallingFunction)
	{
		this.fnAlert(strCallingFunction + ":-" + oEx.message);
	}		


    function JSFW_fnCheckInterestProfileNode(parent, nodelist)
     {
        var nodes = nodelist.split('|');
        if (parent.checked) 
            {
            for (i = 0; i < nodes.length; i++) 
            {
                var ele = this.fnGetElementById(nodes[i]);
                if (ele != null)
                    ele.checked = true;
            }
        }
        else 
        {
            var nodes = nodelist.split('|');
            for (i = 0; i < nodes.length; i++) 
            {
                var ele = this.fnGetElementById(nodes[i]);
                if (ele != null)
                    ele.checked = false;
            }
        }


    }

     function JSFW_fnCheckUncheckParentNode(oChild,nodelist,strParentId)
      {
	    var blnIsAllSubNodesChecked =true;
	     var nodes = nodelist.split('|');
	    for(i=0;i<nodes.length;i++)
	    {
		    var ele = document.getElementById(nodes[i]);
		    if (ele != null && (!ele.checked))
		    {
			    blnIsAllSubNodesChecked = false;
			    break;
		    }
	    }
	    if (blnIsAllSubNodesChecked == true) {
	        var parent = document.getElementById(strParentId);
	        if (parent != null)
	        parent.checked = true;
	    }
	    else {
	        var parent = document.getElementById(strParentId);
	        if (parent != null)
	        parent.checked = false;
	    }
    }
}
var oJSFW = new JavaScriptFramework();

