function salecheck(formField, fieldLabel, formval)
{
	var result = true;
  if (formval.value == 1)	
  {
	
	if (formField.value == '')
	{
		alert('Please enter a value for the "' + fieldLabel + '"field.');
		formField.focus();
		result = false;
	}
	else
	{
	var num = parseInt(formField.value);
		if (isNaN(num) || formField.value == 0)
		{
			alert('Please enter a number for the "' + fieldLabel + '"field. The value must not be zero(0)');
			formField.focus();
			result = false;
		}
	}
	return result;
  }
  else
  {
  return result
  }

}

//////////////////////////////////////////////////////////////////////////////////////////////
function isEmailAddr(email)
{
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf('@');
	if (index > 0)
	{
		var pindex = theStr.indexOf('.',index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
	}
	return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
function validRequired(formField, fieldLabel)
{
	var result = true;
	if (formField.value == '')
	{
		alert('Please enter a value for the "' + fieldLabel + '"field.');
		formField.focus();
		result = false;
	}
	return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
function validEmail(formField,fieldLabel,required)
{
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert('Please enter a complete email address in the form: yourname@yourdomain.com');
		formField.focus();
		result = false;
	}
	return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
function validNum(formField,fieldLabel,required)
{
	var result = true;
	if (required && !validRequired(formField,fieldLabel))
		result = false;
	if (result)
	{
		var num = parseInt(formField.value);
		if (isNaN(num) || formField.value == 0)
		{
			alert('Please enter a number for the "' + fieldLabel + '"field. The field must not be zero(0)');
			formField.focus();
			result = false;
		}
	}
	return result;
	}
//////////////////////////////////////////////////////////////////////////////////////////////
function validDate(formField, fieldLabel, required)
{
	var result = true;
	if (required && !validRequired(formField, fieldLabel))
		result = false;
	if (result)
	{
		var elems = formField.value.split('/');
		result = (elems.length == 3); // should be three components
		if (result)
		{
			var day = parseInt(elems[0]);
			var month = parseInt(elems[1]);
			var year = parseInt(elems[2]);
			result = !isNaN(month) && (month > 0) && (month < 13) && !isNaN(day) && (day > 0) && (day < 32) && !isNaN(year) && (elems[2].length == 4);
		}
		if (!result)
		{
			alert('Please enter Date (DD/MM/YYYY)  for the "' + fieldLabel + '" field.');
			formField.focus();
		}
	}
	return result;
	}
//////////////////////////////////////////////////////////////////////////////////////////////
function IsValidTime(formField, fieldLabel, required) 
{
	if (required && !validRequired(formField, fieldLabel))
		return false;
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	var timeStr = formField.value
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null)
	{
		alert('Please enter time for the "' + fieldLabel + '"field.');
		formField.focus();
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	if(second == '')
	{
		second = null; 
	}
	if(ampm == '')
	{
		ampm = null
	}
	if(hour < 0  || hour > 23)
	{
		alert(' Hour must be between 1 and 12. (or 0 and 23 for military time) ');
		formField.focus();
		return false;
	}
	if(hour <= 12 && ampm == null)
	{
		if (confirm(' Please indicate which time format you are using for " + fieldLabel + ". \n OK = Standard Time \n CANCEL = Military Time'))
		{
			alert('You must specify AM or PM.');
			formField.focus();
			return false;
		}
	}
	if  (hour > 12 && ampm != null)
	{
		alert(' You cant specify AM or PM for military time. ');
		formField.focus();
		return false;
	}
	if(minute < 0 || minute > 59)
	{
		alert ('Minute must be between 0 and 59.');
		formField.focus();
		return false;
	}
	if(second != null && (second < 0 || second > 59))
	{
		alert ('Second must be between 0 and 59.');
		formField.focus();
		return false;
	}
	return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//PLACE THIS FUNCTION IN EACH PAGE ACCORDING TO FORM REQUIREMENT.
//////////////////////////////////////////////////////////////////////////////////////////////
/*
function validateForm(theForm)
{
	if (!validRequired(theForm.username,'User Name'))
		return false;
	if (!validEmail(theForm.email,'Email Address',true))
		return false;
	if (!validDate(theForm.birthdate,'Birth Date',true))
		return false;
	if (!IsValidTime(theForm.time))
		return false;
	return true;
}
*/
//////////////////////////////////////////////////////////////////////////////////////////////
/*###################################################################################################################################
	Returns you the HTTP request.	
###################################################################################################################################*/
	function getHttpObject(){
		var xhttp = null;
		xhttp = (document.all)?new ActiveXObject("MSXML2.XMLHTTP"):new XMLHttpRequest();
		return (xhttp)?xhttp:false;
	}//end of function
/*###################################################################################################################################
	The function will return the option element
###################################################################################################################################*/
	function createOptElement(value, selected, text){
		var opt = document.createElement("option");
		opt.value = value;
		opt.text = text;
		opt.selected = selected;
		return opt;
	}//end of function
/*###################################################################################################################################
	This functin will remove all options element from the select Box
###################################################################################################################################*/
function RemoveAll(dest){
	dest = document.getElementById(dest);
	var len = dest.options.length;
	for(i=0;i<len;i++){
		if(document.all)
			dest.remove(0);
		else
			dest.remove(0);
	}
}//end of fucntion
/*###################################################################################################################################
	This functin will remove all options element from the select Box
###################################################################################################################################*/
function test(obj){
	 str = "";
	 try{
		 for(i in obj)
			str += i + " = " + obj[i] + "\n";
		alert(str);
	 }
	 catch(e){
	 	return false;
	 }
}//end of fucntion
function addOptElement(opt , dest){
	var destlen = dest.options.length;
//	dest.options.add(opt,null);
//	return false;
	if(document.all)
		dest.add(opt);
	else
		dest.options.add(opt,null);
}//end of fucntion
/*###############################################################################################################
###################################################################################################################*/
function round_decimals(original_number, decimals){
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}//end of function
/*###############################################################################################################
###################################################################################################################*/
function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString();
    var decimal_location = value_string.indexOf(".");
    if (decimal_location == -1){
        decimal_part_length = 0;
        value_string += decimal_places > 0 ? "." : "";
    }
    else{
		decimal_part_length = value_string.length - decimal_location - 1;
    }
    var pad_total = decimal_places - decimal_part_length;
    if (pad_total > 0){
		for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
    }
    return value_string;
}//end of function
/*###############################################################################################################
###################################################################################################################*/

