function textCounter(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit);
	else 
		countfield.value = maxlimit - field.value.length;
}
function userButtonColor()
{ 
  var numberForms = document.forms.length;
  var formIndex;
  for (formIndex = 0; formIndex < numberForms; formIndex++)
  {
	var frName = document.forms[formIndex].name;
	if (frName != "")
	{
		var aElements = eval('document.'+frName+'.elements');
		//alert(aElements.length);
		for ( var i = 0; aElements.length > i; i++ )
		{
			var oElement = aElements[ i ];	   
				if( oElement.type == 'button' || oElement.type == 'reset' || oElement.type == 'submit') {
					oElement.style.background = '#F6F9F3'
					oElement.style.color = '#6F9A35'
					//oElement.style.font-size = '12px';
				} // end of if
		} // end of for 
	} // end of if frName != ""
  } // end of for
}
function buttonColor(formName)
{
	 var aElements = eval('document.'+formName+'.elements');
	 for ( var i = 0; aElements.length > i; i++ )
	 {
	   var oElement = aElements[ i ];
	   
	   if( oElement.type == 'button' || oElement.type == 'reset' || oElement.type == 'submit')
	   {
		 oElement.style.background = '#F3F3F9'
		 oElement.style.color = '#4A4A95'
		 //oElement.style.font-size = '12px';
	   }
	 }	
}
function openWindow(url)
{	
	//var theHeight=260;
	var theHeight=355;
	var theWidth=700;
	var theTop=(screen.height/2)-(theHeight/2);
	var theLeft=(screen.width/2)-(theWidth/2);
	var features= "dependant=yes,height="+theHeight+",width="+theWidth+",top="+theTop+",left="+theLeft+",scrollbars=yes,resizable=no, scrollbars=no,menubar=no,toolbar=no,status=yes";
	theWin=window.open(url,'',features);
	theWin.location.reload(true);
}
function openWindow_WOReload(url)
{	
	//var theHeight=260;
	var theHeight=355;
	var theWidth=700;
	var theTop=(screen.height/2)-(theHeight/2);
	var theLeft=(screen.width/2)-(theWidth/2);
	var features= "dependant=yes,height="+theHeight+",width="+theWidth+",top="+theTop+",left="+theLeft+",scrollbars=yes,resizable=no, scrollbars=no,menubar=no,toolbar=no,status=yes";
	theWin=window.open(url,'',features);
	//theWin.location.reload(true);
}
function validateForm(objlist)
{	//alert("From messages.validateForm");
	// parsing validation specification line and alert if not valid.
	// else return true
	// String will be in the form of "document.myform.T1|M|C9"
	// ie. <Object Name><seperator><Manderory flag>
	//     where '|' is seperator
	
	seperator = '|';

	for( i = 0 ; i < objlist.length ; i++)
	{	specificstr = objlist[i] ;
		v =  specificstr.indexOf(seperator) ;
	   	objname		=	specificstr.substring( 0 , v ); // returns object name
		specificstr =	specificstr.substring( v+1 );	//Removing object name from speficstring 

		obj = eval ( objname ) ;
		objvalue = obj.value ;							// Get value of the object in 'objvalue'
		
		flag = true ;
		varlist = "";
		while(specificstr.length != 0)
		{	if(specificstr.indexOf(seperator) > -1)
			{	type		=	specificstr.substring( 0 , specificstr.indexOf(seperator) ); // Type of validation need to apply on the field
				specificstr	=	specificstr.substring( specificstr.indexOf(seperator)+1 );	
			}
			else
			{	type		=	specificstr	
				specificstr =	"" ;
			}
			//******** Validate field value according to type

			if(type == "M" && Number(objvalue) == 0)
			{	errno = "MSG000" ;
				flag = false ;
			}
			else if( type.charAt(0) == 'C' && objvalue.length > 0 )
			{	if(type.indexOf('-') > -1)
				{	min		=	Number(type.substring(2, type.indexOf('-')));   // C(1-12)
					max		=	Number(type.substring(type.indexOf('-')+1, type.indexOf(')')));   // C(1-12)
					if( objvalue.length < min ||  objvalue.length > max  )
					{	errno = "MSG004" ;
						flag = false ;
					}
					varlist = varlist + ":" + min + ":" + max ;					// set variable list to send to display function
				}
				else
				{	min		=	Number(type.substring(2, type.indexOf(')')));   // C(12)
					max		=	min ;
					if( objvalue.length < min ||  objvalue.length > max  )
					{	errno = "MSG007" ;
						flag = false ;
					}
					varlist = varlist + ":" + min ;					// set variable list to send to display function
				}
				
				
				
			}
			else if( type.charAt(0) == 'L' )
			{	varlist	=	varlist + ":" +type.substring(1); // each variable value is prefix by ':'
			}
			else if( type.charAt(0) == 'P' && objvalue.length > 0 )
			{	if(type.substring(1) == "EMAIL" && (! isValidEmail(objvalue)) )
				{	errno = "MSG005" ;
					flag = false ;
				}
			}
			/* Added by Rakesh to cater negative values */
			/* Commented
			else if( type.charAt(0) == 'Z' )
			{
			    myObjLength = type.charAt(1);
			    alert("inside mine length : "+myObjLength);
				if(  myObjLength != Number(myObjLength) )
				{	errno = "MSG006" ;
					flag = false ;
				}
			}
			*/
			/* Added by Rakesh to cater negative values */
			else if( type.charAt(0) == 'N' && objvalue.length > 0 )
			{	
				if(  objvalue != Number(objvalue) )
				{	errno = "MSG006" ;
					flag = false ;
				}
				else if(type.indexOf('-') > -1)
				{	min		=	Number(type.substring(2, type.indexOf('-')));   // C(1-12)
					max		=	Number(type.substring(type.indexOf('-')+1, type.indexOf(')')));   // C(1-12)
					if( Number(objvalue) < min ||  Number(objvalue) > max  )
					{	errno = "MSG011" ;
						flag = false ;
					}
					varlist = varlist + ":" + min + ":" + max ;					// set variable list to send to display function
				}
			}
			else if( type.charAt(0) == 'I' && objvalue.length > 0 )
			{	
				//alert( Number(objvalue) != parseInt(Number(objvalue))  );
				if(  objvalue != Number(objvalue) )
				{	errno = "MSG006" ;
					flag = false ;
				}
				else if(  Number(objvalue) != parseInt(Number(objvalue)) )
				{	errno = "MSG012" ;
					flag = false ;
				}
				else if(type.indexOf('-') > -1)
				{	min		=	Number(type.substring(2, type.indexOf('-')));   // C(1-12)
					max		=	Number(type.substring(type.indexOf('-')+1, type.indexOf(')')));   // C(1-12)
					if( Number(objvalue) < min ||  Number(objvalue) > max  )
					{	errno = "MSG011" ;
						flag = false ;
					}
					varlist = varlist + ":" + min + ":" + max ;					// set variable list to send to display function
				}
				
			}
			if(! flag )
			{	display( errno , varlist );
				obj.focus();
				return false ;
			}

		} // end of while()


	} // end  of for ()
	return true ;
}

