/*************************************************************************************
* Purpose	: Contains user defined functions.
* Version	: 1.0
* Author	: Vasanth
* Date		: 06-10-2008
**************************************************************************************/

/*
######################################################################################
# Function to upload images/videos selected. [page: createproject.php]
# @frm : Name of the form.
######################################################################################
*/
function fnUpload(frm,act)
{
	frm.createproject_hid.value=act;
	frm.tproject_details.value = document.getElementById('tproj_details').value;
	if(document.getElementById('tdonation_impact').value == '<div></div>')
		frm.div_donation_impact.value = "";
	else
		frm.div_donation_impact.value = document.getElementById('tdonation_impact').value;
	frm.submit();
}

function fnShowDiv(divId)
{
	if(divId == 'photosDiv')
	{
		document.getElementById('photosDiv').style.display='';
		document.getElementById('videoDiv').style.display='none';
		document.getElementById('photos').setAttribute('src','images/photos_tab_hover.gif');
		document.getElementById('video').setAttribute('src','images/video_tab.gif');
	}
	else if(divId == 'videoDiv')
	{
		document.getElementById('photosDiv').style.display='none';
		document.getElementById('videoDiv').style.display='';
		document.getElementById('photos').setAttribute('src','images/photos_tab.gif');
		document.getElementById('video').setAttribute('src','images/video_tab_hover.gif');
	}
	else if(divId == 'latestnewsDiv')
	{
		document.getElementById('latestnewsDiv').style.display='';
		document.getElementById('projplanDiv').style.display='none';
		document.getElementById('latestnews').setAttribute('src','images/latestnews_tab_hover.gif');
		document.getElementById('projectplan').setAttribute('src','images/prjplan_tab.gif');
	}
	else if(divId == 'projplanDiv')
	{
		document.getElementById('latestnewsDiv').style.display='none';
		document.getElementById('projplanDiv').style.display='';
		document.getElementById('latestnews').setAttribute('src','images/latestnews_tab.gif');
		document.getElementById('projectplan').setAttribute('src','images/prjplan_tab_hover.gif');
	}
}


/*
######################################################################################
# Field partner selection.
# @obj : Select box object.
# @place : In which select box action has trigged.
######################################################################################
*/
function fnFieldPartner(obj,place)
{
	if(place == 'top')
	{
		var len = document.frmproject.ifield_partner_id_dup.length;
		var i,j;
		//alert("<<< TOP >>>  "+"LENGTH >> "+len+" VALUE >>> "+obj.value)
		for(i=0; i<len; i++)
		{
			document.frmproject.ifield_partner_id_dup[i].removeAttribute('selected',1);
		}

		for(j=0; j<len; j++)
		{
			if(document.frmproject.ifield_partner_id_dup[j].value == obj.value)
				document.frmproject.ifield_partner_id_dup[j].setAttribute('selected','selected');
		}
	}
	else if(place == 'bottom')
	{
		var len = document.frmproject.ifield_partner_id.length;
		var k,l;
		//alert("<<< BOTTOM >>>  "+"LENGTH >> "+len+" VALUE >>> "+obj.value)
		for(k=0; k<len; k++)
			document.frmproject.ifield_partner_id[k].removeAttribute('selected',1);

		for(l=0; l<len; l++)
		{
			if(document.frmproject.ifield_partner_id[l].value == obj.value)
				document.frmproject.ifield_partner_id[l].setAttribute('selected','selected');
		}
	}
	xhReq.open("GET","ajax/fieldpartner_details.php?fpid="+obj.value,true);
	xhReq.onreadystatechange = fnFieldPartnerDisplay;
	xhReq.send(null);
}

function fnFieldPartnerDisplay()
{
	if(xhReq.readystate != 4) { return; }
	var serverResponse = xhReq.responseText;
	document.getElementById('fpdiv').innerHTML = serverResponse;
}


