
var alertMsg = '';
var focusField = null;
var field = '';


function mask( pattern, value ) {
	var  reg = new RegExp( pattern );
	return reg.test( value );	
}

// Removes leading whitespaces 
function LTrim( value ) { 
        
        var re = /\s*((\S+\s*)*)/; 
        return value.replace(re, "$1"); 
        
} 

// Removes ending whitespaces 
function RTrim( value ) { 
        
        var re = /((\s*\S+)*)\s*/; 
        return value.replace(re, "$1"); 
        
} 

// Removes leading and ending whitespaces 
function trim( txtObject ) { 
        txtObject.value = LTrim(RTrim(txtObject.value)); 
        
} 



// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function doDateComparision(fromDate, toDate, param1, param2) {
	if (!(Date.parse(fromDate.value) < Date.parse(toDate.value))) {
				message = message + param2 + " should be greater than the " + param1 + "."+ "\n";
				if(field == ''){
					field = toDate;
				}
	}
	
}



// Pass the date value.
function isDate(dtStr)
{
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month.")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day.")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear + ".")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date.")
		return false
	}
return true
}


// Date compare between fromDate and toDate.
function doDateCompare(fromDate, toDate, param1, param2) {

	var from = validatorForMandatoryField( fromDate, param1);	
	var to = validatorForMandatoryField( toDate, param2);	
	
	var v1= isDate(fromDate.value);
	var v2= isDate(toDate.value);
	
	
	if(  from && to	 ) {
			if(v1 == false){
				alertMsg = alertMsg + param1 + " is invalid.\n" ;
				if(field == ''){
					field = fromDate;
				}
				
			}
			if(v2==false){
				alertMsg = alertMsg + param2 + " is invalid.\n" ;
				if(field == ''){
					field = toDate;
				}
					
			}						
			if(v1 && v2){
					if (!(Date.parse(fromDate.value) < Date.parse(toDate.value))) {
							alertMsg = alertMsg + param1 + " must be less than " + param2 + "\n";
						if(field == ''){
							field = toDate;
						}	
					}
			}
		}
	
}

// Validation for mandatory field
function validatorForMandatoryField( txtObject, fieldName ) {
	trim( txtObject );
	if( mask( "^$", txtObject.value ) )  {
		alertMsg = alertMsg + "Preencha o campo " + fieldName + "\n"
		if(field == ''){
			field = txtObject;
		}
		return false;
	}
	return true;
		//alert( fieldName + " is madatory");	
	
}

// Validation for alphabet value.
function validatorForAlphabet( txtObject , fieldName, mandatory ) {
	if( mandatory ) {
		if( !validatorForMandatoryField( txtObject, fieldName ) )
			return;
	} else {
		trim( txtObject );
	}
	if( txtObject.value != '' && !mask( "^[a-zA-Z]+$", txtObject.value ) ){
		alertMsg = alertMsg + "Please enter valid alphabets for " + fieldName + "\n";
		//alert( "Please enter valid alphabets for " + fieldName);
		if(field == ''){
			field = txtObject;
		}
	}
}
// Validation for Alpha numeric value.
function validatorForAlphaNumeric( txtObject , fieldName , mandatory) {
	//if( !mask( "^[a-zA-Z]+[a-zA-Z0-9]+$", txtObject.value ) )
	if( mandatory ) {
		if( !validatorForMandatoryField( txtObject, fieldName ) )
			return;
	} else
		trim( txtObject );
	if( txtObject.value != '' &&  !mask( "^[a-zA-Z0-9 ]+$", txtObject.value ) ){
		alertMsg = alertMsg + fieldName + " can take alphanumeric value only. Spaces are not allowed at starting and ending point.\n";
		//alert( fieldName + " should be start with the alphabet and it contains alphanumeric value only" );
		if(field == ''){
			field = txtObject;
		}
	}
}

// Validation for interger value.
function validatorForInteger( txtObject , fieldName, mandatory ) {
	if( mandatory ) {
		if( !validatorForMandatoryField( txtObject, fieldName ) )
			return;
	} else
		trim( txtObject );
	if( txtObject.value != '' &&  !mask( "^[0-9]+$", txtObject.value ) ){
		alertMsg = alertMsg + "Please enter valid integer value for " + fieldName + "\n";
//		alert( "Please enter valid integer for " + fieldName);
		if(field == ''){
			field = txtObject;
		}
	}
}

