// Improved & Implemented By Suresh & Padmakar

var ErrMessage;
var ErrItem;
var ErrFound = false;
var EmailIdLastMaxChar = 3
var EmailIdLastMinChar = 2
var NormalCharString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\r\n\f";
var NumericString = "1234567890";
var achr=".~/%-_"

//Check the given item value is null or not.
//If the given Item value is null and compalsary(C) then it sets the error message and return true, Otherwise, it just return true.
//If the given item value is not null then it returns false.
function CheckNull(){
	Item = arguments[0];
	if ((Item.type == "text" || Item.type == "password" || Item.type == "textarea" || Item.type == "file") && Item.value == "")
		return (setError("Please Enter "+Item.name, Item, true));
	else if (Item.type == "select-one" && Item.selectedIndex <= 0)	
		return (setError("Please Select "+Item.name, Item, true));
	return false;
}

//Check All Spaces
function CheckAllSpaces(){
	for(var i=0;i<arguments[0].value.length;i++)
		if (arguments[0].value.charAt(i) != " ") return false;
	return (setError("Please Enter "+Item.name, Item, true));
}



//Check the every charecter in the given Item value, that is in the given character list or not.
//If the character not in the given character list it sets the error message and return true.
//If the all the characters in the given Item value are in the given character list, it returns false.
function CheckeachChar(){
	Item = arguments[0];
	if (Item.type == "text" || Item.type == "password" || Item.type=="textarea" || Item.type == "file"){
		var CharString = arguments[1];
		for(var i=0;i<Item.value.length;i++){
			if (CharString.indexOf(Item.value.charAt(i)) == -1){
				return(setError("Please Enter Valid "+Item.name, Item, true));
			}
		}
	}
	return false;
}

//Check the given Item value is email Id or not.
//If the '@' symbol at begining or not entered it will set Error message.
//If the '@' symbol occured more than one's it set's Error message.
//After the '@' symbol there is no '.' symbol it will set's Error message.
//If '@' symbol and '.' symbol occured together then it will set's Error message.
//If '.' symbol occured at end of the Item value it will set's Error message.
//If the the email id is correct then it just return false.
function CheckEmailId(){
	Item = arguments[0];
	var IndexofAtdharate = Item.value.indexOf("@");
	var LastIndexofPeriod = Item.value.lastIndexOf(".");
	var IndexOfPeriod = Item.value.indexOf(".");

	if ((LastIndexofPeriod + EmailIdLastMinChar + 1 > Item.value.length) || (Item.value.length > LastIndexofPeriod + EmailIdLastMaxChar + 1))
		setError("Please enter valid Email ID for ", Item, true);
	else if (IndexofAtdharate == -1 || IndexofAtdharate == 0)
		setError("Please enter domain name for ", Item, true);
	else if (IndexofAtdharate < Item.value.lastIndexOf("_"))
		setError("Please enter valid Email ID for ", Item, true);
	else if (IndexofAtdharate == (Item.value.indexOf(".") - 1))
		setError("Please enter Domain name for ", Item, true);
	else if (IndexofAtdharate != Item.value.lastIndexOf("@"))
		setError("Please enter valid Email ID for ", Item, true);
	else if (Item.value.substring(IndexofAtdharate+1).indexOf(".") == -1) 
		setError("Please enter valid domain name for ", Item, true);
	else if (Item.value.substring(0,IndexofAtdharate).lastIndexOf(".") == IndexofAtdharate-1)
		setError("Please enter valid domain name for ", Item, true);
	else if (Item.value.lastIndexOf("_") + 1 == IndexofAtdharate)
		setError("Please enter valid Email ID for ", Item, true);
 	else	return false;
	return true;
}

//Check the Item length with in the given limits or not. If it is then returns false, Otherwise set the Error message and return true.
function CheckLength(Item,a,b){
	Item = arguments[0];
	minLength = arguments[1];
	maxLength = arguments[2];
	if (Item.value.length < minLength || Item.value.length > maxLength)
		return(setError("Please Enter minimum of " + minLength 
			+ " Characters and maximum of " + maxLength + " Characters in " + Item.name, Item, true));
	else	return false;
	
}	

/// checks for partial web address validation

function CheckWebId()
{
Item = arguments[0];
var stp = Item.value.indexOf(".");
if(stp<0)
return true;
else
{setError("Please enter valid web address for ", Item, true);
return false;}
}

function CheckWeb()
{
	Item = arguments[0];

	var ep = Item.value.lastIndexOf(".");
	var stp = Item.value.indexOf(".");

if(stp <0)
	setError("'.' is missing in URL for ", Item, true);
 else if ((stp >= 0) && (stp <2))  
	setError("'.' is not in correct place for ", Item, true);
else if (Item.value.length-(ep+1)>4 || Item.value.length-(ep+1)< 2)
	setError("Ending name is not correct for " , Item, true);
else if (ep==stp)
	setError("One more '.' is required for ", Item, true);
else
	return false;
	return true;
} 



