// JavaScript Document
function checkUserFields() {
	
	var objectRef = document.userform;
	var submitForm = true;
	var missingFieldList = "The following field(s) are missing or are incorrect." + "\n";
	missingFieldList = missingFieldList + "Please fill them out or correct them and resubmit the form." + "\n" + "\n";
	
	if (objectRef.user.value == "") {
	 	missingFieldList = missingFieldList + "- User Name" + "\n";
		submitForm = false;
	}
	if (objectRef.first_name.value == "") {
	 	missingFieldList = missingFieldList + "- First Name" + "\n";
		submitForm = false;
	}
	if (objectRef.last_name.value == "") {
	 	missingFieldList = missingFieldList + "- Last Name" + "\n";
		submitForm = false;
	}
	if (objectRef.email.value == "") {
	 	missingFieldList = missingFieldList + "- Email Address" + "\n";
		submitForm = false;
	} else {
		if (objectRef.email.value.indexOf("@") == -1) {
			missingFieldList = missingFieldList + "- Email Address does not appear to be a valid email address" + "\n";
			submitForm = false;
		} else {
			if (objectRef.email.value.indexOf(".") == -1) {
				missingFieldList = missingFieldList + "- Email Address does not appear to be a valid email address" + "\n";
				submitForm = false;
			}
		}
	}
	if (objectRef.role_id.value == "") {
	 	missingFieldList = missingFieldList + "- Role" + "\n";
		submitForm = false;
	}
	if (objectRef.status.value == "") {
	 	missingFieldList = missingFieldList + "- Status" + "\n";
		submitForm = false;
	}
	if (objectRef.user_set.value == 0) {
		if (objectRef.password.value == "" || objectRef.password_confirm.value == "" || objectRef.password.value != objectRef.password_confirm.value) {
			missingFieldList = missingFieldList + "- Passwords are blank or Password and Confirm Password fields do not match" + "\n";
			submitForm = false;
		}
	}
	if (submitForm) {
		return true; 
	} else {
		alert(missingFieldList);
		return false;
	}
}

function checkCategoriesFields() {
	var objectRef = document.categoriesform;
	var submitForm = true;
	var missingFieldList = "The following field(s) are missing or are incorrect." + "\n";
	missingFieldList = missingFieldList + "Please fill them out or correct them and resubmit the form." + "\n" + "\n";
	if (objectRef.description.value == "") {
	 	missingFieldList = missingFieldList + "- Description" + "\n";
		submitForm = false;
	}
	if (submitForm) {
		return true; 
	} else {
		alert(missingFieldList);
		return false;
	}
}
function checkRetentionFields() {
	var objectRef = document.retentionform;
	var submitForm = true;
	var missingFieldList = "The following field(s) are missing or are incorrect." + "\n";
	missingFieldList = missingFieldList + "Please fill them out or correct them and resubmit the form." + "\n" + "\n";
	if (objectRef.description.value == "") {
	 	missingFieldList = missingFieldList + "- Description" + "\n";
		submitForm = false;
	}
	if (submitForm) {
		return true; 
	} else {
		alert(missingFieldList);
		return false;
	}
}