/*
######################################################################################
# Purpose: Submit the project for endorsing
# @frm : Name of the form
######################################################################################
*/
function fnSubmit(frm)
{
	//var str= '0123456789';
	var regexp = new RegExp("^[0-9]*$");
	var ffval = frm.feedbackfreq.value;

	if(frm.feedbackfreq.value == "")
	{
		alert("Please enter project feedback frequency");
		frm.feedbackfreq.focus();
		return false;
	}
	else if(frm.feedbackfreq.value <= 0  || !ffval.match(regexp))  //|| str.indexOf(frm.feedbackfreq.value) < 0)
	{
		alert("Invalid input in project feedback frequency");
		frm.feedbackfreq.focus();
		return false;
	}

	if(frm.submitcmts.value == "")
	{
		alert('Please enter comments');
		frm.submitcmts.focus();
		return false;
	}
	
	if(confirm("Are you sure to submit the project?"))
	{
		frm.submit_hid.value='set';
		frm.submit();
	}
}

/*
######################################################################################
# Purpose: Endorse the project submitted
# @frm : Name of the form
# @act : Action performed (endorsed/rejected)
######################################################################################
*/
function fnEndorse(frm,act)
{
	//var str='123456789';
	var regexp = new RegExp("^[0-9]*$");
	var ffval = frm.feedbackfreq.value;

	if(frm.feedbackfreq.value == "")
	{
		alert("Please enter project feedback frequency");
		frm.feedbackfreq.focus();
		return false;
	}
	else if(frm.feedbackfreq.value <= 0  || !ffval.match(regexp)) // || str.indexOf(frm.feedbackfreq.value)  < 0)
	{
		alert("Invalid input in project feedback frequency");
		frm.feedbackfreq.focus();
		return false;
	}
	if(frm.endorsecmts.value == "")
	{
		alert('Please enter comments');
		frm.endorsecmts.focus();
		return false;
	}

	if(act == 'endorse')
	{
		if(confirm("Are you sure to endorse the project?"))
		{
			frm.endorse_hid.value='set';
			frm.endorse_action.value=act;
			frm.submit();
		}
	}
	else if(act == 'reject')
	{
		if(confirm("Are you sure to reject the project?"))
		{
			frm.endorse_hid.value='set';
			frm.endorse_action.value=act;
			frm.submit();
		}
	}
}

/*
######################################################################################
# Purpose: Endorse the project submitted
# @frm : Name of the form
# @act : Action performed (published/rejected)
######################################################################################
*/
function fnPublish(frm,act)
{
	if(frm.publishcmts.value == "")
	{
		alert('Please enter comments');
		frm.publishcmts.focus();
		return false;
	}
	if(act == 'publish')
	{
		if(confirm("Are you sure to publish the project?"))
		{
			frm.publish_hid.value='set';
			frm.publish_action.value=act;
			frm.submit();
		}
	}
	else if(act == 'reject')
	{
		if(confirm("Are you sure to reject the project?"))
		{
			frm.publish_hid.value='set';
			frm.publish_action.value=act;
			frm.submit();
		}
	}
}


/*
######################################################################################
# Purpose: Load selected page choosen from quick links
# @obj : select box object
######################################################################################
*/
function fnQuickLink(obj)
{
	var arr = obj.value.split("^");
	pageid = arr[1];
	var link = arr[0];
	window.location.href=link+"?pgid="+pageid;
}

function fnSupervisedBy(frm,obj)
{
	var id = obj.value;
	//if(id != 0)
	//{
		frm.supervisedby_hid.value=id
		frm.submit();
	//}
}

function fnFilterBy(frm,obj)
{
	var id = obj.value;
	//if(id!=0)
	//{
		frm.filterby_hid.value=id
		frm.submit();
	//}
}

function fnSortBy(frm,obj)
{
	var id = obj.value;
	if(id!=0)
	{
		frm.sortby_hid.value=id
		frm.submit();
	}
}

// function to alert related msg when clicked on icons on projects grid page [create.php]
function fnAlertMsg(msg)
{
	alert(msg);
}

// function to perform action when an item selected from drop down [donations.php]
function fnOnChange(obj)
{
	if(xhReq.readyState != 4 && xhReq.readyState != 0)
			return;
	xhReq.open("GET",'ajax/donationslist.php?pid='+obj.value,true);
	xhReq.onreadystatechange = fnListDonations;
	xhReq.send(null);
//	window.location.href = 'donations.php?pgid=3&pid='+obj.value;
}