//Check the given Item begins with any given charecters then it sets error message and return true, Otherwise return false.
function CheckBeginingChars(Item,Chars){
	Item = arguments[0];
	Chars = arguments[1];
	var NotAllowedChars = Chars.split("!");
	for(var i=0;i<NotAllowedChars.length;i++){
		if (Item.value.indexOf(NotAllowedChars[i]) == 0){
			switch(NotAllowedChars[i]){
				case "." : return (setError("Starting letter should not be period(.) in "+Item.name, Item, true));
				case "/" : return (setError("Starting letter should not be slash(/) in "+Item.name, Item, true));
				case "'" : return (setError("Starting letter should not be astric(') in "+Item.name, Item, true));
				case "-" : return (setError("Starting letter should not be hyphen(-) in "+Item.name, Item, true));
				case "_" : return (setError("Starting letter should not be underscore(_) in "+Item.name, Item, true));
				case "," : return (setError("Starting letter should not be comma(,) in "+Item.name, Item, true));
				case ":" : return (setError("Starting letter should not be colon(:) in "+Item.name, Item, true));
			}
		}
	}
	return false;
}

//Check the Field is Numeric or not
function CheckNumeric(){
	Item = arguments[0];
	if (isNaN(parseInt(Item.value))){
		return(setError(Item.name + " sholud contain only Numeric values(0-9)", Item, true));
	}
	return false;
}


//Set the error message
function setError(){
	if (arguments.length == 3){
		ErrMessage = arguments[0];
		ErrItem = arguments[1];
		return true;
	}
	return false;
}


//Display the given error message and place the control on the particular field.
function DisplayError(ErrorMessage, Item){
	alert(ErrorMessage);
	ErrMessage = ""
	ErrFound = false;
	Item.focus();
}

//Initialize the List box means it removes the existing options from the given List box group
function InitList(Group){
	if (Group.length > 1) {
		for(var i=Group.length;i>0;i--)
			Group.options[i] = null;
	}
}

//Insert the given value as a option in the given Group with the given index
function setOption(Group, Value, Index){
	var valOption = new Option(Value);
	var valLen = Group.length;
	Group.options[valLen] = valOption;
	Group.options[valLen].value = Index;
}

//Select the Option in the List box by checking the Value
function selectOption(Group, Value){
	for (var i=0;i<Group.length;i++){
		if (Group.options[i].value == Value) {
			Group.selectedIndex == Group[i].index;
			Group[i].selected = true;			
			break;
		}
	}
}


// Suresh
// selecting value in listbox

function CheckList(Item, Name)
{

if (Item.selectedIndex < 1)
{alert("Select the value from "+Name);
Item.focus();}
else
return true;  
}

	


//Select the Radio button in the Radio group by checking the Value
function selectRadio(Item, Value){
	for(var i=0;i<Item.length;i++){
		if (Item[i].value == Value) {
			Item[i].checked = true;
			break;
		}
	}
}

//Select the Check boxes by using the given values
function selectCheckboxes(Item, Value){
	var Val = Value.split(",");
//	alert(Val.length +" " + Item.length);
	for(var i=0;i<Val.length;i++){
		for (var j=0;j<Item.length;j++){
			if (Item[j].value == Val[i]){
				Item[j].checked = true;
				break;
			}
		}
	}		
}

//Suresh
//Checks the Check boxes whether it is selected or not & checks for NO
function CheckBoxes(Item, Value, Name)
{
var a=0;
if(Item)
{
if(Item.length>=2)
{
for(var j=0;j<Item.length;j++)
		{
			if(Item[j].checked == true ){
				a = parseInt(a) + 1;     }
		}
}
else if(Item.checked==true)
a=1;
		
		if(Value==0 && a==0)
		alert("Please select check box for " + Name);
		else if (a<Value)
		alert("Minimum of "+ Value+" values should selected for "+ Name);
		else
		return true;
}
else
{alert("Records are not available to perform this action.");
return false;}
}

// Check all check boxes available by suresh
function CheckAll(Item)
{
if(Item)
{
	if(Item.length>=2)
	{
		for(var j=0;j<Item.length;j++)
		{
			Item[j].checked=true;     
		}
	}
	else 
	Item.checked=true;
return true;
}
}




