/*
	This file contains a list of common javascript functions used through out the site.
	
	Created By - Krishnan
*/


/*
	Function - LoadPage
	Use - To load a PHP file and send it information. This function can be used to load PHP files using javascript to check for duplication.
	
	Arguements:
		PageName - Name of the PHP file to load. This arguement is string in nature. e.g.: LoadPage('hello.php')

		This function takes "undefined" arguements. Any arguements passed to this function other than PageName are taken as fields whose values
		must be posted to the PHP file in "PageName".
		The above arguements are objects in nature. e.g.: LoadPage('hello.php',document.forms[0].field1,document.forms[0].field2)
	
	Conditions:
		This function requires that the page calling it defines 2 functions:
		a. OnRequest - This function will keep running continuously from the start of the request to the end of the request.
					   This function can hold animation etc while the request is being made.

		a. OnRequestCompletion - This function is fired once the request is completed. This function must have a parameter that can recieve the
								 output that the file called in "PageName" gives back to javascript. This output will be string in nature.
*/
var request

function LoadPage(PageName)
{
	if(typeof(OnRequest)=="undefined" || typeof(OnRequestCompletion)=="undefined")
	{
		alert("Please define the functions called 'OnRequest' and 'OnRequestCompletion'.")
		return false
	}
	document.getElementById("DuplicationElem").style.display=""
	request = new ActiveXObject("Microsoft.XMLHTTP");
	request.open('post',PageName,true)
	request.setRequestHeader('content-type','application/x-www-form-urlencoded')
	SendString=""
	for(i=1;i<LoadPage.arguments.length;i++)
	{
		FieldToSend=LoadPage.arguments[i]
		SendString+=FieldToSend.name+'='+escape(FieldToSend.value)+"&"
	}
	SendString=SendString.substring(0,SendString.length-1)
	request.send(SendString)
	request.onreadystatechange=SeekInfo
}
function SeekInfo()
{
	if(request.readyState != 4)
	{
		OnRequest()
	}
	else
	{ 
		var answer = request.responseText;
		OnRequestCompletion(answer)
	}
}



/*
	Function - AtleastOneSelected
	Use - To see if atleast one checkbox amongst a group is selected.
	
	Arguements:
		TheElement - The checkbox object.
		TheElementAlert - The alert to be thrown if none is selected.
		TheConfirmAlert - The confirmation alert to be thrown.
	
	Returns:
		No checkbox is selected:
			returns false 

		1 or more checkbox selected:
			OK is clicked in the confirm box - 	true
			Cancel is clicked in the confirm box - 	false
*/

function AtleastOneSelected(TheElement,TheEmptyAlert,TheConfirmAlert,TheHiddenField)
{
	TheHiddenField.value=","
	
	if(typeof(TheEmptyAlert)=="undefined" || TheEmptyAlert=="")
		TheEmptyAlert='Please select atleast one item.'

	if(typeof(TheConfirmAlert)=="undefined" || TheConfirmAlert=="")
		TheConfirmAlert='Deleted items cannot be restored.\nContinue?'
	
	if(typeof(TheElement.length)=="undefined")
	{
		if(TheElement.checked==false)
		{
			alert(TheEmptyAlert)
			return false
		}
		else
		{
			TheHiddenField.value+=TheElement.value+","
		}
	}
	else
	{
		count=0
		for(i=0;i<TheElement.length;i++)
		{
			if(TheElement[i].checked)
			{
				count++
				TheHiddenField.value+=TheElement[i].value+","
			}
		}
		if(count==0)
		{
			alert(TheEmptyAlert)
			return false
		}
	}
	return confirm(TheConfirmAlert)
}


/*
	Function - resubmit
	Use - Resubmit a form to itself when a select box item is changed.
	
	Arguements:
		TheForm - The form object.
		TheSelectBox - The select box object.
		TheCurrentValue - Resubmitted value of the select box. This will be string in nature.
		TheEmptyValue - The value given for "Please Select" option. This will be string in nature.
		ThePage - By default this function will submit the page to itself. If this option is given then the page will submit the page name given in this arguement. This will be string in nature.
		SubmitForEmpty - By default this function will submit the page only if values other than "Please Select" is chosen. If this arguement is passed then it will also resubmit for the "Please Select" option. This will be string in nature.
*/
function resubmit(TheForm,TheSelectBox,TheCurrentValue,TheEmptyValue,ThePage,SubmitForEmpty)
{

	if(typeof(TheCurrentValue)=="undefined")
		TheCurrentValue="";

	if(typeof(TheEmptyValue)=="undefined")
		TheEmptyValue="";

	if(typeof(ThePage)=="undefined" || ThePage=="")
		ThePage=location.href.replace(/.+\/(.+)$/,"$1")

	if(typeof(SubmitForEmpty)=="undefined")
		SubmitForEmpty=false
	
	if(SubmitForEmpty)
	{
		if(TheSelectBox.value!=TheCurrentValue)
		{
			TheForm.action=ThePage
			TheForm.method="post"
			TheForm.submit()
		}
	}
	else
	{
		if(TheSelectBox.value!=TheEmptyValue && TheSelectBox.value!=TheCurrentValue)
		{
			TheForm.action=ThePage
			TheForm.method="post"
			TheForm.submit()
		}
	}
}

