/*
 * Function    : changewidth
 * Description : This function is used to adjust the width of the container when the user scrolls inside a mailbox.
 * Inputs      : None
 * Returns     : None
 */ 


 var illegalChars = /[\W_]/;

 function validatestate(fld) //user name validation
{
	if(illegalChars.test(fld.value))
	{
		alert("Special characters or Spaces are not allowed in the State");        
		return false
	}
	else
	{
		return true
	}
}
 function validatecity(fld) //user name validation
{
	if(illegalChars.test(fld.value))
	{
		alert("Special characters or Spaces are not allowed in the City");        
		return false
	}
	else
	{
		return true
	}
}

var tomatch= /http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/   
function validateUsername(fld) //user name validation
{
	if (fld.value == "")	
	{
	    alert("Please enter Full Name"); 
		return false
    }
	if ((fld.value.search(/\$/)!=-1)||(fld.value.search(/\*/)!=-1)||(fld.value.search(/\^/)!=-1)||(fld.value.search(/\#/)!=-1)||(fld.value.search(/\!/)!=-1)||(fld.value.search(/\@/)!=-1)||(fld.value.search(/\%/)!=-1)||(fld.value.search(/\&/)!=-1)||(fld.value.search(/\?/)!=-1)||(fld.value.search(/\+/)!=-1)||(fld.value.search(/\=/)!=-1)||(fld.value.search(/\|/)!=-1)||(fld.value.search(/\-/)!=-1)||(fld.value.search(/\}/)!=-1)||(fld.value.search(/\]/)!=-1)||(fld.value.search(/\[/)!=-1)||(fld.value.search(/\}/)!=-1)||(fld.value.search(/\~/)!=-1)||(fld.value.search(/\\/)!=-1)||(fld.value.search(/\//)!=-1)||(fld.value.search(/\)/)!=-1)||(fld.value.search(/\(/)!=-1))
	{
		alert("special characters are not allowed in the Full Name");        
		return false
	}
	if ((fld.value.length < 5)) 
	{
        alert("Full Name Minimum 5 Characters"); 		
	    return false

    }	
	else if ((fld.value.length > 30)) 
	{ 
        alert("Full Name cannot be More than 30 characters"); 
	    return false
	}
	else
	{
		return true
    }
}


function validatePasswordField(fld) //user name validation
{
	
	if ((fld.value.search(/\$/)!=-1)||(fld.value.search(/\*/)!=-1)||(fld.value.search(/\^/)!=-1)||(fld.value.search(/\#/)!=-1)||(fld.value.search(/\!/)!=-1)||(fld.value.search(/\@/)!=-1)||(fld.value.search(/\%/)!=-1)||(fld.value.search(/\&/)!=-1)||(fld.value.search(/\?/)!=-1)||(fld.value.search(/\+/)!=-1)||(fld.value.search(/\=/)!=-1)||(fld.value.search(/\|/)!=-1)||(fld.value.search(/\-/)!=-1)||(fld.value.search(/\}/)!=-1)||(fld.value.search(/\]/)!=-1)||(fld.value.search(/\[/)!=-1)||(fld.value.search(/\}/)!=-1)||(fld.value.search(/\~/)!=-1)||(fld.value.search(/\\/)!=-1)||(fld.value.search(/\//)!=-1)||(fld.value.search(/\)/)!=-1)||(fld.value.search(/\(/)!=-1))
	{
		alert("special characters are not allowed in the Password");        
		return false
	}
	if ((fld.value.length < 6)) 
	{
        alert("Password Minimum 6 Characters"); 		
	    return false

    }	
	else if ((fld.value.length > 20)) 
	{ 
        alert("Password cannot be More than 20 characters"); 
	    return false
	}
	else
	{
		return true
    }
}


/*************************************************************Ending Validate UserName*************************************************/




function validatepostadd(formObj)
{
	
	if( formObj.category.selectedIndex == 0 )
		{
		
			alert( "Select Category " )
			formObj.category.focus( );
			return false;
			
		}
		else if( formObj.add_title.value  == "" ){

			alert( "Title cannot be empty" )
			formObj.add_title.focus( );
			return false;
		}
		else if( formObj.add_description.value  == "" ){

			alert( "Description cannot be empty" )
			formObj.add_description.focus( );
			return false;
		}
		
		else if( formObj.contact.value ==0)
		{
			alert( "Phone no cannot be empty" )
			formObj.contact.focus( );
			return false;
		}
		else if( isNaN(formObj.contact.value ) == true )
		{
			alert( "Phone no can be number" )
			formObj.contact.focus( );
			return false;	
		}
		
		else if(formObj.startdate.value!='')
		{			
			if(!checkdate(formObj.startdate))
			{
				formObj.startdate.focus();
				return false;
			}			
			if( formObj.enddate.value  == "" )
			{
				alert( "End date cannot be empty" )
				formObj.enddate.focus( );
				return false;
			}		
			if(formObj.enddate.value!='')
			{			
				if(!checkdate(formObj.enddate))
				{
					formObj.enddate.focus();
					return false;
				}			
			}
			if(!dateCompare(formObj.startdate,formObj.enddate))
			{
				formObj.enddate.focus();
				return false;
			}
			if(formObj.novolunteer.value!='')
			{
				if(isNaN(formObj.novolunteer.value)==true)
				{
					alert('The Volunteers Can be Number only');
					formObj.novolunteer.focus();
					return false;
				}					
			}	
			
		}
		else if(formObj.enddate.value!='')
		{			
				alert('Please enter the start date');
				formObj.startdate.focus();	
				return false;
		}
		else if(formObj.novolunteer.value!='')
		{			
				alert('Please enter the start date');
				formObj.startdate.focus();	
				return false;
		}
		else
		{
			return true;
		}

}
	
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
       
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
  
    return true;
}

function stripCharsInBag(s, bag)
	{
	var i;
    var 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){
	
    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 date(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=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 : dd-mm-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
}

function enable()
{
   var i=document.getElementById('check');
   if(i.checked==true)
   {
		document.getElementById('open').disabled=false;
   }
   else
   {	
		document.getElementById('open').disabled=true;
   }
   
}

function registerEnable()
{
   var i=document.getElementById('check');
     if(i.checked==true)
   {
		document.getElementById('registration').disabled=false;
   }
   else
   {	
		document.getElementById('registration').disabled=true;
   }
   
}

function replyEnable()
{
   var i=document.getElementById('check');
   if(i.checked==true)
   {
		document.getElementById('send').disabled=false;
   }
   else
   {	
		document.getElementById('send').disabled=true;
   }
   
}

/*************************************************************Ending Validate Password*************************************************/


function validateUserpwd(fld) {  // password validation

    var illegalChars = "/\W/-_*?"; 

    if (fld.value == "") {

        alert("please enter username");
		fld.userPassword.focus();
		return 0;

    } else if ((fld.value.length < 5)) {


        alert("The Username cannot be less than 5 characters"); 
		fld.userPassword.focus();
	    return 0;


    }else if ((fld.value.length > 15)) {


        alert("The Username cannot be More than 15 characters"); 
		fld.userPassword.focus();
	    return 0;

	}else if (illegalChars.match(fld.value)) {
		alert("special characters are not allowed in the password");
        fld.userPassword.focus();
		return 0;
      

    } else {

        return 1;
    }
}

/*************************************************************Ending Validate Password*************************************************/


/*************************************************************Starting trim functions*************************************************/

function trim(str, chars){  //trim function to strip the white spaces
    
	return ltrim(rtrim(str, chars), chars);

}

function ltrim(str, chars){ 
    
	chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");

}

function rtrim(str, chars){
    
	chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");

}

function openpopup( popurl ){

	var winpops=window.open(popurl,"","width=550px,height=500px,scrollbars=yes");

}

/*************************************************************Ending trim functions*************************************************/

/********************User Update Validation Function Starts*********************/

/********************User Update Validation Function Ends*********************/
/*************************************************************Validating Email*************************************************/

function validateEmail( emailString ){  //Email Validation some string must be filled..

		str = emailString;

		var at="@" 
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		
		


		if (str[lstr-1]=="."){
		   alert("Invalid E-mail ID")
		   return false
		}
		
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}
		
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}
		
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}
		
		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		 
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }
		 
		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		 
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		if ((str.search(/\$/)!=-1)||(str.search(/\*/)!=-1)||(str.search(/\^/)!=-1)||(str.search(/\#/)!=-1)||(str.search(/\!/)!=-1)||(str.search(/\%/)!=-1)||(str.search(/\&/)!=-1)||(str.search(/\?/)!=-1)||(str.search(/\+/)!=-1)||(str.search(/\=/)!=-1)||(str.search(/\|/)!=-1)||(str.search(/\-/)!=-1)||(str.search(/\}/)!=-1)||(str.search(/\]/)!=-1)||(str.search(/\[/)!=-1)||(str.search(/\}/)!=-1)||(str.search(/\~/)!=-1)||(str.search(/\\/)!=-1)||(str.search(/\//)!=-1)||(str.search(/\)/)!=-1)||(str.search(/\(/)!=-1))
	{
		alert("special characters are not allowed in the email id");
        
		return false
	}

 		 return true;
}

/*************************************************************Ending Email Page*************************************************/

/*************************************************************Starting Login Validate Page*************************************************/

function validateLoginPage( formObj ){  //Login Page validation

	

	 if( formObj.email.value  == "" ){

		alert( "Email cannot be empty" )
		formObj.email.focus( );
		return false;


	}
	
	else if(!validateEmail( trim( formObj.email.value ) ) ){
	
		formObj.email.focus( );
		return false;
	}
	
		
	else if( trim( formObj.passbox.value ) == 0 ){

		alert( "Password cannot be empty" )
		formObj.passbox.focus( );
		return false;

	}
	
	else{

		return true;

	}

}

/*************************************************************Ending Login Validate Page*************************************************/

/*************************************************************Starting SignUp Page*************************************************/

function validateSignUpPage( formObj ) //validing registeration page
	
	{  
		
		if( formObj.country.value ==''|| formObj.country.value =='Select a Country')
		{	
			
            alert( "Country cannot be empty" )
			formObj.country.focus( );
			return false;
		}
		
		else if(formObj.country.value !="Others")
		{
			if(formObj.state.value!='Others')
			{
				if(formObj.city.value==''||formObj.city.value =='Select a City')
				{					
					alert( "City cannot be empty" );				
					formObj.city.focus( );
					return false;
				}
			}
			else
			{

			}

		}
		
		if(formObj.user_name.value  == '' )
		{

			alert( "Full Name cannot be empty" )
			formObj.user_name.focus( );
			return false;
		}

		else if( !validateUsername( formObj.user_name )){

			return false;
			fld.user_name.focus();
		}

		else if( formObj.email.value  == "" )
		{
			alert( "Email cannot be empty" )
			formObj.email.focus( );
			return false;
		}
		else if(!validateEmail( trim( formObj.email.value ) ) )
		{
		
			formObj.email.focus( );
			return false;
		}	
		
		else if( trim( formObj.pass_word.value ) == 0 ){

			alert( "Password cannot be empty" )
			formObj.pass_word.focus( );
			return false;

		}
		else if( !validatePasswordField( formObj.pass_word ) )
		{

			
			formObj.pass_word.focus( );
			return false;

		}

		else if(!formObj.gender[0].checked && !formObj.gender[1].checked)
		{
		alert( "select gender type" )
		formObj.gender[0].focus();
		return false;
		}
		else if(!formObj.memtype[0].checked && !formObj.memtype[1].checked)
		{
		alert( "select Member type" )
		formObj.memtype[0].focus();
		return false;
		}

		
		else if( formObj.phono.value ==0)
		{
			alert( "Phone No cannot be empty" )
			formObj.phono.focus( );
			return false;
		}
		else if( isNaN(formObj.phono.value ) == true )
		{
			alert( "Phone No can be Number" )
			formObj.phono.focus( );
			return false;
			
		}
		else if( formObj.address.value ==0)
		{
			alert( "Address cannot be empty" )
			formObj.address.focus( );
			return false;
			
		}
		else if( formObj.zipcode.value ==0)
		{
			alert( "Zip code cannot be empty" )
			formObj.zipcode.focus( );
			return false;
			
		}
		else if( isNaN(formObj.zipcode.value ) == true )
		{
			alert( "Zip code can be Number" )
			formObj.zipcode.focus( );
			return false;
			
		}
		else if(formObj.zipcode.value.length<5||formObj.zipcode.value.length>6)
		{
			alert( "Zip code can be 5 or 6 Numbers" )
			formObj.zipcode.focus( );
			return false;
			
		}
		else if(formObj.user_photo.value)
		{
			var a=formObj.user_photo.value;
			var temp=a.split(".");
			var len=temp.length-1;
			if(temp[len]=="jpg"||temp[len]=="png")
			{
			} 
			else
			{
				alert("Only .jpg and .png files allowed!!");
				return false;
			}
		}
		else if( formObj.security.value ==0)
		{
			alert( "Please enter the above security code" )
			formObj.security.focus( );
			return false;
		}
		
		else
		{
			return true;
		}

}

/*************************************************************End SignUp Page*************************************************/


/*************************************************************Starting validate Edit Page*************************************************/


function validateEditPage( formObj )  //validing user edit page
	
	{ 
		
		if(formObj.user_name.value  == '' )
		{

			alert( "Full Name cannot be empty" )
			formObj.user_name.focus( );
			return false;
		}

		else if( !validateUsername( formObj.user_name )){

			return false;
			fld.user_name.focus();
		}

		else if( formObj.email.value  == "" )
		{
			alert( "Email cannot be empty" )
			formObj.email.focus( );
			return false;
		}
		else if(!validateEmail( trim( formObj.email.value ) ) )
		{
		
			formObj.email.focus( );
			return false;
		}	
		
		else if( trim( formObj.pass_word.value ) == 0 ){

			alert( "Password cannot be empty" )
			formObj.pass_word.focus( );
			return false;

		}
		else if( !validatePasswordField( formObj.pass_word ) )
		{			
			formObj.pass_word.focus( );
			return false;
		}

		else if(!formObj.gender[0].checked && !formObj.gender[1].checked)
		{
			alert( "select gender type" )
			formObj.gender[0].focus();
			return false;
		}
		else if(!formObj.memtype[0].checked && !formObj.memtype[1].checked)
		{
			alert( "select Member type" )
			formObj.memtype[0].focus();
			return false;
		}

		
		else if( formObj.phono.value ==0)
		{
			alert( "Phone No cannot be empty" )
			formObj.phono.focus( );
			return false;
		}
		else if( isNaN(formObj.phono.value ) == true )
		{
			alert( "Phone No can be Number" )
			formObj.phono.focus( );
			return false;
			
		}
		else if( formObj.address.value ==0)
		{
			alert( "Address cannot be empty" )
			formObj.address.focus( );
			return false;
			
		}
		else if( formObj.zipcode.value ==0)
		{
			alert( "Zip code cannot be empty" )
			formObj.zipcode.focus( );
			return false;
			
		}
		else if( isNaN(formObj.zipcode.value ) == true )
		{
			alert( "Zip code can be Number" )
			formObj.zipcode.focus( );
			return false;
			
		}
		else if(formObj.zipcode.value.length<5||formObj.zipcode.value.length>6)
		{
			alert( "Zip code can be 5 or 6 Numbers" )
			formObj.zipcode.focus( );
			return false;
			
		}
		else if(formObj.user_photo.value)
		{
			var a=formObj.user_photo.value;
			var temp=a.split(".");
			var len=temp.length-1;
			if(temp[len]=="jpg"||temp[len]=="png")
			{
			} 
			else
			{
				alert("Only .jpg and .png files allowed!!");
				return false;
			}
		}
		else
		{
			return true;
		}
				
}

/* Ajith - Testing delete  Confirmation */

function MsgOkCancel(temp)
{
document.getElementById('deletereq').value=temp;
var fRet;
fRet = confirm('Are you sure you want to delete this request?');
if(fRet==true)
{
document.deleterequest.submit();
}
}



/*****************************************DD-MMM-YYYY Validation***********************************/


function checkdate(zulu)
{
	
	var str=zulu.value;	
	var day, month, year, s1, s2;
	var mod;
	day= str.substr(0,2);
	temp= str.substr(3);

	month= str.substr(3,3);

	year= str.substr(7,4);
	s1= str.substr(2,1);
	s2= str.substr(6,1);
	mod= (year % 4);
	
	if (isNaN(day)== true)
	{
		alert("Day should be number only");
		zulu.focus();
		return false;
	}
	else if ( (day.length) != 2 )
	{
		alert("Day should be greater 2 digit number ");
		zulu.focus();
		return false;
	}
	else if ( ((day.length) == 2) && (day < 0 || day > 31) )
	{
		alert("Invalid Day");
		zulu.focus();
		return false;
	}
	else if (s1!="-")
	{
		alert("The Seperator must be '-'");
		zulu.focus();
		return false;
	}
	else if (isNaN(month) == false)
	{
		alert("Invalid Month ");
		zulu.focus();
		return false;
	}
	else if ( temp.length > 8 )
	{
		alert("Month Can be jan,feb,mar,...dec ");
		zulu.focus();
		return false;
	}
	else if ( (isNaN(month) == true) && (month.length != 3) )
	{
		alert("Month Can be jan,feb,mar,...dec ");
		zulu.focus();
		return false;
	}
	
	else if ( (isNaN(month) == true) && (month.length == 3) && ((month != 'jan') && (month != 'feb') && (month != 'mar') && (month != 'apr') && (month != 'may') && (month != 'jun') && (month != 'jul') && (month != 'aug') && (month != 'sep') && (month != 'oct') && (month != 'nov') && (month != 'dec') && (month != 'JAN') && (month != 'FEB') && (month != 'MAR') && (month != 'APR') && (month != 'MAY') && (month != 'JUN') && (month != 'JUL') && (month != 'AUG') && (month != 'SEP') && (month != 'OCT') && (month != 'NOV') && (month != 'DEC') && (month !='Jan') && (month != 'Feb') && (month != 'Mar') && (month != 'Apr') && (month != 'May') && (month != 'Jun') && (month != 'Jul') && (month != 'Aug') && (month != 'Sep') && (month != 'Oct') && (month != 'Nov') && (month != 'Dec')) )
	{
		
		alert("Invalid Month");
		zulu.focus();
		return false;
	}
	else if ( (day > 30) && ( (month== "apr") || (month== "APR") || (month== "Apr") || (month== "jun") || (month== "JUN") || (month== "Jun") || (month== "sep") || (month== "SEP") || (month== "Sep") ||(month== "nov") || (month== "NOV") || (month== "Nov")) )
	{
		
		alert("Day Cannot be Greater than 30 for the Month of " + month + "  ");
		return false;
	}
	
	else if (s2 != "-")
	{
		alert("The Seperator must be '-'");
		zulu.focus();
		return false;
	}
	else if ( isNaN(year) == true )
	{
		alert("Invalid Year ");
		zulu.focus();
		return false;
	}
	else if ( isNaN(year)== false && (year.length != 4 ) )
	{
		alert("Invalid Year  ");
		zulu.focus();
		return false;
	}
	else if ( (isNaN(year)== false) && (year.length == 4 ) && ((year % 4)== 0) && ((month== "feb") || (month== "FEB")) && (day > 28) )
	{
		
		alert("Day Cannot be Greater than 28 for the Month of " + month + " in Leap Year ");
		zulu.focus();
		return false;
	}
	else if ( (isNaN(year)== false) && (year.length == 4 ) && ((year % 4) != 0) && ((month== "feb") || (month=="FEB")) && (day > 29) )
	{
		alert("Day Cannot be Greater than 29 for the Month of " + month + " in Leap Year");
		zulu.focus();
		return false;
	}
	else
	{
		return true;
	}

}


function validateforgotPassword(formObj)
{
	
	    if(formObj.email.value  == "" )
		{
			alert( "Email cannot be empty" )
			formObj.email.focus( );
			return false;
		}
		else if(!validateEmail(formObj.email.value ))
		{
			formObj.email.focus( );
			return false;
		}	
		else
		{
			return true;
		}
}

function replyMsg(formObj)
{
	if(formObj.message.value  == "" )
		{
			alert( "The empty message should not allowed" )
			formObj.message.focus( );
			return false;
		}
}


function dateCompare(dt1, dt2)
{

var str=dt1.value;	
var str1=dt2.value;	
var day= str.substr(0,2);
var month= str.substr(3,3);
var year= str.substr(7,4);
var day1= str1.substr(0,2);
var month1= str1.substr(3,3);
var year1= str1.substr(7,4);
var date1= month  + ' ' + day + ',' +year;
var date2= month1  + ' ' + day1 + ',' +year1;
var dt1_dt = Date.parse(date1);
var dt2_dt = Date.parse(date2);
	if (dt1_dt > dt2_dt)
	{
		alert("The end date should not less than start date");
		dt2.focus();
		return false;
	} 
	else
	{
		return true;
	}
} 