// ----------------------------------
// Check valid phone number
// ----------------------------------
function ValidatePhoneNo(ControlName)
{
	if (ControlName.value!="")
	{
		var re = /[^ 0-9-]/;
		if (re.test(ControlName.value))
		{
			alert("0 to 9 OR '-' is allowed in phone number.");
			ControlName.focus();				
		    	return false;
		}
	}
	return true;		
}

// ----------------------------------
// Check valid Time
// ----------------------------------
function ValidateTime(ControlName)
{	
	

	//if (!ValidateText('Time', ControlName)) return false;
	if (ControlName.value!="")
	{
		var re = /[^0-9:]/;
		if (re.test(ControlName.value))
		{
			alert("0 to 9 OR ':' is allowed in time.");
			ControlName.focus();				
		    	return false;
		}
	}
	return true;		
}



// Validates length of text boxes /areas
function ValidateTextLength(DisplayName, ControlName, xLength)
{
	if (ControlName.value.length > parseInt(xLength))
	{
		alert(DisplayName + " Should Not be Greater Then " + xLength + " Characters.");
		ControlName.focus();				
	    return false;
	}
	return true;		
}





/* --------------------------------------------------------
Function to validate Text boxes, Allows only characters
------------------------------------------------------------*/

function ValidateText(DisplayName, ControlName)
{
	var re = /^\s+\s+$/;
	
	if(containsInvalidChar(ControlName)==false)
	{
		
		alert(DisplayName + " Should Contain Valid Characters");
		ControlName.select();				
	    return false;
		
	}
	
	
	if ((ControlName.value == null) || (ControlName.value == "") || re.test(ControlName.value)||(trim(ControlName.value)==""))
	{
		alert(DisplayName + " Should Not be Null.");
		ControlName.value=""
		ControlName.focus();				
	    return false;
	}
	return true;		
}




/* --------------------------------------------------------
Function to validate Combo / List boxes, Cannot Leave Blank
------------------------------------------------------------*/

function ValidateCombo(DisplayName, ControlName)
{

	if ((ControlName.value == null) || (ControlName.value == "") || (trim(ControlName.value)==""))
	{
		alert("Please Select a " + DisplayName + ".");
		ControlName.focus();				
	    return false;
	}
	return true;		
}




function ValidateAlphaNum(DisplayName, ControlName)
{
	//var re = /^\s+\s+$/;
	var re = new RegExp("[^ \.'()+,A-Za-z0-9_-]+") ;
	
	if (re.test(ControlName.value))
	{
         alert(DisplayName + " can contain only A-Z, a-z, 0-9, . , _ , ( , + , ' , ) and - characters.") ;
         ControlName.select() ;
        return false ;
    }
	
	
	if ((ControlName.value == null) || (ControlName.value == "") || (trim(ControlName.value)==""))
	{
		alert(DisplayName + " Should Not be Null.");
		ControlName.value=""
		ControlName.focus();				
	    return false;
	}
	return true;		
}





String.prototype.trim = function(){
        return this.replace(/^\s*(\b.*\b|)\s*$/, "$1");
}

function trimAllFields(frm) {
	for (var i=0; i<frm.elements.length; i++) {
		if (frm.elements[i].type == 'text' || frm.elements[i].type == 'textarea') {
			frm.elements[i].value = frm.elements[i].value.trim();
		}
	}
}

function checkBlank(cntr, cntrName) {
        if(cntr.value == ""){
                alert(cntrName + " cannot be blank") ;
                cntr.focus() ;
                return false ;
        }
	return true;
}

function containtsWhiteSpaces(cntr, cntrName) {
        if(cntr.value.indexOf(' ') != -1){
                alert(cntrName + " cannot contain white spaces.") ;
                cntr.focus() ;
                return false ;
        }
	return true;
}

function containsInvalidChars(cntr, cntrName) {
var re1 = new RegExp("[^ \.,A-Za-z0-9_-]+") ;
      if (re1.test(cntr.value)) {
                alert(cntrName + " can contain only A-Z, a-z, 0-9, dot, _ and - characters.") ;
               cntr.focus() ;
                return false ;
        }
}

function containsInvalidChar(cntr)
{
	   var re1 = new RegExp("[^ \.',A-Za-z_-]+") ;
       if (re1.test(cntr.value))
	   {                
		  return false ;
       }
}


