
function validate_form(args,action){
//$('new-listing').select('input', 'textarea', 'select'); 
	var open_li = '<li>';
	var close_li = '</li>';
	var msg = '';
	var formOK = false;
	var split_args;
	var sectionID;
	var sectionName;
	var prod_sel='';
	var micro_sel='';
	//read values from form to validate
	var acres = window.frames[0].document.getElementById('acres');
	var prod_used = window.frames[0].document.getElementById('prod_use');
	var prod_rate1 = window.frames[0].document.getElementById('prod_rate1');
	var prod_rate2 = window.frames[0].document.getElementById('prod_rate2');
	var turnkey_price = window.frames[0].document.getElementById('turnkey_price');
	var tract_size = window.frames[0].document.getElementById('tract_size');
	var micro_rate = window.frames[0].document.getElementById('micro_rate');
	var micro = window.frames[0].document.getElementById('micro');
	var bidder1 = window.frames[0].document.getElementById('bidder1');
	var bidder2 = window.frames[0].document.getElementById('bidder2');
	var bidder3 = window.frames[0].document.getElementById('bidder3');
	var bidder4 = window.frames[0].document.getElementById('bidder4');
	var price1 = window.frames[0].document.getElementById('price1');
	var price2 = window.frames[0].document.getElementById('price2');
	var price3 = window.frames[0].document.getElementById('price3');
	var price4 = window.frames[0].document.getElementById('price4');
	var winnerBid = window.frames[0].document.getElementById('winnerBid');	
	var radioResult = getRadioValue();
	
	//replace white spaces
	var regex = /^[ \t]+|[ \t]+$/;
	args = args.replace(regex,""); 
	//alert(args);
	//The sectionId and SectionName needs no quotes in order to work on the getInfoSection function
	//args = args.replace('validate_form("',""); 
	//args = args.replace('"',""); 
	//args = args.replace(')',""); 
	split_args = args.split(",");
	sectionName = split_args[0];
	sectionID = split_args[1];
	
	prod_sel = getSelectedValue(prod_used);
	micro_sel = getSelectedValue(micro);
	if (!isNumeric(acres)){
		msg = open_li + 'Invalid Acres entered' + close_li;
	}
	if (prod_sel == "DAP-UREA"){
		if (!isNumeric(prod_rate1)){
			msg = msg + open_li + 'Invalid DAP Rate entered' + close_li;
		}
		if (!isNumeric(prod_rate2)){
			msg = msg + open_li + 'Invalid UREA Rate entered' + close_li;
		}
	}else if (prod_sel == "UREA"){
		if (!isNumeric(prod_rate1)){
			msg = msg + open_li + 'Invalid UREA Rate entered' + close_li;
		}
	}else if(prod_sel == "DAP"){
		if (!isNumeric(prod_rate1)){
			msg = msg + open_li + 'Invalid DAP Rate entered' + close_li;
		}
	}
	if (!isNumeric(turnkey_price)){
		msg = msg + open_li + 'Invalid Turnkey Price entered' + close_li;
	}
	if (!isNumeric(tract_size)){
		msg = msg + open_li + 'Invalid Tract Size entered' + close_li;
	}
	if (Len(bidder1.value) <= 0){
		msg = msg + open_li + 'Need Bidder1 Name' + close_li;
	}
	if (!isNumeric(price1)){
		msg = msg + open_li + 'Invalid Bidder1 Price entered' + close_li;
	}
	//Extra validation in case user enters other bidders
	if (Len(bidder2.value) > 0 && !isNumeric(price2)){
		msg = msg + open_li + 'Empty or Invalid Bidder2 Price ' + close_li;
	}
	else if (Len(price2.value) > 0){
		if (!isNumeric(price2)){
			msg = msg + open_li + 'Invalid Bidder2 Price ' + close_li;
		}
		if (isNumeric(price2) && Len(bidder2.value) <= 0){
			msg = msg + open_li + 'Empty Bidder2 Name ' + close_li;
		}
	}
	if (Len(bidder3.value) > 0 && !isNumeric(price3)){
		msg = msg + open_li + 'Empty or Invalid Bidder3 Price ' + close_li;
	}
	else if (Len(price3.value) > 0){
		if (!isNumeric(price3)){
			msg = msg + open_li + 'Invalid Bidder3 Price ' + close_li;
		}
		if (isNumeric(price3) && Len(bidder3.value) <= 0){
			msg = msg + open_li + 'Empty Bidder3 Name ' + close_li;
		}
	}
	if (Len(bidder4.value) > 0 && !isNumeric(price4)){
		msg = msg + open_li + 'Empty or Invalid Bidder4 Price ' + close_li;
	}
	else if (Len(price4.value) > 0){
		if (!isNumeric(price4)){
			msg = msg + open_li + 'Invalid Bidder4 Price ' + close_li;
		}
		if (isNumeric(price4) && Len(bidder4.value) <= 0){
			msg = msg + open_li + 'Empty Bidder4 Name ' + close_li;
		}
	}

	if (micro_sel == 0 && Len(micro_rate.value) > 0){
		msg = open_li + 'Need to select Micronutrient' + close_li;
		if (!isNumeric(micro_rate)){
			msg = open_li + 'Invalid Micro rate entered' + close_li;
		}
	}
	if (micro_sel > 0 && Len(micro_rate.value) >= 0){
		if (!isNumeric(micro_rate)){
			msg = msg + open_li + 'Empty or invalid micro rate ' + close_li;
		}
	}	
	
	if (!radioResult){
		msg = msg + open_li + 'Select a Winner Bid' + close_li;
	}
	if (Len(msg) > 0){
		window.frames[0].document.getElementById('msg').style.display = '';	
		window.frames[0].document.getElementById('msg').innerHTML = msg;
		formOK = false;
	}else{
		window.frames[0].document.getElementById('msg').style.display = 'none';	
		formOK = true;
	}
	
	if (formOK){
		//to submit parent form from iframe 
		//window.parent.form[0].submit();
		//to submit form from iframe
		//window.frames[0].close();
		window.frames[0].document.forms[0].submit();
		//window.frames[0].document.getElementById('list-container').innerHTML = "";
		//closing LyteBox
		//setTimeout("clearDivs()",300);
		//setTimeout("self.parent.myLytebox.end()",300);
		//calling function to refresh section
		 //alert('getInfoSection('+sectionName+','+sectionID+')');
		//var t = setTimeout("getInfoSection(sectionName,sectionID)",300);
		self.parent.myLytebox.end();
		getInfoSection(sectionName,sectionID);
		//setTimeout("getInfoSection(" + sectionName + "," + sectionID + ")", 200);
	}
//	alert(window.frames[0].document.getElementById('prod_rate').value);

	//var item = document.getElementById('frmListing').getElementById('contacts').value; 
	//alert(item);
//var inputs = document.getElementsByName('acres');
	//alert(document.getElementById('acres').value);
	//alert(window.parent.opener.document.frmListing.acres.value);
    // if ( document.frmListing.acres.value == "" )
    // {
        // alert ( "Please fill in the 'Your Name' box." );
        // valid = false;
    // }

    //alert valid;
//alert(document.frmListing.acres.value);
}

