// JavaScript Document

/*
	Function Alert_Message()
	------------------------
	Parameters Passed :-
	1) msg : Alert Message 
	2) objId : Object Id
*/
function Alert_Message(msg, objId)
{alert(msg);objId.select();}

/*
	Function AddToOptionList()
	--------------------------
	Parameters Passed :-
	1) OptionList : Option Listbox Object Id
	2) OptionValue : Listbox option value
	3) OptionText : Listbox option text
*/
function AddToOptionList(OptionList, OptionValue, OptionText)
{ 
	OptionList[eval(OptionList.length)] = new Option(OptionText, OptionValue);
}


/*
	Function Select_Option()
	------------------------
	Parameters Passed :-
	1) objOptionBox : Option Listbox Object Id
	2) SelVal : Value to be selected
*/
function Select_Option(objOptionBox, SelVal)
{
	var cnt;
	for(cnt=0;cnt<=eval(objOptionBox.length)-1;++cnt)
	{
		if(eval(objOptionBox[cnt].value)==eval(SelVal))
		{
			objOptionBox[cnt].selected = true;
		}
	}
}

/*
	Function EDIT_VIEW()
	------------------
	Parameters Passed :-
	1) objForm : Form Object
	2) objId : Id of the object
	3) Id : Id
	4) Dest : Destination page name
*/
function EDIT_VIEW(objForm, objId, Id, dest)
{
	objId.value = Id;
	objForm.action = dest;
	objForm.submit();
}

/*
	Function SetPageNumber()
	----------------------
	Parameters Passed :-
	1) objForm : Form Object
	2) objCurrentPage : Current Page object
	3) Page : Page
	4) TotalPages : Total Pages
	5) dest : Destination page
*/	
function SetPageNumber(objForm, objCurrentPage, Page, TotalPages, dest)
{
	var FormName1 = eval(objForm)
	
	if(eval(Page)==0)
	{
		alert('Page number should be between 1 and '+TotalPages);
		if(FormName1!=null)
		FormName1.GoToPage.focus();
	}
	else if(Page == '')
	{alert("Please enter the Page Number.");
	 if(FormName1!=null)
		FormName1.GoToPage.focus();	
	}
	else
	{
		
		if((eval(Page)>eval(TotalPages)) || (eval(Page)==0))
		{alert('Page number should be between 1 and '+TotalPages);
		 if(FormName1!=null)
			FormName1.GoToPage.focus();
		}
		else
		{
			objCurrentPage.value = Page;
			objForm.action = dest;
			objForm.submit();
		}
	}
}

/*
	Function DoDelete()
	-------------------
	Parameters Passed :-
	1) objForm : Form Object
	2) objDeleteId : Delete Id object
	3) DelType : Delete type :  If 1 -> Checkbox delete(Multiple Delete). If 2 -> Link/Button Delete(Single Delete)
	4) delid : Delete id when delete type is 2
	4) msg : Name of the module. It is used in the conforming message
	5) dest : Delete code file
*/
function DoDelete(objForm, objDeleteId, DelType, delid, msg, dest)
{
	var i, ChkFlag;
	ChkFlag = 0
	
	if(eval(DelType)==1)
	{
		for(i=0;i<eval(objDeleteId.length);++i)
		{
			if(objDeleteId[i].checked==true)
			{ChkFlag = 1;}
		}
		
		if(eval(ChkFlag)==1)
		{
			if (confirm('All the information related with this '+msg+' will be Deleted from the WebSite. \n Are you sure to delete this '+msg+' ?'))
			{
				objForm.action = dest;
				objForm.submit();
			}	
		}
		else
		{alert('Please select at least one '+msg+' to delete');}	
	}
	else if(eval(DelType)==2)
	{
		objDeleteId.value = delid;
		if (confirm('All the information related with this '+msg+' will be Deleted from the WebSite. \n Are you sure to delete this '+msg+' ?'))
			{
			objForm.action = dest;
			objForm.submit();
		}	
	}
}