function checkEmailID(cntrName,cntr) {
	var re1 = new RegExp("[^ \.@A-Za-z0-9_-]+") ;
        if (re1.test(cntr.value)) {
                alert(cntrName + " can contain only A-Z, a-z, 0-9, dot, @, _ and - characters.") ;
                cntr.focus() ;
                return false ;
        }
        if ((cntr.value).indexOf('@') == -1) {
			alert(cntrName + ' should contain @ sign.');
			cntr.focus() ;
                return false;
        }
        if ((cntr.value).indexOf('@') == 0) {
			alert(cntrName + ' cannot begin with the @ sign.');
			cntr.focus() ;
                return false;
        }
        if ((cntr.value).indexOf('.') == -1) {
			alert(cntrName + ' should contain dot sign.');
			cntr.focus() ;
            return false;
        }
        if ((cntr.value).indexOf('.') == 0) {
			alert(cntrName + ' cannot begin with the dot sign.');
			cntr.focus() ;
    		return false;
        }
        var at_pos = (cntr.value).indexOf('@');
        var at_pos_last = (cntr.value).lastIndexOf('@');
        var dot_pos = (cntr.value).lastIndexOf('.');
        if (parseInt(dot_pos) - parseInt(at_pos) == 1) {
			alert(cntrName + ' cannot have the dot sign immediately after the @ sign.');
                cntr.focus() ;
				return false;
        }
        if (parseInt(at_pos) - parseInt(dot_pos) == 1) {
				alert(cntrName + ' cannot have the dot sign immediately before the @ sign.');
                cntr.focus() ;
				return false;
        }
	if (parseInt(at_pos_last) == cntr.value.length) {
				alert(cntrName + ' cannot end with a @ sign .');
				cntr.focus() ;
                return false;
	}
	if (parseInt(dot_pos) == cntr.value.length) {
			alert(cntrName + ' cannot end with a dot sign .');
			cntr.focus() ;
                return false;
	}
        return true;
}


function validateDate(cntrName, cntr, dd, mm, yyyy)
{
	var e_m, e_d, e_y;

	e_m = mm.options[mm.selectedIndex].value;
	e_d = dd.options[dd.selectedIndex].value;
	e_y = yyyy.options[yyyy.selectedIndex].value;

	var e_time = checkDate(e_m, e_d, e_y, 0, 0, 0);
	if (e_time == -1) {
		alert("Invalid date in " + cntrName + ". Please re-enter the date.");
		return false;
	} else {
		return true;
	}
}

function checkDate(mon, day, year, hour, min, sec)
{
	var date = new Date(year - 1900, mon - 1 , day, hour, min, sec);

	var dd = date.getDate();
        var mm = date.getMonth();
        var yy = 1900 + date.getYear();

        if ((dd == day) && (mm == mon - 1) & (yy == year)) {
	        return 0;
        } else {
	        return -1;
	}
}

function isToGreaterThanFrom(f_d, f_m, f_y, t_d, t_m, t_y) {
	var ef_m, ef_d, ef_y;
	var et_m, et_d, et_y;

	ef_m = f_m.options[f_m.selectedIndex].value;
	ef_d = f_d.options[f_d.selectedIndex].value;
	ef_y = f_y.options[f_y.selectedIndex].value;

	et_m = t_m.options[t_m.selectedIndex].value;
	et_d = t_d.options[t_d.selectedIndex].value;
	et_y = t_y.options[t_y.selectedIndex].value;

	var ef_time = getTimeInMilliSeconds(ef_m, ef_d, ef_y, 0, 0, 0);
	var et_time = getTimeInMilliSeconds(et_m, et_d, et_y, 0, 0, 0);

	if ((parseInt(et_time) - parseInt(ef_time)) < 0) {
		return false;
	}	
	return true;
}

function getTimeInMilliSeconds(mon, day, year, hour, min, sec)
{
	var date = new Date(year, mon - 1 , day, hour, min, sec);
	return date.getTime();
}

// -----------------------------------------------------------------
// Check whether value is a positive integer (WILL NOT ACCEEPT ZERO)
// -----------------------------------------------------------------
 
function ValidatePositiveInteger(DisplayName, ControlName)
{
		if (!ValidateNumber(DisplayName, ControlName))
		{
		 ControlName.focus();
		 return false;
		} 

        if (parseInt(ControlName.value) < 0)
        {
			alert("Negative Value Not Allowed.");
			ControlName.focus();
			return false;
        }
        return true;
}

// -----------------------------------------------------------------
// Check whether value is a positive integer (WILL ACCEEPT ZERO)
// -----------------------------------------------------------------

