//controls all the elements in the form
function form(obj_form, is_dynamic_input){
	var myForm = this;
	this.prefix = obj_form;

	//helper variable to handle the form
	if (document.getElementById(this.prefix + 'data')) {
		this.data = document.getElementById(this.prefix + 'data');
	} else {
		return false;
	}

	this.data.onsubmit = function(){
		return myForm.check_fields();
	}

	var default_subject_id = this.prefix + 'default_subject';
	var default_text_id = this.prefix + 'default_text';
	var default_name_id = this.prefix + 'default_name';

	if (document.getElementById(default_subject_id)){
		this.default_subject = document.getElementById(default_subject_id).value;
	}
	if (document.getElementById(default_text_id)){
		this.default_text = document.getElementById(default_text_id).value;
	}
	if (document.getElementById(default_name_id)){
		this.default_name = document.getElementById(default_name_id).value;
	}

	//initialize js for input fields
	if (is_dynamic_input){
		var add_subject = this.prefix + 'add_subject';
		var add_name = this.prefix + 'add_name';

		if (document.getElementById(add_subject)){
			this.nmsubj = new pfInput(document.getElementById(add_subject));
		}
		if (document.getElementById(add_name)){
			this.nmname = new pfInput(document.getElementById(add_name));
		}
	}

	//create an instance of the class form_textarea
	myText = new form_textarea(document.getElementById(obj_form + 'add_text'), obj_form);

	//create 4 instances of the class form_att_control (one for each icon)
	this.myControl1 = new form_att_control(obj_form, obj_form + 'icon_img', obj_form + 'att_img_field');
	this.myControl2 = new form_att_control(obj_form, obj_form + 'icon_video', obj_form + 'att_video_field');
	this.myControl3 = new form_att_control(obj_form, obj_form + 'icon_file', obj_form + 'att_file_field');
	this.myControl4 = new form_att_control(obj_form, obj_form + 'icon_link', obj_form + 'att_link_field');

	//helper variables to handle the control code
	this.code_container = document.getElementById(this.prefix + 'code_container');
	this.captcha_container = document.getElementById('captcha_container');
	this.post_button = document.getElementById(this.prefix + 'post_button');
	this.verify_button = document.getElementById(this.prefix + 'verify_button');
	this.forum_post_link = document.getElementById('forum_post_link');

	//hide form (only one at a time is displayed)
	form.prototype.hide = function(is_show_link){

		//show post link
		if (is_show_link){
			this.forum_post_link.style.display = 'inline';
		}

		//hide form
		this.data.style.display = 'none';
	}

	//display form
	form.prototype.display = function(parent_id, is_hide_link, is_focus){
		this.parent_id = this.prefix + 'm' + parent_id;
		this.current_form_container = document.getElementById(this.parent_id).parentNode;

		//hide post link
		if (is_hide_link){
			this.forum_post_link.style.display = 'none';
		}
		//set form parent id
		document.getElementById(this.prefix + 'parent_id').setAttribute('value', parent_id);

		//append the form to the parent
		document.getElementById(this.parent_id).appendChild(this.data);

		//display form
		document.getElementById(this.prefix + 'data').style.display = 'block';
		document.getElementById(this.prefix + 'empty_fields_message').style.display = 'none';

		//focus textarea
		if (is_focus){
			document.getElementById(this.prefix + 'add_text').focus();

			// highlight the message to which user is replying to, but first make sure no other messages
			// are highlighted and indented correctly
			var h = document.getElementsByTagName('DIV');

			for(i = 0; i < h.length; i++){
				if (h[i].className == 'highlight') {
					h[i].className = 'message_body';
					h[i].style.marginLeft = h[i].initMarginLeft;
					h[i].initMarginLeft = 0;
				}
			}

			this.current_form_container.className = 'highlight';

			//set the left margin of the form
			var IE = /*@cc_on!@*/false;
			var IE_comp_view = document.documentMode && document.documentMode < 8;
			var offsetLeft = IE && IE_comp_view
				? get_offset(document.getElementById('forum')) - get_offset(this.current_form_container)
				: document.getElementById('forum').offsetLeft - this.current_form_container.offsetLeft;
			this.current_form_container.initMarginLeft = this.current_form_container.style.marginLeft;
			this.current_form_container.style.marginLeft = offsetLeft + 'px';

		}
	}

	//display control code
	form.prototype.display_control_code = function(){
		//add the control code and buttons to the container
		this.code_container.appendChild(this.captcha_container);
		this.code_container.appendChild(document.getElementById(this.prefix + 'code'));

		//display control code
		this.captcha_container.style.display = 'block';
		document.getElementById('recaptcha_response_field').focus();

		//hide post button and display verify button (will check the control code and submit the form)
		this.post_button.style.display = 'none';
		this.verify_button.style.display = 'inline';
	}

	//hide control code
	form.prototype.hide_control_code = function(){
		if(this.code_container){
		//check if the control code exists in the container
			if(this.code_container.hasChildNodes()){
				//hide control code
				this.captcha_container.style.display = 'none';
				//hide button to verify and display button to post
				this.post_button.style.display = 'inline';
				this.verify_button.style.display = 'none';
			}
		}
	}

	form.prototype.check_fields = function(){
		var add_subject = this.prefix + 'add_subject';
		var add_name = this.prefix + 'add_name';
		var add_text = this.prefix + 'add_text';

		var is_empty_field = false;

		if (document.getElementById(add_subject)){
			if ( (document.getElementById(add_subject).value == this.default_subject)
			|| (document.getElementById(add_subject).value == '' ) )
				is_empty_field = true;
		}

		if (document.getElementById(add_name)){
			if ( (document.getElementById(add_name).value == this.default_name)
			|| (document.getElementById(add_name).value == '' ) )
				is_empty_field = true;
		}

		if (document.getElementById(add_text)){
			if ( (document.getElementById(add_text).value == this.default_text)
			|| (document.getElementById(add_text).value == '' ) )
				is_empty_field = true;
		}

		if(document.getElementById('recaptcha_response_field')){
			if(document.getElementById('recaptcha_response_field').value == '')
				is_empty_field = true;
		}

		if (is_empty_field){
			//alert('Please fill all the fields');
			document.getElementById(this.prefix + 'empty_fields_message').style.display = 'block';
			return false;
		}
	}
}


