/*
##########################################################################
# File Name: validation.js
# Created By: Bhavik Patel
# Created On: 13-08-2009
# Last Modified By: 
# Last modified On: 
# This file contains all the validation functions
########################################################################## 
*/



  	var arrPublicWeb = new Array(
                                "gmail.com",                                
                                "yahoo.com",                                
                                "yahoo.co.in",                                
                                "aol.com",
                                "hotmail.com",
                                "msn.com",
                                "rediffmail.com"                                                             
                              ); 
  
	/*
     #############################################################################
     # Function Name: showMessage()
     # Created By: Bhavik Patel  
     # Created on: 05-07-09
     # Purpose: alert message and focus on field
     # Parameters: message string to show and form field to focus. 
     #############################################################################
	 */

  
	function showMessage(strMsg, objField)
	{
	   alert(strMsg);
	   objField.focus();
	   return false;
	}

	/*
     #############################################################################
     # Function Name: isBlank()
     # Created By: PHP Team   
     # Created on: 12  Sep 2005
     # Purpose: Function to check wether given value is blank or not.    
     # Parameters: string strValue : string. 
     # ON SUCCESS: Returns TRUE if value is blank.     
     # ON FAILURE: Returns FLASE  if value is not blank.     
     #############################################################################
	 */

	 function isBlank(strValue)
	 {
		 for(var i = 0; i < strValue.length; i++)
		 {
			 var c = strValue.charAt(i);
			 if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
		 }
		 return true; 
	 }

	/*
	###############################################################
	# Function Name: isValidEmail
	# Created By: Aslesha
	# Created on: 3-6-2009
	# Purpose: Validates the email id
	# Parameters: 
	# ON SUCCESS: 
	# ON FAILURE: 
	#
	##############################################################    
	*/  
	
	function isValidEmail(strEmail) 
	{      
		strEmail = trim(strEmail);
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(strEmail) == false) 
		{
			alert('Invalid email address');
			return false;
		}
		else
		{    
			return true;
		}
	}
	
		
	function isValidEmailId(strEmail) 
	{      
		strEmail = trim(strEmail);
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(strEmail) == false) 
		{
			return false;
		}
		else
		{    
			return true;
		}
	}

/*
###############################################################
# Function Name: isValidWebAddress
# Created By: Aslesha
# Created on: 3-6-2009
# Purpose: Validates the email id
# Parameters: 
# ON SUCCESS: 
# ON FAILURE: 
#
##############################################################    
*/  

