/**
 * Behavior rules
 */
var xsdWeaverRules = {
	'a#hideFormInfo' : function(element){
		element.onclick = function(){
			new Effect.BlindUp($('formInfo')); 
			hidethis('hideFormInfo'); 
			showthis('showFormInfo'); 
			return false;
		}
	},
	'a#showFormInfo' : function(element){
		element.onclick = function(){
			new Effect.BlindDown($('formInfo')); 
			hidethis('showFormInfo'); 
			showthis('hideFormInfo'); 
			return false;
		}
	},
	'a#fileMenuLink' : function(element){
		element.onclick = function(){
			return false;
		}
	},
	'a#addMenuLink' : function(element){
		element.onclick = function(){
			return false;
		}
	},
	'a#viewMenuLink' : function(element){
		element.onclick = function(){
			return false;
		}
	},
	'a#viewPreviewLink' : function(element){
		element.onclick = function(){
			hidethis('treeView'); 
			hidethis('htmlView'); 
			hidethis('xsdView'); 
			showthis('previewView'); 
			return false;
		}
	},
	'a#viewTreeLink' : function(element){
		element.onclick = function(){
			hidethis('previewView'); 
			hidethis('htmlView'); 
			hidethis('xsdView'); 
			showthis('treeView'); 
			return false;
		}
	},
	'a#viewHtmlLink' : function(element){
		element.onclick = function(){
			hidethis('treeView'); 
			hidethis('previewView'); 
			hidethis('xsdView'); 
			showthis('htmlView'); 
			return false;
		}
	},
	'a#viewXsdLink' : function(element){
		element.onclick = function(){
			hidethis('treeView'); 
			hidethis('htmlView'); 
			hidethis('previewView'); 
			showthis('xsdView'); 
			return false;
		}
	},
	'a#editApplyLink' : function(element){
		element.onclick = function(){
			// flag to see if added successfully
			var success = false;
			switch(fieldType){
				case 'text':
					// checck if fields checked out
					if(checkTextField()){
						if(editType == 'add'){
							// add new field
							addTextField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editTextField();
						}
						// update views
						updateView();
						// now reset the form
						resetTextField();
						success = true;
					}
					break;
				case 'textarea':
					// checck if fields checked out
					if(checkTextareaField()){
						if(editType == 'add'){
							// add new field
							addTextareaField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editTextareaField();
						}
						// update views
						updateView();
						// now reset the form
						resetTextareaField();
						success = true;
					}
					break;
				case 'wysiwyg':
					// checck if fields checked out
					if(checkWysiwygField()){
						if(editType == 'add'){
							// add new field
							addWysiwygField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editWysiwygField();
						}
						// update views
						updateView();
						// now reset the form
						resetWysiwygField();
						success = true;
					}
					break;
				case 'date':
					// checck if fields checked out
					if(checkDateField()){
						if(editType == 'add'){
							// add new field
							addDateField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editDateField();
						}
						// update views
						updateView();
						// now reset the form
						resetDateField();
						success = true;
					}
					break;
				case 'select':
					// checck if fields checked out
					if(checkSelectField()){
						if(editType == 'add'){
							// add new field
							addSelectField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editSelectField();
						}
						// update views
						updateView();
						// now reset the form
						resetSelectField();
						success = true;
					}
					break;
				case 'group':
					// checck if fields checked out
					if(checkGroupField()){
						if(editType == 'add'){
							// add new field
							addGroupField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editGroupField();
						}
						// update views
						updateView();
						// now reset the form
						resetGroupField();
						success = true;
					}
					break;
				case 'file':
					// checck if fields checked out
					if(checkFileField()){
						if(editType == 'add'){
							// add new field
							addFileField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editFileField();
						}
						// update views
						updateView();
						// now reset the form
						resetFileField();
						success = true;
					}
					break;
				case 'boolean':
					// checck if fields checked out
					if(checkBooleanField()){
						if(editType == 'add'){
							// add new field
							addBooleanField();
						}
						else if(editType == 'edit'){
							// update currently being edited field
							editBooleanField();
						}
						// update views
						updateView();
						// now reset the form
						resetBooleanField();
						success = true;
					}
					break;
				default:
					// open XSD functions
					// check if importing or uploading
					if(document.openXSDForm.openMethod[0].checked){
						// import
						// check if textarea is filled out
						if( isEmpty($F('XSDImportContent')) ){
							alert('Please copy and paste XSD content in textarea');
							$('XSDImportContent').focus();
						}
						else {
							// alert('Importing ' + $('XSDImportContent').value);
							// import
							if( xsdWeaverForm.importXsd($F('XSDImportContent'), {appendElement: $('openOption').checked}) ){
								
								if(!$('openOption').checked){
									document.xsdWeaverForm.fw_formName.value = xsdWeaverForm.name;
									document.xsdWeaverForm.fw_formTitle.value = xsdWeaverForm.label;
									document.xsdWeaverForm.fw_formDesc.value = xsdWeaverForm.description;
								}
			
								// update group array and box
								// updateGroupArray();
								updateGroupList();
								
								// update views
								updateView();
								success = true;
								// alert('Success?');
							}
							else {
								alert('Error in parsing XSD!');
							}
						}
					}
					else if(document.openXSDForm.openMethod[1].checked){
						uploadFileName = $('XSDUploadFile').value;

						// upload
						if( isEmpty(uploadFileName) ){
							alert('Please choose a XSD file to upload');
							$('XSDUploadFile').focus();
						}
						else if( uploadFileName.substring(uploadFileName.length-3,uploadFileName.length).toLowerCase() != 'xsd' ){ // check if file has .xsd extension
							alert('Please choose a valid XSD file with .xsd extension');
						}
						else{
							// submit file
							document.uploadXSDForm.submit();
						}
					}
					else{
						alert('Please select open method');
						$('openByImport').focus();
					}
					break;
			}
			
			// check if added successfully
			if(success){
				// reset
				editType = null;
				fieldType = null;
				currentField = null;
				target = null;
				dateUpdateTargetForm = null;
				dateUpdateTarget = null;
				hidethis('datePickerBox');
				hidethis('backDrop'); 
				hidethis('edit'); 
				showthis('previewView');
			}
			
			return false;
		}
	},
	'a#editCloseLink' : function(element){
		element.onclick = function(){
			editType = null;
			fieldType = null;
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			hidethis('backDrop'); 
			hidethis('edit'); 
			showthis('previewView');
			return false;
		}
	},
	'a#addTextFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'text';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			showthis('textFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add text field";
			showthis('edit');
			return false;
		}
	},
	'a#addTextareaFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'textarea';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			showthis('textareaFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add textarea field";
			showthis('edit');
			return false;
		}
	},
	'a#addWysiwygFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'wysiwyg';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			showthis('wysiwygFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add wysiwyg field";
			showthis('edit');
			return false;
		}
	},
	'a#addDateFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'date';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			showthis('dateFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add date field";
			showthis('edit');
			return false;
		}
	},
	'a#addSelectionFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'select';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			showthis('selectionFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add selection field";
			showthis('edit');
			return false;
		}
	},
	'a#addGroupFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'group';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('fileFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			showthis('groupFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add group field";
			showthis('edit');
			return false;
		}
	},
	'a#addFileFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'file';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('booleanFieldEdit');
			showthis('groupListBox');
			showthis('fileFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add file field";
			showthis('edit');
			return false;
		}
	},
	'a#addBooleanFieldLink' : function(element){
		element.onclick = function(){
			editType = 'add';
			fieldType = 'boolean';
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('fileFieldEdit');
			showthis('groupListBox');
			showthis('booleanFieldEdit');
			hidethis('openXSDWizard');
			$('editTitle').innerHTML = "Add boolean field";
			showthis('edit');
			return false;
		}
	},
	'input#fw_textEmail' : function(element){
		element.onclick = function(){
			document.textEditForm.fw_textNumberMin.value = '';
			document.textEditForm.fw_textNumberMax.value = '';
			hidethis('textEditNumberOptions');
		}
	},
	'input#fw_textPhone' : function(element){
		element.onclick = function(){
			document.textEditForm.fw_textNumberMin.value = '';
			document.textEditForm.fw_textNumberMax.value = '';
			hidethis('textEditNumberOptions');
		}
	},
	'input#fw_textNumber' : function(element){
		element.onclick = function(){
			showthis('textEditNumberOptions');
		}
	},
	'input#fw_textNoValidation' : function(element){
		element.onclick = function(){
			document.textEditForm.fw_textNumberMin.value = '';
			document.textEditForm.fw_textNumberMax.value = '';
			hidethis('textEditNumberOptions');
		}
	},
	'a#minDatePicker' : function(element){
		element.onclick = function(){
			$('datePickerBox').style.top = posY + "px";
			$('datePickerBox').style.left = posX + "px";
			dateUpdateTarget = 'fw_dateMin';
			dateUpdateTargetForm = 'dateEditForm';
			showthis('datePickerBox');
			return false;
		}
	},
	'a#maxDatePicker' : function(element){
		element.onclick = function(){
			$('datePickerBox').style.top = posY + "px";
			$('datePickerBox').style.left = posX + "px";
			dateUpdateTarget = 'fw_dateMax';
			dateUpdateTargetForm = 'dateEditForm';
			showthis('datePickerBox');
			return false;
		}
	},
	'a#datePickerCloseLink' : function(element){
		element.onclick = function(){
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			return false;
		}
	},
	'a#addOptionLink' : function(element){
		element.onclick = function(){
			addSelectOption(); 
			return false;
		}
	},
	'a#deleteOptionLink' : function(element){
		element.onclick = function(){
			deleteSelectOption(); 
			return false;
		}
	},
	'a#moveOptionUpLink' : function(element){
		element.onclick = function(){
			moveSelectOptionUp(); 
			return false;
		}
	},
	'a#moveOptionDownLink' : function(element){
		element.onclick = function(){
			moveSelectOptionDown(); 
			return false;
		}
	},
	'a#resetOptionLink' : function(element){
		element.onclick = function(){
			resetSelectOptions(); 
			return false;
		}
	},
	'input#fw_formName' : function(element){
		element.onchange = function(){
			xsdWeaverForm.name = $F('fw_formName');
		},
		element.onfocus = function(){
			xsdWeaverForm.label = $F('fw_formName');
		},
		element.onblur = function(){
			xsdWeaverForm.name = $F('fw_formName');
		}
	},
	'input#fw_formTitle' : function(element){
		element.onchange = function(){
			xsdWeaverForm.label = $F('fw_formTitle');
		},
		element.onfocus = function(){
			xsdWeaverForm.label = $F('fw_formTitle');
		},
		element.onblur = function(){
			xsdWeaverForm.label = $F('fw_formTitle');
		}
	},
	'textarea#fw_formDesc' : function(element){
		element.onchange = function(){
			xsdWeaverForm.description = $F('fw_formDesc');
		},
		element.onfocus = function(){
			xsdWeaverForm.label = $F('fw_formDesc');
		},
		element.onblur = function(){
			xsdWeaverForm.description = $F('fw_formDesc');
		}
	},
	'a#formInfoUpdateLink' : function(element){
		element.onclick = function(){
			// update form info
			updateFormInfo()
			// update display
			updateView();
			return false;
		}
	},
	'a#newXsdLink' : function(element){
		element.onclick = function(){
			if( confirm('This will clear existing form data. Proceed?') ){
				// remove all form elements
				xsdWeaverForm.reset();
				// reset form info
				resetFormInfo();
				// update display
				updateView();
			}
			return false;
		}
	},
	'a#openXsdLink' : function(element){
		element.onclick = function(){
			// open xsd form
			editType = null;
			fieldType = null;
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			resizebgOverlay('backDrop');
			showthis('backDrop');
			resizeeditArea('edit');
			hidethis('previewView');	/* cuz IE shows select form element */
			hidethis('textFieldEdit');
			hidethis('textareaFieldEdit');
			hidethis('wysiwygFieldEdit');
			hidethis('dateFieldEdit');
			hidethis('selectionFieldEdit');
			hidethis('groupFieldEdit');
			hidethis('booleanFieldEdit');
			hidethis('groupListBox');
			hidethis('fileFieldEdit');
			hidethis('uploadWizardBox');
			hidethis('importWizardBox');
			if(document.openXSDForm.openMethod[0].checked){
				hidethis('uploadWizardBox');
				showthis('importWizardBox');
			}
			if(document.openXSDForm.openMethod[1].checked){
				showthis('uploadWizardBox');
				hidethis('importWizardBox');
			}
			showthis('openXSDWizard');
			$('editTitle').innerHTML = "Open XSD";
			showthis('edit');
			return false;
		}
	},
	'a#saveXsdLink' : function(element){
		element.onclick = function(){
			// update form info
			updateFormInfo()
			
			// first check to see if form information is filled out
			if( isEmpty(xsdWeaverForm.name) ){
				alert("Please specify form name");
				document.xsdWeaverForm.fw_formName.focus();
			}
			else if( isEmpty(xsdWeaverForm.label) ){	
				alert("Please specify form title");
				document.xsdWeaverForm.fw_formTitle.focus();
			}
			else if( isEmpty(xsdWeaverForm.description) ){
				alert("Please specify form description");
				document.xsdWeaverForm.fw_formDesc.focus();
			}
			else if( xsdWeaverForm.formElements.length == 0 ){
				// second check to see if there are any elements
				alert("There is no form elements!");
			}
			else{
				// now save to file
				// save the xsd into a var
				var xsdString = xsdWeaverForm.toXsd();
				
				// parameters to pass
				var params = $H({ xsdContent: xsdString });
				
				// call ajax
				new Ajax.Request('downloadXsd.php', {onSuccess: saveXsdHandler, method: 'post', parameters: params.toQueryString()});
				// location.href = 'downloadXsd.php?' + params.toQueryString();
			}
			return false;
		}
	},
	'input#openByImport' : function(element){
		element.onclick = function(){
			hidethis('uploadWizardBox');
			showthis('importWizardBox');
			new Effect.Highlight('importWizardBox');
		}
	},
	'input#openByUpload' : function(element){
		element.onclick = function(){
			showthis('uploadWizardBox');
			hidethis('importWizardBox');
			new Effect.Highlight('uploadWizardBox');
		}
	}
	
}