function ValidateInteger(DisplayName, ControlName)
{
		if (!ValidateNum(DisplayName, ControlName)) return false;

        if (parseInt(ControlName.value) <= 0)
        {
			alert("Negative Value Not Allowed.");
			ControlName.focus();
			return false;
        }
        return true;
}


// ----------------------
// Check value as number 
// ----------------------
 
function ValidateNum(DisplayName, ControlName)
{
        if (isNaN(parseFloat(ControlName.value)))
        {
                alert(DisplayName + " should be a Valid Number.");
				ControlName.focus();
                return false;
        }
        if (isNaN(ControlName.value))
        {
                alert(DisplayName + " should be a Valid Number.");
				ControlName.focus();
                return false;
        }
               
        return true;
}




// ----------------------
// Check value as number checks for 0 as well
// ----------------------
 
function ValidateNumber(DisplayName, ControlName)
{
        
         if(parseInt(ControlName.value)< 0)
        {
			alert(DisplayName + " should be a Valid Number.");
			ControlName.focus();
            return false;
        }
        
        if (isNaN(parseInt(ControlName.value)))
        {
                alert(DisplayName + " should be a Valid Number.");
				ControlName.focus();
                return false;
        }
        if (isNaN(ControlName.value))
        {
                alert(DisplayName + " should be a Valid Number.");
				ControlName.focus();
                return false;
        }
        if (trim(ControlName.value)=="0")
        {
                alert(DisplayName + " should be a Valid Number.");
				ControlName.focus();
                return false;
        }
        
        return true;
}




// ----------------------
// Check for a valid Integer
// ----------------------
 
function ValidateInteger(DisplayName, ControlName)
{
        if(ControlName.value.indexOf('.')!= -1)
        {
			alert(DisplayName + " should be a Valid Integer.");
			ControlName.focus();
            return false;
        }
        
        if(parseInt(ControlName.value)< 0)
        {
			alert(DisplayName + " should be a Valid Integer.");
			ControlName.focus();
            return false;
        }
        
        if (isNaN(parseInt(ControlName.value)))
        {
                alert(DisplayName + " should be a Valid Integer.");
				ControlName.focus();
                return false;
        }
        if (isNaN(ControlName.value))
        {
                alert(DisplayName + " should be a Valid Integer.");
				ControlName.focus();
                return false;
        }
        if (trim(ControlName.value)=="0")
        {
                alert(DisplayName + " should be a Valid Integer.");
				ControlName.focus();
                return false;
        }
        
        return true;
}





function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}


function getMonthName(month)
{
	var mnthName

	switch (month)
	{
		case 1 :
		 		mnthName = "Jan"
		 		break;
				
		case 2 :
		 		mnthName = "Feb"
		 		break;
				
	    case 3 :
				mnthName = "Mar"
		 		break;
		
		case 4 :
				mnthName = "Apr"
		 		break;
				
		case 5 :
				mnthName = "May"
		 		break;
				
		case 6 :
				mnthName = "Jun"
		 		break;									
				
		case 7 :
				mnthName = "Jul"
		 		break;		
				
		case 8 :
				mnthName = "Aug"
		 		break;		
				
		case 9 :
				mnthName = "Sep"
		 		break;		
				
		case 10 :
				mnthName = "Oct"
		 		break;		
				
		case 11 :
				mnthName = "Nov"
		 		break;		
				
		case 12 :
				mnthName = "Dec"
		 		break;	
	
	}

	return mnthName
		 
}


function getMonth(monthName)
{
	var month

	switch (monthName)
	{
		case "Jan" :
		 		month = "1"
		 		break;
				
		case "Feb" :
		 		month = "2"
		 		break;
				
	    case "Mar" :
				month = "3"
		 		break;
		
		case "Apr" :
				month = "4"
		 		break;
				
		case "May" :
				month = "5"
		 		break;
				
		case "Jun" :
				month = "6"
		 		break;									
				
		case "Jul" :
				month = "7"
		 		break;		
				
		case "Aug" :
				month = "8"
		 		break;		
				
		case "Sep" :
				month = "9"
		 		break;		
				
		case "Oct" :
				month = "10"
		 		break;		
				
		case "Nov" :
				month = "11"
		 		break;		
				
		case "Dec" :
				month = "12"
		 		break;	
	
	}

	return month
		 
}


function Mod(a, b) { return a-Math.floor(a/b)*b }