function getAlertMsg() {
		return alertMsg;
}
function setAlertMsg( value ) {
		alertMsg = value;
}



// Validation For Login Page. ( login.jsp)
function validationLoginForm(var1, var2){
	
	// For User name 
	validatorForMandatoryField(document.loginForm.userName ,var1);
	// For Password
	validatorForMandatoryField(document.loginForm.password ,var2);
	
	// Get the any alert message.
	if ( getAlertMsg()!= '' ) {
		alert( getAlertMsg() );
		alertMsg = '';
		if(field != ''){
                	focusField = field;
                	focusField.focus();
        }
        field = '';
		return false;
	}
	return true;
}

//Begin
function isValidDate(dateStr, msg) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\.)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert( msg );
return false;
}
day = matchArray[1]; // parse date into variables
month = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}

return true;  // date is valid
}
//  End -->
function comparedate(fromdate,todate)
{
	var currentTime = new Date();

	var currentmonth = currentTime.getMonth() + 1
	var currentday = currentTime.getDate()
	var currentyear = currentTime.getFullYear()
	
	
	var datePat = /^(\d{1,2})(\.)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	var frommatchArray = fromdate.match(datePat); // is the format ok?
	var tomatchArray = todate.match(datePat); // is the format ok?
	
	fromday = frommatchArray[1]; // parse date into variables
	frommonth = frommatchArray[3];
	fromyear = frommatchArray[4];
	
	today = tomatchArray[1]; // parse date into variables
	tomonth = tomatchArray[3];
	toyear = tomatchArray[4];
	
	if(currentyear>fromyear)
		{
			alert('StartDate Should Not Be Lessthan CurrentDate');
			return false;
		}
	else if (currentyear==fromyear)
			{
				if(currentmonth>frommonth)
					{
						alert('StartDate Should Not Be Lessthan CurrentDate');
						return false;
					}
				else if  (currentmonth==frommonth)
					{
						if (currentday>fromday)
							{
								alert('StartDate Should Not Be Lessthan CurrentDate');
								return false;
							}
						else
							{
								//return true;
							} 
						//return true;
					}
					//return true;
			}
	else 
		{
			//return true;
		}
	
	if(toyear<fromyear)
		{
			alert('StartDate Should Not Be Greaterthan EndDate');
			return false;
		}
	else if (toyear==fromyear)
			{
				if(tomonth<frommonth)
					{
						alert('StartDate Should Not Be Greaterthan EndDate');
						return false;
					}
				else if  (tomonth==frommonth)
					{
						if (today<fromday)
							{
								alert('StartDate Should Not Be Greaterthan EndDate');
								return false;
							}
						else
							{
								return true;
							} 
						return true;
					}
					return true;
			}
	else 
		{
			return true;
		}
}
function dateValidation(contentStDt,contentEdDt)
{ 

 
/* Validation For Content Date */

	//trim(contentStDt);
	//trim(contentEdDt);

    if(contentStDt.value == "")  { 
       alert('Start Date is Required');
       return false;
     }   
	   if(!isValidDate(contentStDt.value, "Start date is not in a valid format.") )
	  	 {
	      return false;
	  	 }
 
    
   
    if(contentEdDt.value == "")  { 
      
        alert('End Date is Required');
        return false;
       }   
	   if(!isValidDate(contentEdDt.value, "End date is not in a valid format.") )
	  	 {
	      return false;
	  	 } 
    
  if(!comparedate(contentStDt.value,contentEdDt.value))
  	{
	      return false;
	  	}
 
   return true;
    
}


function dateValidationContent(contentStDt,contentEdDt)
{ 

 
/* Validation For Content Date */

	//trim(contentStDt);
	//trim(contentEdDt);

    if(contentStDt.value == "")  { 
       alert('Start Date is Required');
       return false;
     }   
	   if(!isValidDate(contentStDt.value, "Start date is not in a valid format.") )
	  	 {
	      return false;
	  	 }
 
    
   
    if(contentEdDt.value == "")  { 
      
        alert('End Date is Required');
        return false;
       }   
	   if(!isValidDate(contentEdDt.value, "End date is not in a valid format.") )
	  	 {
	      return false;
	  	 } 
    
  if(!comparedateContent(contentStDt.value,contentEdDt.value))
  	{
	      return false;
	  	}
 
   return true;
    
}