//****************************************************************
//	Use this function to design/format the message
//****************************************************************
function display( msgid , varlist )
{	messages = new messagelist();
	alert( addvariables(	messages[msgid] , varlist ));
}


//****************************************************************
//	Define messages here
//****************************************************************
function messagelist()
{	this.MSG000	=	"Please fill in '#01'.";	
	this.MSG001	=	"Please enter a value for '#01' in '#02' format.";
	this.MSG002	=	"Please enter a valid email id in the '#01' field."
	this.MSG003 	=   	"The number of characters in the field '#01' should not exceed more than '#02' characters." ;
	this.MSG004 	=	"The number of characters in the field '#01' should not exceed more than #03 characters.";
	this.MSG005	=	"Please enter valid E-mail address e.g. name@domain.com";
	this.MSG006	=	"Please enter a Numeric value for the field '#01'. ";
	this.MSG007	=	"Please enter a value in the field '#01'. The  value cannnot exceed more than #02 characters. ";
	this.MSG008 	=   	"Please enter values in the fields 'NRIC' or 'User Name'. The fields cannot be left blank.";
	this.MSG009 	=   	"Please select minimum one record for deletion. " ;
	this.MSG010 	=	"Please enter value of '#01' in the range of #02-#03 .";
	this.MSG011 	=	"Value for '#01' should be in the range of #02-#03 .";
	this.MSG012 	=	"Please enter an integer value for '#01' e.g. 12 , 34 , 2 . \n A Decimal value like 3.4 cannot be accepted.";
}

function addvariables(	msgstr ,	varlist )
{	varvalues = new Array() ;						// Array to handle variable values 
	i = 0 ;											// initialize i again for next use 
	
	varlist = varlist + ":";						// ':' has been postfixed for building following logic
	while(	varlist.indexOf(":") > -1 && varlist.length > 1)
	{	
		varlist = varlist.substring(1);				// remove first char ie ":"
		varvalues[i] = varlist.substring( 0 , varlist.indexOf(":") );	// get and assign variable value
		varlist = varlist.substring(varlist.indexOf(":"));			// remove variable value
		i++ ;
	}

	
	finalmsg = msgstr ;
	while( finalmsg.indexOf('#') > -1 )
	{	hashindex = finalmsg.indexOf('#') ;
		finalmsg =	finalmsg.substring( 0 , hashindex) + varvalues[Number( finalmsg.substring( hashindex + 1 , hashindex + 3) )-1] +	finalmsg.substring( hashindex + 3) ;	
	}
	return finalmsg ;
}

//****************** Pattern checking functions 
//
//***********************************************
function isValidEmail(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   return false
		 }

 		 return true					
	}

/*-------------------------------- 
 * 20May2005 - validate CC Email 
 ---------------------------------*/
