// JSHead Functions
// Assumes the Mindwatering & Rocking Chair Software Validation functions are being used. Assumes the Mindwatering GTIN/UPC validation function is being used. (They are in the MW repository.)

function FldGetVal(fldobj, splitchars) {
	var fldArray;
	if (fldobj==null) {
		fldArray = new Array();
		fldArray[0] = '';
		return fldArray;
	}
	var fldvals = fldobj.value
	fldArray = fldvals.split(splitchars);
	return fldArray;
}
function FldSetVal(fldobj, fldArray) {
	if (fldobj==null) {
		return false;
	}
	fldobj.value = fldArray;
}
function FldInsertVal(fldobj, fldArray, indexnum, newval, splitchars) {
	if (fldobj==null) {
		return false;
	}
	if (newval=='') {
		newval = ' ';
	}
	if (fldArray[0]=='') {
		fldobj.value = newval;
	} else {
		fldArray.splice(indexnum, 0, newval);
		fldobj.value = fldArray.join(splitchars);
	}
}
function FldReplaceVal(fldobj, fldArray, indexnum, newval, splitchars) {
	if (fldobj==null) {
		return false;
	}
	if (newval=='') {
		newval = ' ';
	}
	fldArray[indexnum] = newval;
	fldobj.value = fldArray.join(splitchars);
}
function FldRemoveVal(fldobj, fldArray, indexnum, splitchars) {
	if (fldobj==null) {
		return false;
	}
	fldArray.splice(indexnum, 1);
	fldobj.value = fldArray.join(splitchars);
}
function FldToggleUpdate(fldobj, fldArray) {
	if (fldobj==null) {
		return false;
	}
	fldlen = fldArray.length;;
	fldobj.options.length = 0;
	for (i=0; i<=fldlen; i++) {
		if (i == 0) {
			fldobj.options[fldobj.options.length] = new Option( 'New' );
		} else {
			fldobj.options[fldobj.options.length] = new Option( i );
		}
	}
}
function CheckRowFields() {
	hasErrors = false;
	errorMsgs = '';
	if(!CheckField(f.D_UPCIn,'The UPC field is required.')) hasErrors = true;
	if(!CheckField(f.D_PLUIn,'The Item PLU field is required. Enter 0 if there is no Item Number.')) hasErrors = true;
	if(!CheckField(f.D_DescIn,'The Description field is required.')) hasErrors = true;
	if(CheckButton(f.D_IsPLUIn, '', 'Y') ) { 
		// plu not upc
	} else {
		// upc - check number entered if not blank (blank already checked)
		if(f.D_UPCIn.value !='') {
			if(checkGTIN(f.D_UPCIn)== false) {
				hasErrors = true;
			}
		}
	}
	
	if(hasErrors==true) {
		if (errorMsgs !='') {
			alert(errorMsgs);
		}
       	return false;
    	} else {
     	return true;
     }
}
function UpdateRowFields() {
	// get row selection value
	var rowcurr = f.D_RowSel.selectedIndex;
	rowcurr = rowcurr;
	// get existing values
	fldArray1 = FldGetVal(f.D_UPC, '\n');
	fldArray2 = FldGetVal(f.D_IsPLU, '\n');
	fldArray3 = FldGetVal(f.D_PLU, '\n');
	fldArray4 = FldGetVal(f.D_IsDamage, '\n');
	fldArray5 = FldGetVal(f.D_Desc, '\n');
		
	// get current row values
	fldval1 = f.D_UPCIn.value;
	fldval2 = GetButtonVal(f.D_IsPLUIn);
	fldval3 = f.D_PLUIn.value;
	fldval4 = GetButtonVal(f.D_IsDamageIn);
	fldval5 = f.D_DescIn.value;
	
	// insert of update row
	if (rowcurr == 0 ) {
		// check if over limit
		if (CheckFldMax(f.D_UPC, 15)) {					// update to desired limit and update alert note below
			alert ('Cannot add another line. Maximum number of lines reached (16).');
			return false;
		}
		// insert new row values (at top)
		FldInsertVal(f.D_UPC, fldArray1, 0, fldval1, '\n');
		FldInsertVal(f.D_IsPLU, fldArray2, 0, fldval2, '\n');
		FldInsertVal(f.D_PLU, fldArray3, 0, fldval3, '\n');
		FldInsertVal(f.D_IsDamage, fldArray4, 0, fldval4, '\n');
		FldInsertVal(f.D_Desc, fldArray5, 0, fldval5, '\n')
	} else {
		// update row
		FldReplaceVal(f.D_UPC, fldArray1, rowcurr - 1, fldval1, '\n');
		FldReplaceVal(f.D_IsPLU, fldArray2, rowcurr - 1, fldval2, '\n');
		FldReplaceVal(f.D_ItemNum, fldArray3, rowcurr - 1, fldval3, '\n');
		FldReplaceVal(f.D_IsDamage, fldArray4, rowcurr - 1, fldval4, '\n');
		FldReplaceVal(f.D_Desc, fldArray5, rowcurr - 1, fldval5, '\n');
	}
	// reset temp row
	f.D_UPCIn.value = '';
	f.D_PLUIn.value = '';
	f.D_DescIn.value = '';
	// update the toggle field
	FldToggleUpdate(f.D_RowSel, fldArray1);
	// update the row numbers
	f.D_RowNum.value = GetRowNumStr(fldArray1);
	// update the field sizes
	SetRowFldHeights(fldArray1);
}
function RemoveRow() {
	// get row selection value
	var rowcurr = f.D_RowSel.selectedIndex;
	if (rowcurr == 0) {
		// cancel
		alert ('Please select a row number before clicking the Remove button.')
		return false;
	}
	// get existing values
	fldArray1 = FldGetVal(f.D_UPC, '\n');
	fldArray2 = FldGetVal(f.D_IsPLU, '\n');
	fldArray3 = FldGetVal(f.D_PLU, '\n');
	fldArray4 = FldGetVal(f.D_IsDamage, '\n');
	fldArray5 = FldGetVal(f.D_Desc, '\n');
	
	// remove the selected row from the array
	FldRemoveVal(f.D_UPC, fldArray1, rowcurr - 1, '\n');
	FldRemoveVal(f.D_IsPLU, fldArray2, rowcurr - 1, '\n');
	FldRemoveVal(f.D_PLU, fldArray3, rowcurr - 1, '\n');
	FldRemoveVal(f.D_IsDamage, fldArray4, rowcurr - 1, '\n');
	FldRemoveVal(f.D_Desc, fldArray5, rowcurr - 1, '\n');

	// reset temp row
	f.D_UPCIn.value = '';
	f.D_PLUIn.value = '';
	f.D_DescIn.value = '';
	
	// update the row numbers
	f.D_RowNum.value = GetRowNumStr(fldArray1);
	// update the row heights
	SetRowFldHeights(fldArray1);
}
function ToggleGetRow() {
	// get row selection value
	var rowcurr = f.D_RowSel.selectedIndex;
	if (rowcurr == 0) {
		// reset temp row
		f.D_UPCIn.value = '';
		f.D_PLUIn.value = '';
		f.D_DescIn.value = '';
		return true;
	}
	// get existing values
	fldArray1 = FldGetVal(f.D_UPC, '\n');
	fldArray2 = FldGetVal(f.D_IsPLU, '\n');
	fldArray3 = FldGetVal(f.D_PLU, '\n');
	fldArray4 = FldGetVal(f.D_IsDamage, '\n');
	fldArray5 = FldGetVal(f.D_Desc, '\n');
	// set the temp fields
	f.D_UPCIn.value = fldArray1[rowcurr - 1 ];
	//SetButtonVal(f.D_IsPLUIn, fldArray2[rowcurr - 1 ]);				// leave at last value
	f.D_PLUIn.value = fldArray3[rowcurr - 1 ];
	//SetButtonVal(f.D_IsDamageIn, fldArray4[rowcurr - 1 ]);			// leave at last value
	f.D_DescIn.value = fldArray5[rowcurr - 1 ];
}
function CheckFldMax(fldobj, maxnum) {
	// returns if new row would be over limit
	if (fldobj==null) {
		return true;		// return over
	}
	var fldArray1 = FldGetVal(fldobj, '\n');
	var fldlen = fldArray1.length;
	if (fldlen > maxnum) {
		return true;
	} else {
		return false;
	}
}
function GetRowNumStr(fldArray1) {
	var fldabound = fldArray1.length;
	switch (fldabound) {
	case 0 :
		return '1';	
	case 1 :
		return '1';
	case 2 :
		return '1' + '\n' + '2';
	case 3 :
		return '1' + '\n' + '2' + '\n' + '3';
	case 4 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4';
	case 5 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5';
	case 6 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6';
	case 7 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7';
	case 8 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8';
	case 9 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9';
	case 10 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9' + '\n' + '10';
	case 11 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9' + '\n' + '10' + '\n' + '11';
	case 12 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9' + '\n' + '10' + '\n' + '11' + '\n' + '12';
	case 13 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9' + '\n' + '10' + '\n' + '11'  + '\n' + '12' + '\n' + '13';
	case 14 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9' + '\n' + '10' + '\n' + '11'  + '\n' + '12' + '\n' + '13' + '\n' + '14';
	case 15 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9' + '\n' + '10' + '\n' + '11'  + '\n' + '12' + '\n' + '13' + '\n' + '14' + '\n' + '15';
	case 16 :
		return '1' + '\n' + '2' + '\n' + '3'  + '\n' + '4' + '\n' + '5' + '\n' + '6' + '\n' + '7' + '\n' + '8' + '\n' + '9' + '\n' + '10' + '\n' + '11'  + '\n' + '12' + '\n' + '13' + '\n' + '14' + '\n' + '15' + '\n' + '16';
	}
	return '';
}
function SetRowFldHeights(fldArray1) {
	var fldabound = fldArray1.length + 1;
	fldObj = f.D_RowNum;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_UPC;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_IsPLU;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_PLU;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_IsDamage;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_Desc;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
}
function SetRowStartFldHeights() {
	// get field to array
	fldArray1 = FldGetVal(f.D_UPC, '\n');
	// set heights
	var fldabound = fldArray1.length + 1;
	fldObj = f.D_RowNum;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_UPC;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_IsPLU;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_PLU;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_IsDamage;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	fldObj = f.D_Desc;
	if (fldObj !=null && fldObj !='undefined') {
		fldObj.style.height = (fldabound * 16) + 'px';
	}
	return true;
}