function fnListDonations()
{
	if(xhReq.readyState != 4) { return; }
	var serverResponse = xhReq.responseText;
	//alert(serverResponse);

	document.getElementById('dlist').innerHTML = serverResponse;
}

function fnSaveFP(frm,type)
{
	frm.act.value=type;
	frm.tindividuals_assisted_hid.value = document.getElementById('tindividuals_assisted').innerHTML;
	frm.tapplication_details_hid.value = document.getElementById('tapplication_details').innerHTML;
	frm.submit();
}

// Donations List and Adding Offline Donations
function fnShowContent(divid)
{
	if(divid == 'existinguserdiv')
	{
		window.location.href="addofflinedonation.php?pgid=4&action=add";
		/*document.getElementById('existinguserdiv').style.display='';
		document.getElementById('newuserdiv').style.display='none';
		document.getElementById('message').style.display=document.getElementById('message').style.display='none'?'none':'';*/
	}
	else if(divid == 'newuserdiv')
	{
		document.getElementById('existinguserdiv').style.display='none';
		document.getElementById('newuserdiv').style.display='';
		if(document.getElementById('message') != null)
			document.getElementById('message').style.display=document.getElementById('message').style.display='none'?'none':'';
		/*if(document.getElementById('mandatory') == null)
			document.getElementById('mandatory').style.display=document.getElementById('mandatory').style.display='none'?'none':'';*/
	}
	else if(divid == "nugeneraldonation")
		document.getElementById('nuprojectslistdiv').style.display='none';
	else if(divid == "nuprojectslistdiv")
		document.getElementById('nuprojectslistdiv').style.display='';
	else if(divid == "eugeneraldonation")
		document.getElementById('euprojectslistdiv').style.display='none';
	else if(divid == "euprojectslistdiv")
		document.getElementById('euprojectslistdiv').style.display='';
}

// Milestone popup window
function fnMilestonePopup(pid)
{
	window.open('milestonepopup.php?pid='+pid,'Milestone','width=480,height=230,scrollbars=1,resizable=0,left=360,top=200');
}

// Field Partner popup window
function fnFieldParnterPopup()
{
	window.open('fieldpartnerpopup.php','FieldPartner','width=1050,height=600,scrollbars=0,resizable=0,left=50,top=150');
}

function fnOnChangeUserDonations(obj,iuser_id)
{
	if(xhReq.readyState != 4 && xhReq.readyState != 0)
			return;
	xhReq.open("GET",'ajax/user_donations_list.php?iuser_id='+iuser_id+'&pid='+obj.value,true);
	xhReq.onreadystatechange = fnListUserDonations;
	xhReq.send(null);
}

function fnListUserDonations()
{
	if(xhReq.readyState != 4) { return; }
	var serverResponse = xhReq.responseText;

	document.getElementById('dlist').innerHTML = serverResponse;
}

// Popup window to upload images for the project (Page: createproject.php)
function fnUploadImages(prjid)
{
	window.open('imageupload.php?pid='+prjid,'','width=700,height=430,scrollbars=1,resizable=1,left=200,top=200');
}

// Popup window to preview project images (Page: createproject.php)
function fnPreviewImage(phtid)
{
	window.open('preview.php?phtid='+phtid,'','width=500,height=500,resizable=0');
}

// Add Offline Donation details of Existing User
function fnAddOfflineDonation(frm)
{
	var modelen = frm.modeofdonation.length; // Mode of donation length
	var dtypelen = frm.donationtype.length; // Donation type length
	var i=j=0;
	var mode = 0;
	var type = 0;
	for(i=1; i<=modelen; i++)
	{
		var t = String('eum'+i);
		if(document.getElementById(t).checked)
			frm.eumode.value=document.getElementById(t).value;
		else
			mode++;
	}
	for(j=1; j<=dtypelen; j++)
	{
		var te = String('eut'+j);
		if(document.getElementById(te).checked)
			frm.eutype.value=document.getElementById(te).value;
		else
			type++;
	}

	if(modelen == mode)
		frm.eumode.value="error";
	if(dtypelen == type)
		frm.eutype.value="error";

	frm.act.value = 'euset';
	frm.submit();
}


