/*------------------------------------------------------------------------------------------------------------------------*/ 
/*
 * Author - Sri
 * Dated - 11/12/2009
 *
 * Comments - This file contains miscellaneous functions for doing dynamic math calculations.
 *
 */
/*------------------------------------------------------------------------------------------------------------------------*/ 

 /*
  * Multiply the source 'field's value with the price and put the resultant in the destination field in the HTML
  * Anything other than numbers in the input will turn the text input to red and won't follow through the calculation
  *
  */
function multiplyAndPopulate(field, destId, price) {
	var total, srcElement, dstElement, num;
	var check = true;
	num = field.value;
	
	//check that all characters are digits, ., -, or ""
	for(var i=0;i < num.length; i++)
	{
	   var new_key = num.charAt(i); //cycle through characters
	   if(((new_key < "0") || (new_key > "9")) && 
	        !(new_key == ""))
	   {
	        check = false;
	        break;
	   }
	}
	
	//apply appropriate colour based on value
	if(!check)
	{
		field.style.backgroundColor = "red";
	}
	else
	{
		total = num * price;
		document.getElementById(destId).value = total
		field.style.backgroundColor = "white";
	}
	return check;
}

 /*
  * Add the value of two fields and put it in the destination field.
  * Not scalable at the moment but can be made so later.
  *
  */
function addToDestination(srcId1, srcId2, destId) 
{
	var srcval1 = 0, 
	srcval2 = 0, 
	destval = 0;
	srcval1 = parseInt(document.getElementById(srcId1).value);
	srcval2 = parseInt(document.getElementById(srcId2).value);
	destval = srcval1 + srcval2;
	document.getElementById(destId).value =  destval;
	return true;
}


/*
 * Validate whether a field in the form is greater than zero or not
 */
function validate(field) {
	var amount = document.getElementById(field).value;
	if (amount == null || amount == "" || amount <= 0) {
		alert("Form cannot be submitted with zero amount.");
		return false
	}
	else {
		return true;
	}
}