function clearDivs(){
	window.frames[0].document.getElementById('list-container').innerHTML = "";//alert(1);
	self.parent.myLytebox.end();
	getInfoSection(sectionName,sectionID);
}

function checkProduct(item){
    var sel = getSelectedValue(item);
	if (sel == "DAP-UREA"){
		document.getElementById('lbProdrate1').innerHTML='DAP Rate in lbs/acres';
		document.getElementById('lbProdrate2').style.display='';
		document.getElementById('elProdrate2').style.display='';
	}else{
		document.getElementById('lbProdrate1').innerHTML='Product Rate in lbs/acres';
		document.getElementById('lbProdrate2').style.display='none';
		document.getElementById('elProdrate2').style.display='none';
	}
}

function delListing(fert_id,sectionName,sectionID){
	var r=confirm("Delete this listing?");
	if (r==true){
		DWRUtil.useLoadingMessage('Deleting Listing...');
		DWREngine._execute('/components/intranet.cfc', null, 'deleteFertilizing', fert_id,sectionName,sectionID,fert_results);	
	}else{
		return false;
	}
}

// call back function for getInfoSection()
function fert_results (r) {
	var section_id = r.r1;
	var section_name = r.r2;
	//calling function to refresh section
	getInfoSection(section_name,section_id);
	LyteBox.prototype.updateLyteboxItems();	
}

function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
	
	var btn = valButton(myForm.winnerBid);
	
	if (btn == null) alert('No radio button selected');
	else alert('Button value ' + btn + ' selected');
	//alert(winnerBid.value);
	return false;
}
function getRadioValue()
{
	var checked=0;
	//var inputnodes = document.getElementsByTagName('input');	
	var inputnodes = window.frames[0].document.getElementsByTagName('input');	
	var dd = window.frames[0].document.getElementById('winnerBid');
	
	//loop through all the inputs to find all the radio buttons
	for(i=0; i<inputnodes.length; i++)
	{	//alert(inputnodes[i].name);
		if(inputnodes[i].type=="radio" && inputnodes[i].checked){
			//optionnodes = document.getElementById('opt'+inputnodes[i].value);
			//optValues = optValues + ',' + optionnodes.value;
			checked ++;
		}
	}
	if (checked > 0){
		return true;
	}else{
		return false;
	}
}