function MM_openBrWindow2(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function clearCustomerValues(complaint) {
	var objectRef = document.complaint;
		objectRef.cust_no_display.value = "";
		if(complaint == '') {
			objectRef.customer.value = "";
		}
		objectRef.name.value = "";
		objectRef.address.value = "";
		objectRef.city.value = "";
		objectRef.state.value = "";
		objectRef.zip.value = "";
		objectRef.type.value = "";
		objectRef.id.value = "";
		objectRef.division.value = "";
		objectRef.telephone.value = "";
		objectRef.history.disabled=true;
}

function retentionCodeDisable(retention) {
		if(retention == "N") {
			document.complaint.retention_reason_code.disabled=false;
		} else {
			document.complaint.retention_reason_code.disabled=true;
			document.complaint.retention_reason_code.value="";
		}
}

function openCategoryHelp(category) {
	URL = "category.php?category="+category;
	//alert(URL);
	window.open(URL,"catgory","width=300 height=400");
}

function openViewHistory(customer) {
	URL = "index.php?action=complaint&proc=display&view=customer&customer="+customer;
	//alert("test");
	window.open(URL,"history","width=1000 height=800");
}

function setAssignTo(assigntoDetails, category, plant) {
		var objectRef = document.complaint;
		var x;
		var assignTo;
		var assignUser;
		for (x in assigntoDetails) {
			if(assigntoDetails[x][1] == category && assigntoDetails[x][2] == plant) {
				assignTo = assigntoDetails[x][3] + " - " + assigntoDetails[x][4] + ", " + assigntoDetails[x][5];
				assignUser = assigntoDetails[x][3];
			}
		}
		if(assignTo == undefined) {
			objectRef.assign_ed.value = '';
			objectRef.assign_to.value = '';
		} else {
			objectRef.assign_ed.value = assignTo;
			objectRef.assign_to.value = assignUser;
		}
		
}

function updateAddress(customerDetails, cusObject) {
	if (cusObject == "") {
		var objectRef = document.complaint;
		objectRef.cust_no_display.value = "";
		objectRef.name.value = "";
		objectRef.address.value = "";
		objectRef.city.value = "";
		objectRef.state.value = "";
		objectRef.zip.value = "";
		objectRef.type.value = "";
		objectRef.id.value = "";
		objectRef.customer_no.value = "";
		return 0;
	} else {
    	var objectRef = document.complaint;
		objectRef.cust_no_display.value = customerDetails[cusObject][0];
		objectRef.name.value = customerDetails[cusObject][1];
		objectRef.address.value = customerDetails[cusObject][2];
		objectRef.city.value = customerDetails[cusObject][3];
		objectRef.state.value = customerDetails[cusObject][4];
		objectRef.zip.value = customerDetails[cusObject][5];
		objectRef.type.value = customerDetails[cusObject][6];
		objectRef.id.value = customerDetails[cusObject][7];
		objectRef.customer_no.value = customerDetails[cusObject][7];
		//alert("customer_no = " + objectRef.customer_no.value);
		return 0;
	}
}

function checkComplaintStatus(status, complaintStatusOrigin) {
	
	for (i=0; i<status.length; i++) {
		if(status[i].checked) {
			statusValue = status[i].value;
		}
	}
	if(complaintStatusOrigin != 2) {
		if(statusValue == 2) {
			var answer = confirm ("Status has been set to Closed. Are you sure you want to save?");
			if(answer) {
				return true;
			} else {
				return false;
			}
		}
	} else {
		return true;
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function showHideNotifDate(status, resolved_status) {
	var statusValue = "";
	var resolved_statusValue = "";
	
	for (i=0; i<status.length; i++) {
		if(status[i].checked) {
			statusValue = status[i].value;
		}
	}
	
	for (i=0; i<resolved_status.length; i++) {
		if(resolved_status[i].checked) {
			resolved_statusValue = resolved_status[i].value;
		}
	}
	
	
	if(statusValue == 1 &&  resolved_statusValue == 1) {
		//alert("show");
		MM_showHideLayers('layNotifcationDate','','show');
		MM_showHideLayers('layNotifcationDateText','','show');
		//objectRef.notif_date.disabled=true;
	} else {
		MM_showHideLayers('layNotifcationDate','','hide');
		MM_showHideLayers('layNotifcationDateText','','hide');
		//objectRef.notif_date.disabled=false;
		
	}
}

function setAnonymous(anonymous) {
		var objectRef = document.complaint;
		var x;		
		for (i=0; i<anonymous.length; i++) {
			if(anonymous[i].checked) {
				anonymousValue = anonymous[i].value;
			}
		}
		if(anonymousValue == 'YES') {
			objectRef.name.disabled=true;
			objectRef.address.disabled=true;
			objectRef.city.disabled=true;
			objectRef.state.disabled=true;
			objectRef.zip.disabled=true;
			objectRef.telephone.disabled=true;	
		} else {
			objectRef.name.disabled=false;
			objectRef.address.disabled=false;
			objectRef.city.disabled=false;
			objectRef.state.disabled=false;
			objectRef.zip.disabled=false;
			objectRef.telephone.disabled=false;			
		}
}


