// JavaScript Document

function fillProductAmount(ele, groupid){
	var target = document.getElementById('amount_'+groupid);
	if(ele.checked){
		target.value = '0.00';
	}
	if(!ele.checked){
		target.value = '';
	}
}

function fillProductCheck(ele, groupid){
	var target = document.getElementById('check_'+groupid);
	if(ele.value.length > 0){
		target.checked = true;
	}
	if(ele.value.length == 0){
		target.checked = false;
	}
}


function validCheckboxAlerts(checkboxAlerts){
	if (checkboxAlerts == ''){
		checkboxAlerts = new Array();//if the argument is null an empty array is created
		} else {			
			checkboxAlerts = checkboxAlerts.split(",");//the split function creates an array is the argument contains data
	}
	for(var i=1; i<checkboxAlerts.length; i++){
		if(document.getElementById('checkbox_'+checkboxAlerts[i]).checked == false){
			if(!confirm('You have not checked the option "'+document.getElementById('checkbox_'+checkboxAlerts[i]).alt+'"\r\n\nOK = Continue with processing.\r\nCancel = Return to entry screen.')){
				return false;
			}	
		}
	}
	return true;
}



function validBM(form){
	var arBMfields = new Array();
	if(form.bmid_1){
		arBMfields[0] = form.bmid_1;
	}
	if(form.bmid_2){
		arBMfields[1] = form.bmid_2;
	}
	if(arBMfields.length == 0){
		return true;
	}
	for(var i=0; i<arBMfields.length; i++){
		if(arBMfields[i].value > 0){
			return true;
		}
	}
	return false;
}

function validSR(form){
	var arSRfields = new Array();
	if(form.srid_1){
		arSRfields[0] = form.srid_1;
	}
	if(form.srid_2){
		arSRfields[1] = form.srid_2;
	}
	if(form.srid_3){
		arSRfields[2] = form.srid_3;
	}
	if(arSRfields.length == 0){
		return true;
	}
	for(var i=0; i<arSRfields.length; i++){
		if(arSRfields[i].value > 0){
			return true;
		}
	}
	return false;
}




function validateEntry(form, stExempt){

	checked_data = getCheckedObject(document.getElementsByName("field[paymenttypeid]")).id;
	arChecked_data = checked_data.split(':');
	if(parseInt(arChecked_data[1]) == 1){
		stExempt += ',bankid,contract_term,contract_buy,contract_sell,contract_amt,payment_in,payment_out';
	}
	if(!validForm(form, stExempt)){
		return false;
	}	
	
	// We are going through a series of checks.
	// If any "important" parts are not entered, the user will be given the choice to
	// continue with processing or stop to enter such data.
	
	
	
	if(!validBM(form)){
		if(!confirm('You have not entered any Business Manager\r\n\nOK = Continue with processing.\r\nCancel = Return to entry screen.')){
			return false;
		}		
	}
	
	if(!validSR(form)){
		if(!confirm('You have not entered any Sales Representatives\r\n\nOK = Continue with processing.\r\nCancel = Return to entry screen.')){
			return false;
		}		
	}
	
	if(form.checkboxAlerts){
		if(!validCheckboxAlerts(form.checkboxAlerts.value)){
			return false;
		}
	}
	
	if(form.paymenttype_1 && form.paymenttype_1.checked){
		if((form.contract_term && form.contract_term.value.length) || (form.contract_amt && form.contract_amt.value.length) || (form.contract_buy && form.contract_buy.value.length) || (form.contract_sell && form.contract_sell.value.length)){
			if(!confirm('You have entered contract data and set Payment Method to "Cash."\r\nAny contract data entered will not be stored.\r\n\nOK = Continue with processing.\r\nCancel = Return to entry screen.')){
				return false;
			}	
		}	
	}
	return true;
}