//Set the Days List by using the array Days. The values of the days will change by the Month.
function setDayList(ListGroupDay,ListGroupMonth,ListGroupYear){
	var maxDays
	//Find the Year is Leap year or not
	if (ListGroupYear.selectedIndex != 0){
		var Year = parseInt(ListGroupYear.options[ListGroupYear.selectedIndex].text);
		var Month = ListGroupMonth.selectedIndex;
		var maxDays = 31;
		if (Month == 2)
			if (Year%4 == 0)	maxDays = 29;
			else		maxDays = 28;
		else if (Month == 4 || Month == 6 || Month == 9 || Month == 11)
			maxDays = 30;
	}
	InitList(ListGroupDay);
	for(var i=1;i<=maxDays;i++)
		setOption(ListGroupDay, i, i);
	ListGroupDay.selectedIndex = 0;
}


//This function checks Combo BOX 
function CheckCOMBO(Item,disp){
  ErrMessage = "Please choose a value" 
  if (Item.selectedIndex == 0 ) 
    { 
     alert("Please select a " + disp); 
     Item.focus();
     return false;
    } 
  else
    return true; 
   
} 
  
//This function checks the Login page
function LoginInto(Item){
	if (CheckNull(Item.UserName,"C"))
		DisplayError(ErrMessage, ErrItem);
	else if(CheckeachChar(Item.UserName,NormalCharString+NumericString+"_"))
		DisplayError(ErrMessage, ErrItem);
	else if (CheckNull(Item.Password,"C"))
		DisplayError(ErrMessage, ErrItem);
	else if (CheckeachChar(Item.Password,NormalCharString+NumericString))
		DisplayError(ErrMessage,ErrItem);
	else
		document.frmLogin.submit();
}

// Narayana

function Checkmax(a,b){
	var Order = new Array();
	if (a.value > b ){ 
		DisplayError("Please enter below or equal to " + b,a);
		return false;
	}
	else
	for(var i=0;i<document.forms[0].length;i++){
		if (document.forms[0].elements[i].type == "text" && document.forms[0].elements[i].name.indexOf("Order") != -1)
			if (document.forms[0].elements[i].value > a.value ){
				DisplayError("Please enter below or equal to " +a.value,document.forms[0].elements[i]);
				return false;
				break;
			}
	}

	return true; 
} 

//Disable an item
function DisableItem(Item){
	Item.disabled = true;
}

//This function for all the checking 
function CheckValidate(){
	Item = arguments[0];
	ErrFound = false;
	CheckMax = arguments.length;
	if (arguments[CheckMax-2] == "UserDefined")	CheckMax = CheckMax -2 ;
	for(var i=1;i<CheckMax;){
		var IncArgVal = 1;
		TypeOfValidation = arguments[i];
		switch(TypeOfValidation){
			case "NULL" : if (CheckNull(Item)){ ErrFound = true;}break;
			case "EMAIL" : if (!CheckNull(Item) && CheckEmailId(Item))
			{ ErrFound = true;}break;
			case "WEB" : if( CheckWebId(Item)  &&  CheckeachChar(Item,arguments[i+1]))
			 { ErrFound = true;}IncArgVal=3;break; 
			case "LENGTH" : if(!CheckNull(Item) && CheckLength(Item,arguments[i+1],arguments[i+2])){
						ErrFound = true;}IncArgVal = 3;break;
			case "CHARS" : if(!CheckNull(Item) &&  CheckeachChar(Item,arguments[i+1])){
						ErrFound = true;}IncArgVal = 2;break;
			case "BEGINCHAR" : if (!CheckNull(Item) && CheckBeginingChars(Item,arguments[i+1])){
						ErrFound = true;}IncArgVal = 2;break;
			case "PASSWORD" : if (arguments[i+1].value != Item.value){
						setError("Confirm Password should be same as the Password", arguments[i+1],true);ErrFound = true;}
						IncArgVal = 2;break;
		}

		if (ErrFound) break;
		i += IncArgVal;
	}
	if (Item.type == "text" || Item.type == "password" || Item.type == "textarea" || Item.type == "file")
		if (!CheckNull(Item) && CheckAllSpaces(Item))
			ErrFound = true;
	if (ErrFound && arguments[arguments.length-2] != "UserDefined" && arguments[arguments.length-2] != "UserDefinedh" ) {
		DisplayError(ErrMessage, ErrItem);
		return false;
	}
	else if (ErrFound && arguments[arguments.length-2] == "UserDefined") {
		DisplayError(ErrMessage.substring(0,ErrMessage.lastIndexOf(" ")) + " " + arguments[arguments.length-1], ErrItem);
		return false;
	}
	else if (ErrFound && arguments[arguments.length-2] == "UserDefinedh") {
		DisplayError(arguments[arguments.length-1], ErrItem);
		return false;
	}
	return true;
}


function autoselect(drp,selectedvalue)
{
ad = drp;
for(i=0;i<ad.length;i++)
{
if(ad.options[i].value == selectedvalue)
{
ad.options.selectedIndex = i ;
}
}
}