// utility functions
function isnumsimple(numtock) {
	if (numtock.length ==0) {
		return false;
	}
	if (isNaN( parseFloat(numtock) )) {
		return false;
	}
	return true;
}
function chknum(myval) {
	var mychar;
	var mypoint = 0;
	var isvalidnumber;
	if (myval.length==0) {
		// cannot be nothing
		alert("Nothing is not a number. Enter a number.");
		return false;
	}
	for(var i = 0; i < myval.length; i++) {
		mychar = myval.charAt(i);
		if(mychar == ".") {
			mypoint++;
		}
	}
	if(mypoint > 1) {
		alert("Invalid Number: more than one decimal points.");
		return false;
	} else {
		for(var i = 0; i < myval.length; i++) {
			if(myval.charAt(i) != ".") {
				if(isNaN(myval.charAt(i))) {
					alert("Invalid Number: non numeric character entered.");
					return false;
				}
			}
		}
	}
}

function DoRightNum(bigstr, numchars) {
	strlen = String(bigstr).length;
	if (strlen <= + numchars) {
		// string is shorter or has same number of characters
		return bigstr;
	} else {
		resultstr = bigstr.substring(strlen, strlen - numchars);
		return resultstr;
	}
}

// validation functions - uses the Mindwatering and Rocking Chair Software functions - also in this repository
function GetButtonVal(field) {
	// get button value
	var selBtn = -1;
	for (i=field.length-1; i > -1; i--) {
		if (field[i].checked) {
		selBtn = i;
		}
	}
	if (selBtn == -1) {
		// no button selected
		return '';
	} else {
		// return button value
		return field[selBtn].value;
	}
}
function SetButtonVal(field, newvalue) {						// note: this function is not always working in various browser versions
	// validate button - field
	if( !field)
		return;
	var fldlength = field.length;
	if( fldlength == undefined) {
		field.checked = (field.value == newvalue.toString());
		return;
	}
	for(var i = 0; i < fldlength; i++) {
		field[i].checked = false;
		if(field[i].value == newvalue.toString()) {
			field[i].checked = true;
		}
	}
}