
// This lets us modify which type of report we're dealing with
// Expected values are ncmr, tdr, der
var reportType = 'ncmr';

function enableRMA(){
    rmaContactBox = new String('');
    rmaContactBox = '<input onchange="saveRMA()" type="text" id="RMA_CONTACT" name="RMA_CONTACT" value="">';
    rmaNumberBox = new String('');
    rmaNumberBox = '<input onchange="saveRMA()" type="text" id="RMA_NUM" name="RMA_NUM" value="">';
                       
    var contd = document.getElementById('RMA_CON_TD');
    var numtd = document.getElementById('RMA_NUM_TD');               
   
    con_string = new String('');
    num_string = new String('');
    
    var rma1 = document.getElementById('RMA1');
    var rma2 = document.getElementById('RMA2');
    var rmasave = document.getElementById('rmasave');
    
    
    //If Radio button is set to Yes
    if(rma1.checked && !rma2.checked){
        //get the current values
        con_string = Trim(contd.innerHTML);
        num_string = Trim(numtd.innerHTML);
        
        contd.innerHTML = rmaContactBox;
        numtd.innerHTML = rmaNumberBox;
              
        //enbale the contact box
        var contact = document.getElementById('RMA_CONTACT');
        //contact.value = con_string;

        //enable the number box
        var number = document.getElementById('RMA_NUM');
        //number.value = num_string;
    //If radio button is not set to yes
    }else{
        var contact = document.getElementById('RMA_CONTACT');
        var number = document.getElementById('RMA_NUM');
        
        con_string = contact.value;
        num_string = number.value

        contd.innerHTML = con_string;
        numtd.innerHTML = num_string;
    } 
        
}
function enableRMA2(){
    var contact = document.getElementById('RMA_CONTACT');
    var number = document.getElementById('RMA_NUM');
    var rma = document.getElementById('RMA');
    var rmasave = document.getElementById('rmasave');
    
    if(rma.checked){
        contact.disabled = false;
        number.disabled = false;
        rmasave.style.visibility = '';
	    //rmasave.style.display = 'block';
    }else{
        rmasave.style.visibility="hidden";
        //rmasave.style.display="none";
        contact.disabled = true;
        number.disabled = true;
    } 
        
}
function saveRMA(){
    var rmasave = document.getElementById('rmasave');
    rmasave.style.visibility = '';    
}
function enableQAC(which){
	document.getElementById('QA_REJECT_CODE').disabled = which;
}
function saveQAC(){
	document.getElementById('QA_REJECT1').value = "Y";
	document.getElementById('QA_REJECT1').checked = true;
}
function checkAttach(){
    var attdesc = document.getElementById('ATTACHMENT_DESC');
    if(attdesc.value == ''){
        alert('You must enter a description');
        return false;
    }
    var att = document.getElementById('attachment');
    if(att.value == ''){
        alert('You must choose a file to upload');
        return false;
    }
    return true;
}
function  qaRejectChange(){
    var yes = document.getElementById('qarejecty');
    var no = document.getElementById('qarejectn');
    var code = document.getElementById('REJECT_CODE');
    code.disabled = true;
    yes.checked = false;
    no.checked = true;
}
function  qaAcceptChange(){
    var yes = document.getElementById('qaapprovedy');
    var no = document.getElementById('qaapprovedn');
    var code = document.getElementById('REJECT_CODE');
    code.disabled = false;
    yes.checked = false;
    no.checked = true;
}
function Trim(TRIM_VALUE){
    if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);

	if(TRIM_VALUE==""){
    	return "";
    }else{
     	return TRIM_VALUE;
    }
}
function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";


	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;


	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){}


		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	}
	return strTemp;
}
function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;
}
var BBRespVal = '';
var RecLiVal  = '';
function checkBBRESP(obj){
	if(obj.value != '0'){
		//changed
		document.f.BBRESP.value = obj.value;
		BBRespVal = obj.value;
		if(BBRespVal == "Y"){
			//empty RecLi
			RecLiVal = document.f.RELI.value;
			document.f.RELI.value = '';
			document.f.RELIchanged.value = 1;
		}
	}else{
		//reset
		document.f.BBRESP.value = '';
		if(RecLiVal != ''){
			//replace RecLi
			document.f.RELI.value = RecLiVal;
			document.f.RELIchanged.value = '';
		}
	}
}
function checkNcmrForm(){
	//go thru each part
	
	//check bbresp
	var BBR = document.f.BBRESP;
	var RL = document.f.RELI;
	var patterns = [new RegExp('^RE[0-9]+-[0-9]+$','i'), new RegExp('^(|WO)[-0-9]+ [.0-9]+$','i'), new RegExp('^SH[0-9]+(| [0-9].*)$','i')];
	woPattern = '"### #';
	
	if(reportType == 'tdr'){
	    patterns = [new RegExp('^RE[0-9]+-[0-9]+$','i'), new RegExp('^(|WO)[-0-9]+(| [.0-9]+)$','i'), new RegExp('^SH[0-9]+(| [0-9].*)$','i')];
	    woPattern = '"###"';
	}
	
	if(RL){
		if(RL.value == ''){
	    	alert('You must enter a value for Wo/Re/Sh.');
    		return false;
		}
		var fail = true;
		for(var i=0;i<patterns.length;i++){
		    if(patterns[i].test(RL.value)){
		        fail = false;
		        break;
		    }
		}
		if(fail){
	    	alert('You must enter a valid value for Wo/Re/Sh in the form of '+woPattern+' or "RE###-#" or "SH###" or "SH### #"');
    		return false;
		}
	}
	if(BBR && RL && BBR.value == 'N'){
		if(document.f.RELIchanged.value && !(/^RE[0-9]+-[0-9]+$/i.test(RL.value))){
   	    	alert('If B&B is not responsile, you must enter a REC#-LI# of the form "RE####-#"');
   	    	return false;
		}
	}

	//For TDRs: check nonconf found
	var NCf = document.getElementById('NC_FOUND');
	if(null != NCf && !NCf.value){
    	alert('Non Conformance Found: you must select an option');
    	return false;
	}
	
	//check nonconf
	var NC = document.getElementById('NONCONF');
	var NCa = document.getElementById('NONCONFappend');
	if((!NCa && NC && NC.value != '') || (NCa && NCa.value != '')){
		var NCN = document.getElementById('NONCONF_CONTACT');
		if(NCN && NCN.value == ''){
   	    	alert('Non Conformance: you must enter a contact');
   	    	return false;
		}
	}
	
	//check RMA
	var RMA = document.getElementById('RMA1');
	if(RMA && RMA.checked && RMA.value == 'Y'){
		var RMAnum  = document.getElementById('RMA_NUM');
		var RMAname = document.getElementById('RMA_CONTACT');
		if(!RMAnum || RMAnum.value == ''){
   	    	alert('RMA: you must enter a number');
   	    	return false;
		}
		if(!RMAname || RMAname.value == ''){
   	    	alert('RMA: you must enter a contact');
   	    	return false;
		}
	}
	
	//check rootcause
	var RC = document.getElementById('ROOTCAUSE');
	var RCa = document.getElementById('ROOTCAUSEappend');
	if((!RCa && RC && RC.value != '') || (RCa && RCa.value != '')){
		var RCN = document.getElementById('ROOTCAUSE_CONTACT');
		if(RCN && RCN.value == ''){
   	    	alert('Root Cause: you must enter a contact');
   	    	return false;
		}
	}
	
	//check corrective action
	var CA = document.getElementById('CACTION');
	var CAa = document.getElementById('CACTIONappend');
	if((!CAa && CA && CA.value != '') || (CAa && CAa.value != '')){
		var CAN = document.getElementById('CACTION_CONTACT');
		if(CAN && CAN.value == ''){
   	    	alert('Corrective Action: you must enter a contact');
   	    	return false;
		}
	}
	
	//check attachment
    var attdesc = document.getElementById('ATTACHMENT_DESC');
    var att = document.getElementById('attachment');
    if(attdesc && att && (attdesc.value != '' || att.value != '')){
	    if(attdesc.value == ''){
   	    	alert('Attachment: you must enter a description');
   	    	return false;
   		}
    	if(att.value == ''){
        	alert('Attachment: you must choose a file to upload');
        	return false;
    	}
    }
    
    //check qa reject
    var QA = document.getElementById('QA_REJECT1');
	if(QA && QA.checked && QA.value == 'Y'){
		var QAC = document.getElementById('QA_REJECT_CODE');
		if(!QAC || QAC.value == ''){
        	alert('QA Reject: you must choose a rejection code');
        	return false;
		}
	}			
    
    //check qa accept for workcenter code
    var QAA = document.getElementById('QA_APPROVAL1');
	if(QAA && QAA.checked && QAA.value == 'Y'){
		var QAAC = document.getElementById('RESP_WC');
		if(!QAAC || QAAC.selectedIndex <= 0){
        	alert('QA Accept: you must choose a work center');
        	return false;
		}
	}			
    
    //check extension
    var EXdesc = document.getElementById('EXTENSION_DESC');
    if(EXdesc && EXdesc.value != ''){
    	var EXname = document.getElementById('EXTENSION_CONTACT');
    	if(!EXname || EXname.value == ''){
        	alert('Extension: you must enter a contact');
        	return false;
    	}
    }
    
    //check effect date
	var EffD = document.getElementById('EFFECT_DATE_DAY');
	var EffM = document.getElementById('EFFECT_DATE_MONTH');
	var EffY = document.getElementById('EFFECT_DATE_YEAR');
	if(EffD && EffM && EffY){
		if(EffD.value != '' || EffM.value != '' || EffY.value != ''){
			if(EffD.value == ''){
        		alert('Effect Date: Day not selected');
        		return false;
	    	}
			if(EffM.value == ''){
        		alert('Effect Date: Month not selected');
        		return false;
	    	}
			if(EffY.value == ''){
    	    	alert('Effect Date: Year not selected');
        		return false;
	    	}
		}
	}
    
    //all's good
    return true;
}
//Function for automatically changing CA based on RC
function rcToCa(val){
	var changeTo = false;
	var caObj = document.f.CACODE;
	switch(val){
		case 21: case '21':
			//change CA to 11
			if(changeTo === false){
				changeTo = 11;
			}
		case 39: case '39':
			//change CA to 20
			if(changeTo === false){
				changeTo = 20;
			}
		//case : case '':
			//change CA to ...
			
			//implement change
			for(var i=0;i<caObj.length;i++){
				if(caObj.options[i].value == changeTo){
					caObj.selectedIndex = i;
					break;
				}
			}
			
			break;
		default:
			//nothing
	}
}