//[# $Id: isValid.js 10070 2009-08-10 13:50:59Z ankita $ #]
//[# $HeadURL: http://svn.monsterindia.noida/svn/monster-prod/pages/gov/itc/html/itc/isValid.js $ #]

//<input type="text" name="full_name"  title='Name' //id="validate-ValidName-AllowAlphaNum-AllowDigitsOnly-ValidPhone-ValidAddress-Radio-CheckBox-Dropdown">

var Valid=false;


function is_Email(string) 
{ //validate alphabets and dot and'// 
	
	var isAlpha = /^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/gi;
	
	if (string.search(isAlpha)==-1)
	{
		return false;
	}
	return true;
}

function is_Alpha(string) 
{ //validate alphabets and dot and'// 
	var isAlpha = /(^[a-zA-Z\s\.\']+)$/;
	if (string.search(isAlpha)==-1)
	{
		return false;
	}
	return true;
}
//validate alphabets and dot and'// 
function is_Alphanum(string) 
{ //validate for alphabtes and numaric characters//
	var isAlpha = /(^[a-zA-Z0-9\s]+)$/;
	if (string.search(isAlpha)==-1)
	{
		return false;
	}
	return true;
} 
//validate for numbers only//
function isNumber(strng) 
{
	var re10digit=/^([0-9]+)$/ ;
	if (strng.search(re10digit)==-1)
	{
		return false;
	}
	return true;
}

function validation(string) 
{
	if (string == '')
	{
		return false;
	}
	return true;
}
function userName(string)
{
	var isUser = /(^[a-zA-Z0-9\.\_]+)$/;
	if (string.search(isUser)==-1)
	{
		return false;
	}
	return true;
}
function checklength(title,string,more,less)
{
	if(more!= '' && less != '' && (string.length < less || string.length > more))
	{
		MJS.SKR.showAlertMsg({MSG:less + " " + document.getElementById('to').value +" " +more+" "+document.getElementById('char_bet').value+" "+title});
		return false;
	}
	else if(less != '' && string.length < less)
	{
		MJS.SKR.showAlertMsg({MSG:title+" "+document.getElementById('char_more').value+" "+less+" "+document.getElementById('chars').value});
		return false;
	}
	else if(more!= '' && string.length > more)
	{
		MJS.SKR.showAlertMsg({MSG:title+" "+document.getElementById('char_less').value+" "+more+" "+document.getElementById('chars').value});
		return false;
	}
	return true;
}
function checkSpaces(string)
{
	 var invalid = " ";
     // Check for white space
    if (string.indexOf(invalid) > -1) 
	{
		return false;
	}
	return true;	
}

//validate for phone number //
function isPhone(strng) 
{
        var re10digit=/^([0-9\-\+ \/]+)$/ ;
        if (strng.search(re10digit)==-1)
        {
			return false;
        }
        return true;
}

//Remove the spaces in front and back of the string//
function TrimValue(str)
{
        string = str.replace(/^(\s)*/, '');
        string = string.replace(/(\s)*$/, '');
        return string;
}
//validte address for the form//
function is_Add(string) 
{
        if (!string) return false;
        var iChars = "*^~%<>";
        for (var i = 0; i < string.length; i++) 
        {
			if (iChars.indexOf(string.charAt(i)) != -1)
			return false;
        }
        return true;
}

function RadioCheckbox(string) 
{
        var buttonGroup=string;
        var check=0;
        if (buttonGroup[0]) 
        { // if the button group is an array (one button is not an array)
			for (var ii=0; ii<buttonGroup.length; ii++) 
			{
				if (buttonGroup[ii].checked == true) 
				{
						check++;
				}
			}
        } 
        else 
        {
			if (buttonGroup.checked) 
			{ 
					check++;
			}
        }
        return check;
}
function is_Decimal(string) {
		
        var isDecimal = /(^\d{0,3}(\.\d{0,2})?$)|(^\d{3}$)/
        if (string.search(isDecimal)==-1)
        {
			return false;

        }
		else if(string > 100)
		{
			return false;
		}
return true;
}

// Function for personnel checks

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
		{
			return radioObj.value;
		}
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function isMobile(strng)
{
	 var re10digit=/^\d{10}$/ ;
        if (strng.search(re10digit)==-1)
        {
			return false;
        }
        return true;
}

function ExtraChecksForgotPwd()
{
	if(document.forgot_pwd.email.value == '' && document.forgot_pwd.mobile.value == '')
	{
		MJS.SKR.showAlertMsg({MSG:document.getElementById('mbl_add').value, FOCUS_OBJ: document.forgot_pwd.email.focus()});
		return false;
	}
}

function ExtraChecks()
{
        var j=0;
        var k=0;
        for(var i=0;i<document.app_form.skill.options.length;i++)
        {
                        if(document.app_form.skill.options[i].selected)
                        {
                                j++;
                                if(document.app_form.skill.options[i].value == '110')
                                {
                                        k=1;
                                }
                        }
        }
        if(k == '1' && document.app_form.other_skill.value == '')
        {
                        alert('Other skill is required.');
                        document.app_form.other_skill.focus();
                        return false;
        }
        if(j > 2)
        {
                alert('Maximum 2 options can be selected.');
                document.app_form.skill.focus();
                return false;
        }
        if(document.app_form.qual.value == '27' && document.app_form.other_qual.value == '')
        {
                alert('Other qualification is required');
                document.app_form.otherQual.focus();
                return false;
        }
        var otherLangChk = RadioCheckbox(document.app_form.oth_lang2);
        if((document.app_form.oth_lang1.value == '' && otherLangChk > 0) || (document.app_form.oth_lang1.value != '' && otherLangChk == 0))
        {
                alert('Other language is required.');
                document.app_form.oth_lang1.focus();
                return false;
        }

}



////////////////// check with current date..............
function chkCurDate( chkYear, chkMon, chkDay )
{
        var sDate = new Date();
        var mon = parseInt(1+sDate.getMonth());
        var day = sDate.getDate();
        var yr  = parseInt(sDate.getFullYear());
		
		if (chkYear > yr )
		{
			 return false;
		}
		else if (( chkMon >mon)&&(chkYear==yr))
		{
				return false;
		}	
        else if ((chkDay > day) &&  (chkMon==mon) && (chkYear==yr))
        {
			return false;
        }
		else if ((chkDay < 21) &&  (chkMon=='3') && (chkYear==yr)) //a date of deposit shuld not be earlier than 21 March 2009 
        {
			return false;
        }
		else if ((chkDay > 11) &&  (chkMon=='4') && (chkYear==yr)) //Last date for acceptance of Application money : 11 April, 2009
        {
			return false;
        }
		
		return true;
}

///////////////////////////////----------Date Validation----------------////////////////////////////////
function Validate_date(id)
{
	var id =id;
	var day_temp='';
	var mon_temp='';
	var yr_temp='';
	if (id =='DOB')
	{
		tt="Date of Birth";
		day_temp=document.app_form.Dob_dd.value;
		mon_temp=document.app_form.Dob_mm.value;
		yr_temp=document.app_form.Dob_yyyy.value;
		ff1=document.app_form.Dob_dd;
		ff2=document.app_form.Dob_mm;
		ff3=document.app_form.Dob_yyyy;

	}
			
	
	if (mon_temp == '-1')
	{
		alert('Please select month in '+tt+'.');
		ff2.focus();
		return false;
	}
	if (day_temp == '-1')
	{
		alert('Please select day in '+tt+'.');
		ff1.focus();
		return false;
	}
	if (yr_temp == '-1')
	{
		alert('Please select year in '+tt+'.');
		ff3.focus();
		return false;
	}
	//////// Month of Days 31 Have No Problem //////////

	//////// Month of Days = 30//////////
	if ( (mon_temp == 4 || mon_temp == 6 || mon_temp == 9 || mon_temp == 11) && day_temp > 30)
	{
		alert('Please select proper day in '+tt+'.');
		ff1.focus();
		return false;
	}
	else if (mon_temp == 2)
	{
		if ( (yr_temp % 4 == 0) && (yr_temp % 400 != 0))   //Leap Yr
		{
			if (day_temp > 29)
			{
				alert('Please select proper day in '+tt+'.');
				ff1.focus();
				return false;
			}
		}
		else if (day_temp > 28)
		{
			alert('Please select proper day in '+tt+'.');
			ff1.focus();
			return false;
		}
	}
	return true;
 }
function showAlertMessage( msg, focusObj )
{
	if ( focusObj )
	{
		focusObj.focus();
	}
	alert( msg );
	//MJS.SKR.showAlertMsg({MSG: msg, FOCUS_OBJ: focusObj});
}


//Form name is only to be passed to the function//
function isValid(f)     
//main function//
{
	
	var e=f.elements;
	var l=e.length;
	var form = f.name;
	for(i=0; i<=l-1; i++)
	{
		if(e[i])
		{
		var validate=TrimValue(e[i].id);
	
		if(validate != '')
		{
			var function_array=validate.split("-");
			for (j=0; j< function_array.length; j++)
			{
				var name = function_array[j];
				
				if(name =="validate" && !validation(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('validate').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="ValidName" && TrimValue(e[i].value)!='' && !is_Alpha(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('validname').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name.search('_') != -1)
				{
					checkLen = name.split("_");
					if(checkLen[0] == "CheckLength" && TrimValue(e[i].value)!='' && !checklength(e[i].alt,TrimValue(e[i].value),checkLen[1],checkLen[2]))
					{
						//e[i].select();
						//e[i].focus();
						return false;
					}
				}
				else if(name =="NoSpaceAllowed" && TrimValue(e[i].value)!='' && !checkSpaces(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('nospaceallowed').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="ValidUsername" && TrimValue(e[i].value)!='' && !userName(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('validusername').value, FOCUS_OBJ: e[i]});
					return false;
				}		
				else if(name =="AllowDigitsOnly" && TrimValue(e[i].value) !='' && !isNumber(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('allowdigitsonly').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="AllowDecimal" && TrimValue(e[i].value)!='' && !is_Decimal(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('allowdecimal').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="AllowAlphaNum" && TrimValue(e[i].value)!='' && !is_Alphanum(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('allowalphanum').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="ValidPhone" && TrimValue(e[i].value)!='' &&  !isPhone(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('validphone').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="ValidMobile" && TrimValue(e[i].value)!='' &&  !isMobile(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('validmobile').value, FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="ValidEmail" && TrimValue(e[i].value)!='' && !is_Email(TrimValue(e[i].value)))
				{
					MJS.SKR.showAlertMsg({MSG:document.getElementById('validemail').value+' '+e[i].alt+' ', FOCUS_OBJ: e[i]});
					return false;
				}
				else if(name =="ValidAddress" && TrimValue(e[i].value)!='' && !is_Add(TrimValue(e[i].value)))
				{
					if(e[i].alt)
					{
					MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('validaddress').value, FOCUS_OBJ: e[i]});
					return false;
					}
					else
					{
					MJS.SKR.showAlertMsg({MSG:e[i].title+' '+document.getElementById('validaddress').value, FOCUS_OBJ: e[i]});
					return false;
					}

				}
				else if(name =="Radio" | name =="CheckBox")
				{
					var buttonGroup = document.getElementsByName(e[i].name);
					var getvalue = RadioCheckbox(buttonGroup);
					if(getvalue =='0')
					{
							MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('validate').value, FOCUS_OBJ: e[i]});
							return false;
					}
				}
				else if(name =="Dropdown")
				{
					var SelectValue = e[i].value;
					if(SelectValue == '' | SelectValue=='0')
					{
							MJS.SKR.showAlertMsg({MSG:e[i].title+' '+document.getElementById('validate').value, FOCUS_OBJ: e[i]});
							return false;
					}
				}
				else if(name=="PassMatch" && TrimValue(e[i].value)!='')
                {
						if((e[i].value) != (e[i-1].value))
						{
							MJS.SKR.showAlertMsg({MSG:e[i].alt+' '+document.getElementById('and').value+' ' +e[i-1].alt+' '+document.getElementById('mismatch').value, FOCUS_OBJ: e[i]});
							return false;		
						}
						
                }
			}
		}
	}
	}

	
	if(f.name == 'forgot_pwd' )
	{
		var forgotPwd = ExtraChecksForgotPwd();
		return forgotPwd;
	}
	//if(f.name == 'app_form')
    //{
     //           var extrachecks = ExtraChecks();
     //           return extrachecks;
     //}
	return true;
} 