function comparedateContent(fromdate,todate)
{
	var currentTime = new Date();

	var currentmonth = currentTime.getMonth() + 1
	var currentday = currentTime.getDate() 
	var currentyear = currentTime.getFullYear()
	
	
	var datePat = /^(\d{1,2})(\.)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	var frommatchArray = fromdate.match(datePat); // is the format ok?
	var tomatchArray = todate.match(datePat); // is the format ok?
	
	fromday = frommatchArray[1]; // parse date into variables
	frommonth = frommatchArray[3];
	fromyear = frommatchArray[4];
	
	today = tomatchArray[1]; // parse date into variables
	tomonth = tomatchArray[3];
	toyear = tomatchArray[4];

	
	
	var fromDate = new Date( fromyear,  frommonth - 1, fromday);
	var toDate = new Date( toyear, tomonth -1, today  );	

	
	if( fromDate <= currentTime  ) {
		alert('Start date should be greater than Current date');
		return false;
	} 
	
	if( fromDate >= toDate ) {
		alert('End date should be greater than Start date');
		return false;		
	}
	
	return true;
}
function isChecked(chkBoxObj)
{
	return chkBoxObj.checked;
}
function init() {

    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];        
        if (theInput.type == 'text') {  
            if (theInput.value != '') {
              var textvalue =  theInput.value;
				textvalue = textvalue.toLowerCase()
				var s = new String(textvalue);
				s = s.replace(/<script type=\"text\/javascript\">/g," ");
				s = s.replace(/<script>/g," ");
				s = s.replace(/<\/script>/g," ");	
				theInput.value = s;
				alert(theInput.value);
				var words=s.split("<script>"); //split using blank space as delimiter
				if(words.length > 1)
				{
					alert("<Script> tag is not allowed in " + theInput.id);
					return false;
				}
				var words1=s.split("\/script"); //split using blank space as delimiter
				if(words1.length > 1)
				{
					alert("<Script> tag is not allowed in " + theInput.id);
					return false;
				}
            }//inner if			
	     }//outer if
		}//for
}//init
function comparedateexchange(fromdate,todate)
{
	/*var currentTime = new Date();

	var currentmonth = currentTime.getMonth() + 1
	var currentday = currentTime.getDate()
	var currentyear = currentTime.getFullYear()
	*/
	
	var datePat = /^(\d{1,2})(\.)(\d{1,2})\2(\d{2}|\d{4})$/;
	
	var frommatchArray = fromdate.match(datePat); // is the format ok?
	var tomatchArray = todate.match(datePat); // is the format ok?
	
	fromday = frommatchArray[1]; // parse date into variables
	frommonth = frommatchArray[3];
	fromyear = frommatchArray[4];
	
	today = tomatchArray[1]; // parse date into variables
	tomonth = tomatchArray[3];
	toyear = tomatchArray[4];
	
	/*if(currentyear>fromyear)
		{
			alert('StartDate Should Not Be Lessthan CurrentDate');
			return false;
		}
	else if (currentyear==fromyear)
			{
				if(currentmonth>frommonth)
					{
						alert('StartDate Should Not Be Lessthan CurrentDate');
						return false;
					}
				else if  (currentmonth==frommonth)
					{
						if (currentday>fromday)
							{
								alert('StartDate Should Not Be Lessthan CurrentDate');
								return false;
							}
						else
							{
								//return true;
							} 
						//return true;
					}
					//return true;
			}
	else 
		{
			//return true;
		}
	*/
	if(toyear<fromyear)
		{
			alert('StartDate Should Not Be Greaterthan EndDate');
			return false;
		}
	else if (toyear==fromyear)
			{
				if(tomonth<frommonth)
					{
						alert('StartDate Should Not Be Greaterthan EndDate');
						return false;
					}
				else if  (tomonth==frommonth)
					{
						if (today<fromday)
							{
								alert('StartDate Should Not Be Greaterthan EndDate');
								return false;
							}
						else
							{
								return true;
							} 
						return true;
					}
					return true;
			}
	else 
		{
			return true;
		}
}