// function to handle saving?
function saveXsdHandler(t){
	
	// parameters to pass
	var params = $H({ getXsdFile: 'true', fileName: xsdWeaverForm.name  });
				
	location.href = 'downloadXsd.php?' + params.toQueryString();
			
}

// function to handle uploaded file content
function uploadXsdHandler(fileContent){
	if( isEmpty(fileContent) ){
		alert('Empty file content!');
	}
	else {
		// alert('Importing ' + $('XSDImportContent').value);
		// import
		if( xsdWeaverForm.importXsd(fileContent, {appendElement: $('openOption').checked}) ){
			// check if form name and stuff has to be overwritten
			
			if(!$('openOption').checked){
				document.xsdWeaverForm.fw_formName.value = xsdWeaverForm.name;
				document.xsdWeaverForm.fw_formTitle.value = xsdWeaverForm.label;
				document.xsdWeaverForm.fw_formDesc.value = xsdWeaverForm.description;
			}
			
			// update group array and box
			// updateGroupArray();
			updateGroupList();
			
			// update views
			updateView();
			
			// alert('Success?');
			// reset fields
			editType = null;
			fieldType = null;
			currentField = null;
			target = null;
			dateUpdateTargetForm = null;
			dateUpdateTarget = null;
			hidethis('datePickerBox');
			hidethis('backDrop'); 
			hidethis('edit'); 
			showthis('previewView');
		}
		else {
			alert('Error in parsing XSD!');
		}
	}
}

// register behavior rules
Behaviour.register(xsdWeaverRules);