/*
	Function DoUpdate()
	-------------------
	Parameters Passed :-
	1) objForm : Form Object
	2) objPublishId : Checkbox id object
	3) objPublishId : Text Object which stores the ids of the checked boxes
	4) objUnPublishId : Text Object which stores the ids of the unchecked boxes
	5) Msg : Name of the module. It is used in the conforming message
*/
function DoUpdate(objForm, objPubId, objPublishId, objUnPublishId, msg, dest)
{
	var i, Chk_Id, UnChk_Id;

	Chk_Id = "";
	UnChk_Id = "";
	
	for(i=0;i<eval(objPubId.length);++i)
	{
		if(objPubId[i].checked==true)
		{Chk_Id = Chk_Id+','+objPubId[i].value;}
		else
		{UnChk_Id = UnChk_Id+','+objPubId[i].value;}
	}
	
	objPublishId.value = Chk_Id;
	objUnPublishId.value = UnChk_Id;
	
	if(confirm('Are you sure you want to publish/unpublish the '+msg+' ?')==true)
	{
		objForm.action = dest;
		objForm.submit();
	}	
}

function validcharacter_check(ele_id, ele_valid_char)
{
	var i, fval;
	fval = Trim(ele_id.value.toLowerCase());
	ele_valid_char = ele_valid_char.toLowerCase();
	for(i=0;i<=eval(fval.length)-1;++i)
	{
		if(ele_valid_char.indexOf(fval.charAt(i))==-1)
		{return false;}
	}
	return true;
}

function invalidcharacter_check(ele_id, ele_invalid_char)
{
	var i, fval;
	fval = Trim(ele_id.value.toLowerCase());
	ele_invalid_char = ele_invalid_char.toLowerCase();
	for(i=0;i<=eval(fval.length)-1;++i)
	{
		if(ele_invalid_char.indexOf(fval.charAt(i))!=-1)
		{return false;}
	}
	return true;
}

function emformat(obj)
{
	if((Trim(obj.value).length!=0))
	{
		if(obj.value.indexOf(' ')!=-1)
		{
			alert('Email format should be abc@xyz.com');
			return false;
		}
		
		if(obj.value.indexOf('@')==-1)
		{
			alert('Email format should be abc@xyz.com');
			return false;
		}
		
		pos = obj.value.indexOf('@'); //Finding the position of the @ character.
		smstr = obj.value.substr(eval(pos)+1) //Reteriving the string after the @ character
		if(smstr.indexOf('.')==-1)
		{
			alert('Email format should be abc@xyz.com');
			return false;
		}
		
		if(smstr.length<3)
		{
			alert('Email format should be abc@xyz.com');
			return false;
		}
		
		ansmstr = obj.value.substr(0,eval(pos)) //Reteriving the string before the @ character
		if(ansmstr.length<2)
		{
			alert('Email format should be abc@xyz.com');
			return false;
		}
	}
	return true;
}

/*
	Fucntion NotAllowed(objFileField) :-
	---------------------------------
	1) objFileField : Object reference of the file field
*/
function NotAllowed(objFileField)
{
	alert('Please select the file using Browse Button.'); 
	objFileField.blur();
}

function Check_FileType(ele_id, ele_filetype)
{
	if(Trim(ele_id.value).length!=0)
	{
		var sp, i, ftype_fg;
		var fd = ele_id.value;
		var filelen_1 = fd.length;
		
		var arr1 = fd.split("\\");
		
		var pos_1 = fd.lastIndexOf(".");
		pos_1 = pos_1+1;
		
		var extn_1 = fd.substr(pos_1,filelen_1);
		extn_1 = extn_1.toUpperCase();
		if(ele_id.value.lastIndexOf(".")==-1)
		{return false;}
		ele_filetype = ele_filetype.toUpperCase();
		sp = ele_filetype.split(",");
		ftype_fg = 0;
		for(i=0;i<=eval(sp.length)-1;++i)
		{
			if(extn_1==Trim(sp[i]))
			{ftype_fg=1;}
		}
		if(eval(ftype_fg)==0)
		{return false;}
		else
		{return true;}
	}
}

function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
		{return"";}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE=="")
		{return "";}
	else
		{return TRIM_VALUE;}
} 

function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0)
		{return"";}
	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
} 

function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
		{return"";}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	}
	return strTemp;
}