function isValidWebAddress(strWeb) {
   // var v = new RegExp();
  // v1.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
  // v.compile("^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");

 var v=/^(?:http:\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/i ;
   
// var v= /^(ftp|https?):\/\/+(www\.)?[a-z0-9\-\.]{3,}\.[a-z]{3}$/ ;
 //var regex =/^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ ;
   
    if (v.test(strWeb)) 
        return true;
	else 
		return false;
} 

function checkURL(value) {
var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
if(urlregex.test(value))
{
return(true);
}
return(false);
}


function onlyNumbers(evt)
{
    var e = window.event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

/*
Validation for float numbers
*/

function validateFloat(str) {
	return /^[-+]?[0-9]+(\.[0-9]+)?$/.test(str);
}

/*
###############################################################
# Function Name: strstr
# Created By: Aslesha
# Created on: 3-6-2009
# Purpose: Validates the email id
# Parameters: 
# ON SUCCESS: 
# ON FAILURE: 
#
##############################################################    
*/  

function strstr( haystack, needle, bool ) {
    
    var pos = 0;
    
    haystack += '';
    pos = haystack.indexOf( needle );
    if (pos == -1) {
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}
/*
###############################################################
# Function Name: isValidCorporateEmail
# Created By: Aslesha
# Created on: 3-6-2009
# Purpose: Validates the email id
# Parameters: 
# ON SUCCESS: 
# ON FAILURE: 
#
##############################################################    
*/  

function isValidCorporateEmail(strEmailHost) 
{   
   
    var arr2str = arrPublicWeb.toString(); 
    var intResult=arr2str.search(strEmailHost);    
      
    if(parseInt(intResult)>=0) 
    {
        return false;
    }
    else
	    return true; 
    
}


/*
################################################################################
# Function Name: trim
# Created By: Aslesha
# Created on:  
# Purpose:removes leading and trailing spaces from the string
# Parameters: No parametrs.
# ON SUCCESS: 
# ON FAILURE: 
#################################################################################    
*/     
function trim(s)
{
    return s.replace(/(^\s+)|(\s+$)/g, "");
}






function MWJ_findSelect( oName, oDoc ) { //get a reference to the select box using its name
	if( !oDoc ) { oDoc = window.document; }
	for( var x = 0; x < oDoc.forms.length; x++ ) { if( oDoc.forms[x][oName] ) { return oDoc.forms[x][oName]; } }
	for( var x = 0; document.layers && x < oDoc.layers.length; x++ ) { //scan layers ...
		var theOb = MWJ_findObj( oName, oDoc.layers[x].document ); if( theOb ) { return theOb; } }
	return null;
}
function dateChange( d, m, y ) {
	d = MWJ_findSelect( d ), m = MWJ_findSelect( m ), y = MWJ_findSelect( y );
	//work out if it is a leap year
	var IsLeap = parseInt( y.options[y.selectedIndex].value );
	IsLeap = !( IsLeap % 4 ) && ( ( IsLeap % 100 ) || !( IsLeap % 400 ) );
	//find the number of days in that month
	IsLeap = [31,(IsLeap?29:28),31,30,31,30,31,31,30,31,30,31][m.selectedIndex];
	//store the current day - reduce it if the new month does not have enough days
	var storedDate = ( d.selectedIndex > IsLeap - 1 ) ? ( IsLeap - 1 ) : d.selectedIndex;
	while( d.options.length ) { d.options[0] = null; } //empty days box then refill with correct number of days
	for( var x = 0; x < IsLeap; x++ ) { d.options[x] = new Option( x + 1, x + 1 ); }
	d.options[storedDate].selected = true; //select the number that was selected before
	if( window.opera && document.importNode ) { window.setTimeout('MWJ_findSelect( \''+d.name+'\' ).options['+storedDate+'].selected = true;',0); }
}
function setToday( d, m, y ) {
	d = MWJ_findSelect( d ), m = MWJ_findSelect( m ), y = MWJ_findSelect( y );
	var now = new Date(); var nowY = ( now.getYear() % 100 ) + ( ( ( now.getYear() % 100 ) < 39 ) ? 2000 : 1900 );
	//if the relevant year exists in the box, select it
	for( var x = 0; x < y.options.length; x++ ) { if( y.options[x].value == '' + nowY + '' ) { y.options[x].selected = true; if( window.opera && document.importNode ) { window.setTimeout('MWJ_findSelect( \''+y.name+'\' ).options['+x+'].selected = true;',0); } } }
	//select the correct month, redo the days list to get the correct number, then select the relevant day
	m.options[now.getMonth()].selected = true; dateChange( d.name, m.name, y.name ); d.options[now.getDate()-1].selected = true;
	if( window.opera && document.importNode ) { window.setTimeout('MWJ_findSelect( \''+d.name+'\' ).options['+(now.getDate()-1)+'].selected = true;',0); }
}
function checkMore( y, curBot, curTop, min, max ) {
	var range = curTop - curBot;
	if( typeof( y.nowBot ) == 'undefined' ) { y.nowBot = curBot; y.nowTop = curTop; }
	if( y.options[y.selectedIndex].value == 'MWJ_DOWN' ) { //they have selected 'lower'
		while( y.options.length ) { y.options[0] = null; } //empty the select box
		y.nowBot -= range + 1; y.nowTop = range + y.nowBot; //make note of the start and end values
		//adjust the values as necessary if we will overstep the min value. If not, refill with the
		//new option for 'lower'
		if( min < y.nowBot ) { y.options[0] = new Option('Lower ...','MWJ_DOWN'); } else { y.nowBot = min; }
		for( var x = y.nowBot; x <= y.nowTop; x++ ) { y.options[y.options.length] = new Option(x,x); }
		y.options[y.options.length] = new Option('Higher ...','MWJ_UP');
		y.options[y.options.length - 2].selected = true; //select the nearest number
		if( window.opera && document.importNode ) { window.setTimeout('MWJ_findSelect( \''+y.name+'\' ).options['+(y.options.length - 2)+'].selected = true;',0); }
	} else if( y.options[y.selectedIndex].value == 'MWJ_UP' ) { //A/A except upwards
		while( y.options.length ) { y.options[0] = null; }
		y.nowTop += range + 1; y.nowBot = y.nowTop - range;
		y.options[0] = new Option('Lower ...','MWJ_DOWN');
		if( y.nowTop > max ) { y.nowTop = max; }
		for( var x = y.nowBot; x <= y.nowTop; x++ ) { y.options[y.options.length] = new Option(x,x); }
		if( max > y.nowTop ) { y.options[y.options.length] = new Option('Higher ...','MWJ_UP'); }
		y.options[1].selected = true;
		if( window.opera && document.importNode ) { window.setTimeout('MWJ_findSelect( \''+y.name+'\' ).options[1].selected = true;',0); }
	}
}
function reFill( y, oBot, oTop, oDown, oUp ) {
	y = MWJ_findSelect( y ); y.nowBot = oBot; y.nowTop = oTop;
	//empty and refill the select box using the range of numbers specified
	while( y.options.length ) { y.options[0] = null; }
	if( oDown ) { y.options[0] = new Option('Lower ...','MWJ_DOWN'); }
	for( var x = oBot; x <= oTop; x++ ) { y.options[y.options.length] = new Option(x,x); }
	if( oUp ) { y.options[y.options.length] = new Option('Higher ...','MWJ_UP'); }
}

//Checks for min lenght
function validateMinLength(strString, intLength)
{
	if(strString.length < intLength) return false;
	else return true;
}