// Add Offline Donation details of New User
function fnAddOfflineDonationNewUser(frm)
{
	var modelen = frm.numodeofdonation.length; // Mode of donation length
	var dtypelen = frm.nudonationtype.length; // Donation type length
	var i=j=0;
	var mode = 0;
	var type = 0;
	for(i=1; i<=modelen; i++)
	{
		var t = String('num'+i);
		if(document.getElementById(t).checked)
			frm.numode.value=document.getElementById(t).value;
		else
			mode++;
	}
	for(j=1; j<=dtypelen; j++)
	{
		var te = String('nut'+j);
		if(document.getElementById(te).checked)
			frm.nudontype.value=document.getElementById(te).value;
		else
			type++;
	}

	if(modelen == mode)
		frm.numode.value="error";
	if(dtypelen == type)
		frm.nudontype.value="error";

	frm.act.value = 'nuset';
	frm.submit();
}


// Donation amount input field for Offline donation of Existing user
function eunumbersonly(e,frm)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	var field = frm.euamount; // Donate field

		if (unicode!=8)
		{ //if the key isn't the backspace key (which we should allow)
			if((field.value).length == 0 && unicode == 48) // restrict if first character is 0(zero)
				return false; //disable key press
			else{
				if (unicode<48||unicode>57) //if not a number
				return false; //disable key press
			}
		}
}
function euRemoveZero(frm)
{
	var field = frm.euamount; // Donate field
	if ((field.value).length == 1 && field.value == 0)
	{
		field.value = '';
	}
	else if((field.value).length > 0 && (field.value).charAt(0) == 0)
	{
		field.value = (field.value).replace("0","");
	}
}

// Donation amount input field for Offline donation of New user
function nunumbersonly(e,frm)
{
	var unicode=e.charCode? e.charCode : e.keyCode
	var field = frm.nuamount; // Donate field

		if (unicode!=8)
		{ //if the key isn't the backspace key (which we should allow)
			if((field.value).length == 0 && unicode == 48) // restrict if first character is 0(zero)
				return false; //disable key press
			else{
				if (unicode<48||unicode>57) //if not a number
				return false; //disable key press
			}
		}
}
function nuRemoveZero(frm)
{
	var field = frm.nuamount; // Donate field
	if ((field.value).length == 1 && field.value == 0)
	{
		field.value = '';
	}
	else if((field.value).length > 0 && (field.value).charAt(0) == 0)
	{
		field.value = (field.value).replace("0","");
	}
}

function fnSearch(frm,page)
{
	var fromdate = document.getElementById('fromdate')
	var todate = document.getElementById('todate')
	if(fromdate.value == "" && todate.value != "")
	{
		alert("Please select From date");
		return;
	}
	else if(fromdate.value != "" && todate.value != "")
	{
		if(!comparetwodates(fromdate,todate))
		{
			alert("To date must be later than From date");
			fromdate.focus();
			return;
		}
	}
	
	frm.act.value="search";
	frm.action = page;
	frm.submit();
}

function comparetwodates(elem1,elem2){
	 start_date = elem1.value.split("-");
	 end_date = elem2.value.split("-")
	// Access each element in the array.
	yr1 = start_date[2];
	mon1 = start_date[1];
	dt1 = start_date[0];
	yr2 = end_date[2];
	mon2 = end_date[1];
	dt2 = end_date[0];
	var date1 = new Date(yr1, mon1, dt1);
	var date2 = new Date(yr2, mon2, dt2);
	if(date1 > date2)
	{
		
		return false;
	}else
		return true;
}

// sort table data
function fnSortTableFields(frm,sortfld,page)
{
	if(frm.sortorder.value != "" && frm.sortorder.value == "DESC")
		frm.sortorder.value="ASC";
	else if(frm.sortorder.value != "" && frm.sortorder.value == "ASC")
		frm.sortorder.value="DESC";
	else
		frm.sortorder.value="DESC";

	frm.sortfld.value = sortfld;
	frm.action = page;
	frm.submit();
}

// Popup window to upload photos for the content management
function fnCMSPhotos()
{
	window.open('cmsphotoupload.php','','width=700,height=430,scrollbars=1,resizable=1,left=200,top=200');
}

function fnViewAllPhotos()
{
	window.open('viewallphotos.php','','width=720,height=585,scrollbars=1,resizable=1,left=200,top=150');
}