//Show/hide the title of each icon in the icon bar and display the field to attach a file when the corresponding icon is clicked
function form_att_control(obj_form, obj, att_field_id){
	var myIcon = this;
	this.att_field_id = att_field_id;

	//initialize variables
	var icon_title_container = obj_form + 'icon_title_container';
	var att_field_container = obj_form + 'att_field_container';

	//verify objects exist
	if (!document.getElementById(obj) || !document.getElementById(icon_title_container) || !document.getElementById(att_field_container) || !document.getElementById(att_field_id)){
		return false;
	}

	//create elements
	this.obj = document.getElementById(obj);
	this.defaultTitle = this.obj.getAttribute('title');
	this.obj.setAttribute('title', '');
	this.icon_title_container = document.getElementById(icon_title_container);
	this.att_field_container = document.getElementById(att_field_container);
	this.att_field_original = document.getElementById(att_field_id);

	//base functions for inheritance, called when some event happens
	this.obj.onmouseover = function(){
		myIcon.mouseover();
	}

	this.obj.onmouseout = function(){
		myIcon.mouseout();
	}

	this.obj.onclick = function(){
		myIcon.click();
	}

	//onmouseover, show icon title
	form_att_control.prototype.mouseover = function(){
		icon_title_container_span = document.createElement('SPAN');
		icon_title_container_span.appendChild(document.createTextNode(this.defaultTitle));
		this.icon_title_container.appendChild(icon_title_container_span);
	}

	//onmouseout, remove icon title
	form_att_control.prototype.mouseout = function(){
		this.icon_title_container.removeChild(icon_title_container_span);
	}

	//onclick, show fields to attach files (up to 3 for each file type)
	form_att_control.prototype.click = function(){
		var num_children = 0;
		//get the array of fields
		var a = this.att_field_container.getElementsByTagName('DIV');

		//change the style of the attachments container
		this.att_container = document.getElementById(this.att_field_container.parentNode.id);
		if(this.att_container.className == 'float_attachments'){
			this.att_container.className = 'forum_attachments';
		}

		//go through the array and mark the number of children for each file type
		for (i = 0; i < a.length; i++){
			class_regexp = new RegExp(this.att_field_original.className);

			if(class_regexp.exec(a[i].className)){
				num_children ++;
			}
		}
		//only 3 children allowed
		if (num_children >= 3){
			return false;
		}

		//append the field to the container and display it in the form
		var copy = this.att_field_original.cloneNode(true);
		copy.id = null;

		this.att_field_container.appendChild(copy);
		copy.style.display = 'block';

		//add javascript to inputs
		if(this.att_field_id.indexOf('video') != -1 || this.att_field_id.indexOf('link') != -1)
			this.input_field = new pfInput(copy.getElementsByTagName('INPUT')[0]);

		//create an instance of the form_close_button class
		var controlButton = new form_close_button(copy.getElementsByTagName('IMG')[1], this.att_field_container);
	}
}

//when the close button is clicked the field is removed
function form_close_button(obj, main_cont) {
	var myButton = this;
	this.obj = obj;

	//base function for inheritance, called with onclick event
	this.obj.onclick = function(){
		myButton.close();
	}

	//remove button container from main container
	form_close_button.prototype.close = function(){
		main_cont.removeChild(this.obj.parentNode);
	}
}

//when an user clicks the textarea, the hint text is hidden and the the icons to attach files are displayed
function form_textarea(obj, obj_form){

	//initialize values
	var myText = this;
	this.obj = obj;
	this.text0 = 'text0';
	this.text1 = 'text1';
	this.defaultText = this.obj.value;

	//base functions for inheritance
	this.obj.onfocus = function(){
		myText.focus();
	}

	this.obj.onblur = function(){
		myText.blur();
	}

	//onfocus, remove hint text
	form_textarea.prototype.focus = function(){
		if (this.obj.value == this.defaultText) {
			this.obj.value = '';
			this.obj.className = this.text1;
		}
	}

	//onblur, displays hint text if the user did not enter any text
	form_textarea.prototype.blur = function(){
		if (this.obj.value == '') {
			this.obj.value = this.defaultText;
			this.obj.className = this.text0;
		}
	}
}

function get_offset(node) {
	if (node == document.body) {
		return 0;
	} else {
		return node.offsetLeft + get_offset(node.parentNode);
	}
}
