/**************** Global Variables ****************/



/**************** Specific Functions ****************/
function checkCustomer(userID){
	var userIDArray = getFieldValue('userIDList').split(',');
	var errorList = '';
	var tempUsername = '';
	var adminUsername = Trim(getFieldValue('adminUsername').toLowerCase());
	var empUsername = Trim(getFieldValue('empUsername').toLowerCase());
	var currentUsername = Trim(getFieldValue('Username' + userID).toLowerCase());
	var action = '';
	
	if(getFieldValue('Go') != 'Delete'){
		errorList = checkField('Name' + userID,'NOTNULL','Customer Name',errorList);
		errorList = checkField('Username' + userID,'NOTNULL','Username',errorList);
		errorList = checkField('Password' + userID,'NOTNULL','Password',errorList);
		
		for(var x=0; x < userIDArray.length; x++){
			tempUsername = Trim(getFieldValue('Username' + userIDArray[x]).toLowerCase());
			if(userIDArray[x] != userID && tempUsername == currentUsername){
				errorList += 'All Usernames must be unique.\n';
			}
		}
		
		if(adminUsername == currentUsername){
			errorList += 'Username cannot be the same as the admin account username\n';
		}
		
		if(empUsername == currentUsername){
			errorList += 'Username cannot be the same as the employee account username\n';
		}
		
		if(errorList.length > 0){
			alert(errorList);
		}
		else{
			action = (userID == '') ? 'create' : 'update';
			
			if(confirm('Are you sure you want to ' + action + ' this customer?')){
				submitForm('Customers');
			}
		}
	}
	else{
		var customerIDs = getFieldValue('userIDList').split(',');
		var msg = 'This action will delete the following customers and their associated certificates including files:\n';
		
		for(var i=0; i < customerIDs.length; i++){
			if(checkedStatus('deleteCustomer' + customerIDs[i])){
				msg += ' - ' + getFieldValue('Name' + customerIDs[i]) + '\n';
			}
		}
		
		msg += '\nDo you wish to proceeed?';
		
		if(confirm(msg)){
			submitForm('Customers');
			return true;
		}
		else{
			setFieldValue('Go', '');
			return false;
		}
	}
}

//Returns true if a customer has been checked, false if none have been
function customersChecked(){
	var customerIDs = getFieldValue('userIDList').split(',');
	for(var i=0; i < customerIDs.length; i++){
		if(checkedStatus('deleteCustomer' + customerIDs[i])){
			return true;
		}
	}
	return false;
}

function releaseCustomerDeleteButton(){
	if(customersChecked()){
		enableField('DeleteCustomers');
	}
	else{
		disableField('DeleteCustomers');
	}
}


function checkPasswords(){
	if(getFieldValue('AdminPassword') != getFieldValue('ConfirmAdminPassword')){
		alert('Admin Password and Confirm Password Fields MUST match.');
		return false;
	}
	
	if(getFieldValue('EmpPassword') != getFieldValue('EmpAdminPassword')){
		alert('Employee Password and Confirm Password Fields MUST match.');
		return false;
	}
	
	return confirm('Are you sure you want to update the Administrator and Employee Logins?');
}

function checkCertificate(){
	var errorList = '';
	var action = '';
	//Check to see if this certificate has been marked for deletion
	var deleteCertificate = 'deleteCertificate' + getFieldValue('certID');
	var msg = '';
	var fileName = Trim(getFieldValue('FileName'));
	
	try{
		var checkDelete = (getFieldValue(deleteCertificate) == 'true');
	}
	catch(e){
		var checkDelete = false;
	}
	
	if(!checkDelete){
		errorList = checkField('UserID','NOTFIRST','Customer', errorList);
		errorList = checkField('CertTypeID','NOTFIRST','Certificate Type', errorList);
		//errorList = checkField('PONumber','NOTNULL','PO Number', errorList);
		errorList = checkField('CertNumber','NOTNULL','Certificate Number', errorList);
		errorList = checkField('CertDate','NOTNULL','Certificate Date', errorList);
		errorList = checkField('Description','NOTNULL','Certificate Description', errorList);
		
		if(errorList.length > 0){
			alert(errorList);
			return false;
		}
		action = (parseInt(getFieldValue('certID')) > 0) ? 'update' : 'create';
	
		return confirm('Are you sure you want to ' + action + ' this certificate?');
	}
	else{
		msg = 'Are you sure you want to DELETE this certificate?'
		if(fileName.length > 0){
			msg += '\nNOTE: The attached file \"' + fileName + '\" will be deleted as well.'
		}
		
		if(confirm(msg)){
			return true;
		}
		else{
			setFieldValue(deleteCertificate, 'false');
			return false;
		}
	}
}

//Returns true if a certificate has been checked, false if none have been
function certificatesChecked(){
	var certIDs = getFieldValue('certificateIDList').split(',');
	for(var i=0; i < certIDs.length; i++){
		if(checkedStatus('deleteCertificate' + certIDs[i])){
			return true;
		}
	}
	return false;
}

function releaseCertificateDeleteButton(){
	if(certificatesChecked()){
		enableField('DeleteCertificates');
	}
	else{
		disableField('DeleteCertificates');
	}
}

function deleteCertificateCheck(){
	var certIDs = getFieldValue('certificateIDList').split(',');
	var msg = 'This action will delete the following certificates and their associated files:\n';
	
	for(var i=0; i < certIDs.length; i++){
		if(checkedStatus('deleteCertificate' + certIDs[i])){
			msg += ' - ' + getFieldValue('CertNumber' + certIDs[i]) + '\n';
		}
	}
	
	msg += '\nDo you wish to proceeed?';
	
	return confirm(msg);
}

/**************** Pop-up Windows ****************/

function ShowProgress(PID){
	var xPos, yPos;
	var winWidth, winHeight;

	var strAppVersion = navigator.appVersion;
	var winstyle = '';
	
	var errorList = '';
	var action = (getFieldValue('FileName').length > 0) ? 'Replace' : 'Add';
	
	errorList = checkField('UploadedFileName', 'NOTNULL', action + ' Certificate File', errorList);
	
	if(errorList.length > 0){
		alert(errorList);
		return false;
	}
	
	if(confirm('Are you sure you want to attach this file to this certificate?')){
		if(strAppVersion.indexOf('MSIE') != -1 && strAppVersion.substr(strAppVersion.indexOf('MSIE')+5,1) > 4){
			winWidth = 385;
			winHeight = 140;
			winstyle = 'dialogWidth:' + winWidth + 'px; dialogHeight:' + winHeight + 'px; center:yes';
			window.showModalDialog('framebar.asp?to=10&PID=' + PID + '&b=IE', null, winstyle);
		}
		else{
			winWidth = 375;
			winHeight = 115;
			xPos = (screen.width / 2) - (winWidth / 2);
			yPos = (screen.height / 2) - (winHeight / 2);
			window.open('framebar.asp?to=10&PID=' + PID + '&b=NN', '', 'top=' + yPos + ',left=' + xPos + ',width=' + winWidth + ',height=' + winHeight + ',modal=yes', true);
		}
		return true;
	}
	return false;
}