function chkCCEmail(str) {
	var result = true;
	var delim=",";
	
	var lat=str.indexOf(delim);
	var lstr=str.length;
	//alert('ccEmailList = ' + str + ' :: ' + lat + ' :: ' + lstr );
	
	//alert('check last character :: ' + + str.indexOf(delim));
	/* Check 1st occurrence or last occurrence */
	if (str.indexOf(delim)== 0 || str.indexOf(delim)==lstr-1){
 	  //alert('ccEmailList at 1st occurrence or last occurrence ' + str.indexOf(delim) );
 	   result = false;
	}
		
	var str_array = str.split(delim);
	//alert('array length ::'  + str_array.length);
	for (var i=0; i < (str_array.length); i++ ) {
		//alert(i+1 + "# " + str_array[i] );
		if (! isValidEmail(str_array[i]) || str_array[i] == "") {
	 	    alert("Please specify valid email for CC Email") ;
			result = false;	
			break;
		}
	}
//alert('chkCCEmail is ' + result);
	return result;

}

function validatesugview() {
	 email  = "" ;
     radioGrp = replyform.useraction ;
      for (var i=0; i<radioGrp.length; i++) {
         if (radioGrp[i].checked) {
            //alert( "useraction :" +radioGrp[i].value ) ;
			useraction = radioGrp[i].value ;
         }
      }	  
    // Check for reply type
	replyGrp = replyform.mailtype ;
	isRlyTypSelected = false ;
      for (var i=0; i<replyGrp.length; i++) {
         if (replyGrp[i].checked) {
            //alert( "Selected Reply :" +replyGrp[i].value ) ;
			selectedReply = replyGrp[i].value ;
			isRlyTypSelected = true;
         }
      }	  



    //alert("email"  + replyform.channellemail.value) ;
    //alert("reply"  + replyform.usertxt.value) ;
    //alert("Selected Reply :" + selectedReply) ;
	if (useraction == 2)
	{
		//alert ("Please enter the channel subject"  ) ;
        if ( replyform.status.value == "P")
        {
			alert ("The suggestion is published to the website.It cannot be channelled") ;
            replyform.channellemail.value = "" ;
            return false ;  
        } 

		if (replyform.channelsubject.value == "")
		{
            alert ("Please enter the channel subject") ;
			return false ;
		}

		//if ( selectedReply != 1)
		//{
		//	alert("Please choose reply 4 for channeling.") ;
		//	return false ;
		//}
			
			if  (isValidEmail(replyform.channellemail.value) | chkEmailList() ) 
              {
				var emailValid = true;
				
				 if ( replyform.channellemail.value == "" ) 
			     {
				    replyform.allemail.value = email.substring(0,email.length -1) ;
				    //alert("Email List(w/o: channel email) = " + replyform.allemail.value) ; 
			     }
		     	 else
			     {
				    replyform.allemail.value=email + replyform.channellemail.value ;
				    //alert("Email List (default + channel email) = " + replyform.allemail.value) ; 
			     }
				 
				/* 20May2005 - include checking of CC Email */
				var ccEmailList = replyform.ccEmailList.value;
				var ccEmailListExists = false;
				if  ( ccEmailList != "" ) 
				{
					//alert('ccEmailList :: ' + ccEmailList);
					ccEmailListExists = true;
					if ( !chkCCEmail(ccEmailList) )
					{
						emailValid = false;
						//alert('ccEmailListValid :: ' + ccEmailListValid);
					} 
				}
			     return emailValid ;
              }
              else
	  	      {
			     alert("Please specify valid email Id") ;
			     return false ;
	          }

		
		// Check for assigned reference number
    	if (replyform.validrefno.value == "NotAssigned")
	    {
		   if (confirm("You have not assign a reference number to this suggestion. Do you wsh to continue?\n\nOK=Yes - Cancel=No")){ 
			  // Check for valid email address
			  return true;
		   }
		   else
		   {
		      return false;
		   }
	    }
	}

	//if (useraction == 3 | useraction == 4)
	if (useraction == 3 )
	{
		//alert("Reply :" + replyform.usertxt.value + "End") ;
		if (replyform.publishreply.value == "")
		{
			alert("Please enter the reply message before publish.")
			return false ;
		}
        else 
		{			
			return true ;
		}		
	}
	if (useraction == 1 )
	{
		if (!isRlyTypSelected)
		{
			alert("Select a reply type" ) ;
			return false ;
		} 

	    // Check for assigned reference number
    	if (replyform.validrefno.value == "NotAssigned")
	    {
		   if (confirm("You have not assign a reference number to this suggestion. Do you wsh to continue?\n\nOK=Yes - Cancel=No")){ 
		      return true ;
		   }
		   else
		   {
		      return false ;
		   }
	    }		
	}


//return false ;

}


function validateloginform() {
	if (loginform.userid.value == "")
	{
		alert ("Please enter your IC number")
		return false ;
	}
	else if ( loginform.userpasswd.value == "")
	{
		alert("Please enter your password");
		return false ;

	}
	else
	{
	return true;
	}
}

function chkEmailList() {
	result = false ;
	emailGrp = replyform.emailGrp ;
		  for (var i=0; i<emailGrp.length; i++) {
			 if (emailGrp.options[i].selected) {
				//alert( "Selected Email :" +emailGrp.options[i].value ) ;
				email += emailGrp.options[i].value + ","
				//alert( "Selected Email :" + email ) ;
				result  = true ;
			 }
		  }	  
    return result ; 

}
