/* Author: Ankita Bhavsar
   Description: This Javacsript code is for Goods module 
*/

//function to validate Donate Goods form

function validateDonateGoods()
{
	var itemtype_id = document.getElementById('itemtype_id');
	var item_other_type = document.getElementById('item_other_type');
	//  Do not remove ths code 
	//var item_name = document.getElementById('item_name');
	//var item_description = document.getElementById('item_description');
	var item_currentquantity = document.getElementById('item_currentquantity');
	var item_condition = document.getElementById('item_condition');
	var item_fairvalue = document.getElementById('item_fairvalue');

	var item_pickdeadline = document.getElementById('item_pickdeadline');

	if(!empty(itemtype_id))
	{
		retFalse(itemtype_id,ERR['donateGoods']['itemtype_id']);
		return false;
	}
	else
	{
		retTrue(itemtype_id);
	}
	if(itemtype_id.value == '0')
	{
		if(!empty(item_other_type) || !alpha(item_other_type))
		{
			retFalse(item_other_type,ERR['donateGoods']['item_other_type']);
			return false;
		}
		else
		{
			retTrue(item_other_type);
		}
	}

	/* Do not remove ths code
	
	if(!empty(item_name))
	{
		retFalse(item_name,ERR['donateGoods']['item_name']);
		return false;
	}
	else
	{
		retTrue(item_name);
	}*/

	/*if(!empty(item_description))
	{
		retFalse(item_description,ERR['donateGoods']['item_description']);
		return false;
	}
	else
	{
		retTrue(item_description);
	}*/

	if(!empty(item_currentquantity) || !intNumber(item_currentquantity,0))
	{
		retFalse(item_currentquantity,ERR['donateGoods']['item_currentquantity']);
		return false;
	}
	else
	{
		retTrue(item_currentquantity);
	}

	if(!empty(item_condition))
	{
		retFalse(item_condition,ERR['donateGoods']['item_condition']);
		return false;
	}
	else
	{
		retTrue(item_condition);
	}
	
	if(!empty(item_pickdeadline))
	{
		retFalse(item_pickdeadline,ERR['donateGoods']['item_pickdeadline']);
		return false;
	}
	else
	{
		retTrue(item_pickdeadline);
	}
	
	//alert(item_fairvalue.value);
	//alert(!isFloat(item_fairvalue.value));
	if(!IsNumeric(item_fairvalue.value))
	{
		retFalse(item_fairvalue,ERR['donateGoods']['item_fairvalue']);
		return false;
	}
	else
	{
		retTrue(item_fairvalue);
	}

}


//function to validate Request Goods form

function validateRequestGoods()
{
	var itemtype_id = document.getElementById('itemtype_id');
	var item_other_type = document.getElementById('item_other_type');
	//var item_name = document.getElementById('item_name');
	//var item_description = document.getElementById('item_description');
	var item_requestedquantity = document.getElementById('item_requestedquantity');

	if(!empty(itemtype_id))
	{
		retFalse(itemtype_id,ERR['donateGoods']['itemtype_id']);
		return false;
	}
	else
	{
		retTrue(itemtype_id);
	}
	if(itemtype_id.value == '0')
	{
		if(!empty(item_other_type) || !alpha(item_other_type))
		{
			retFalse(item_other_type,ERR['donateGoods']['item_other_type']);
			return false;
		}
		else
		{
			retTrue(item_other_type);
		}
	}

	/*if(!empty(item_name))
	{
		retFalse(item_name,ERR['donateGoods']['item_name']);
		return false;
	}
	else
	{
		retTrue(item_name);
	}*/

	/*if(!empty(item_description))
	{
		retFalse(item_description,ERR['donateGoods']['item_description']);
		return false;
	}
	else
	{
		retTrue(item_description);
	}*/

	if(!empty(item_requestedquantity) || !intNumber(item_requestedquantity,0))
	{
		retFalse(item_requestedquantity,ERR['donateGoods']['item_requestedquantity']);
		return false;
	}
	else
	{
		retTrue(item_requestedquantity);
	}
}

//Function to enable other field type
function changetoOther(value)
{
	if(value == "0")
	{
		document.getElementById("item_other_type").disabled = false;
	}
	else
	{
		document.getElementById("item_other_type").value = "";
		document.getElementById("item_other_type").className = "txtboxsearch3";
		document.getElementById('item_other_typeErr').innerHTML = "";
		document.getElementById("item_other_type").disabled = true;
	}
}
function goToRequest(url) {

    if(confirm(ERR['confirm']['request']))
    {
        window.location=url;
    }
    else
    return false;
}

function confirmRequest(form, qty , dateneeded, countRows)
{
	var countQty = reqqty_textboxCount(form,countRows);	
	if(countQty == 1)
	{
		alert("Please Enter Valid Qty");
		return false;
	}
	else {
	/*else if(countDateneeded==1)
	{
		alert("Please Select date");
		return false;
	}*/
	//var countQtyVal = req_textboxValueCount(form, dateneeded);
	//alert(countQtyVal);
	
	//return false;
	
	
	var isGrater = 0;
	var isDateEmpty = 0;
	
		if(confirm(ERR['confirm']['request']))
			{				
			/*	if(countRows == 1)
				{			
					var currentQty = (document.checkAllForm.item_currentqty.value == "") ? currentQty="0" : currentQty=document.checkAllForm.item_currentqty.value;
					
					var approvedQty = (document.checkAllForm.item_approvedqty.value == "") ? approvedQty="0" : approvedQty=document.checkAllForm.item_approvedqty.value;
					
					 if(parseInt(approvedQty) > parseInt(currentQty))
					 {							
						 isGrater = parseInt(isGrater) + 1;			
					 }
					 else 
					 {					
						totalVal = parseInt(totalVal) + parseInt(approvedQty);	
					 }
				}
				else 
				{*/				
					for(i=0;i<countRows;i++)
					{							
					    var itemVal = parseInt(document.frm_viewalldonation.itemid[i].value);
						var item_qtyreq = "item_qtyreq-"+itemVal;
						var item_remainingqty = "item_remainingqty-"+itemVal;
						var item_dateneeded = "item_dateneeded-"+itemVal;				
						var item_qtyreq = (document.getElementById(item_qtyreq).value == "") ? item_qtyreq="0" : item_qtyreq=document.getElementById(item_qtyreq).value;
						var item_remainingqty = (document.getElementById(item_remainingqty).value == "") ? item_remainingqty="0" : item_remainingqty=document.getElementById(item_remainingqty).value;
						var item_dateneeded = document.getElementById(item_dateneeded).value;
						
						if(parseInt(item_qtyreq) > parseInt(item_remainingqty)) 
						{
							 isGrater = parseInt(isGrater) + 1;		
						}
						else if(item_qtyreq != "0" && item_dateneeded == "")
						{
							isDateEmpty = parseInt(isDateEmpty) + 1;		
						}						
					}
				}	
				
				if(isGrater > 0) {				
				   alert(ERR['select']['reqqty']);
				   return false;
				}
				else if(isDateEmpty>0)
				{
				   alert(ERR['select']['enterdate']);
				   return false;
				}
				else 
				{				
				       
						form.mode.value = "request_goods";						
						form.submit();					
				 }
			}
		
	//} 
	
	return false;
	
	
}