/*
	Function - ChooseAll
	Use - Chooses all the checkboxes of a particluar group. It can take a reference checkbox.
	
	Arguements:
		TheElement - The checkbox group object.
		TheRefElement- The element that will act as the reference. This must alos be a checkbox.
*/
function ChooseAll(TheElement,TheRefElement)
{
	if(typeof(TheElement)=="undefined")
	{
		return false
	}
	TheStatus=true
	if(typeof(TheRefElement)!="undefined" && TheRefElement.type=="checkbox")
	{
		TheStatus=TheRefElement.checked
	}

	if(typeof(TheElement.length)=="undefined")
	{
		if(TheElement.type=="checkbox")
		{
			TheElement.checked=TheStatus
		}
		else if(TheElement.type=="select")
		{
			TheElement.selected=TheStatus
		}
	}
	else
	{
		for(i=0;i<TheElement.length;i++)
		{
			if(TheElement[i].type=="checkbox")
			{
				TheElement[i].checked=TheStatus
			}
			else if(TheElement[i].type=="select")
			{
				TheElement[i].selected=TheStatus
			}
		}
	}
}

/*
	Function - TrackChange
	Use - Tracks change in checkbox statuses
	
	Arguements:
		TheCheckBox - The induvidual checkbox object. e.g.: TrackChange(this)
	
	Conditions:
		The checkbox must have an attribute called "RecordId"
*/
var HideStr=""
function TrackChange(TheCheckBox)
{
	TheValue=TheCheckBox.getAttribute("RecordId")

	if(HideStr.indexOf('***'+TheValue+'-')!=-1)
	{
		HideStr=HideStr.replace('***'+TheValue+'-'+'false***','')
		HideStr=HideStr.replace('***'+TheValue+'-'+'true***','')
	}
	else
	{
		if(TheCheckBox.checked)
		{
			HideStr+='***'+TheValue+'-'+'true***'
		}
		else
		{
			HideStr+='***'+TheValue+'-'+'false***'
		}
	}
}


/*
	Function - ChangeStatus
	Use - This function must always be called by the (De)Activate button.
	
	Arguements:
		TheForm - The form object.
		TheAction - The action of the form object. Must be a php file name.
		TheEmptyAlert - The alert to be shown if no changes have been made to records.
		TheConfirmAlert - The confirmation alert.
		TheHiddenField - The hidden field that will store the ids to be sent across to the page given in action attribute.
*/
function ChangeStatus(TheForm,TheAction,TheEmptyAlert,TheConfirmAlert,TheHiddenField)
{
	if(HideStr=="")
	{
		alert(TheEmptyAlert)
		return false
	}
	else
	{
		if(confirm(TheConfirmAlert))
		{
			TheHiddenField.value=HideStr
			TheForm.action=TheAction
			TheForm.submit()
		}
		else
		{
			return false
		}
	}
}


/*
	Function - GoToPage
	Use - Part of the pagination framework.
	
	Arguements:
		ThePage - The page number the page must go to.
*/
function GoToPage(ThePage)
{
	TheUrl=location.href
	document.forms[0].CurrentPage.value=ThePage
	document.forms[0].action = TheUrl.replace(/.*\/(.*)/,"$1")
	document.forms[0].submit()
}


/*
	Function - OnRequest
	Use - Part of the XMLHTTP framework. This function will keep firing when the request is beign made.
	
	Conditions:
		Requires an element called "DuplicationElem" defined.
*/
function OnRequest()
{
}

function CheckPassword(NewPassWord,ConfirmPassword)
{
	var strPass = NewPassWord.value;
	var strconfPass = ConfirmPassword.value;
	if (strPass!=strconfPass) 
	{
		alert("New Password and Confirm Password are not same")
		ConfirmPassword.focus();
		return false;
	}
	return true;
}

// this function deletes the selected files. The parameters to be passed is same as for function AtleastOneSelected() and finally add where the page it has to be submitted

function DeleteMe(TheElement,TheEmptyAlert,TheConfirmAlert,TheHiddenField,TheActionPage)
{
	if(AtleastOneSelected(TheElement,TheEmptyAlert,TheConfirmAlert,TheHiddenField))
	{
		document.forms[0].action=TheActionPage
		document.forms[0].submit()
	}
}

function EmptyField(TheField)
{
	TheField.value=""
}
