/*
	Javascript Document
	Document Name: functions.js
	
	Description : THis is the main javaScript file that contains all functions used in website
	
	Author: Fuse Developments, Inc. Giancarlo Gomez
*/

var fileCount = 1;

function contactSubmit(){
	
	var url = 'actForms.cfm';
	var pars = $('contactForm').serialize(true);
	var myRegExp = /Succesful/;

	
	new Ajax.Request(url, {
		parameters: pars,
		method: 'post',
		onCreate: function(){
			$('submit').disable();
			$('theForm').setOpacity(0.3);
			$('processing').show();
		},
		onSuccess: function(transport) {


			if (transport.responseText.match(/Form /)){
				$('msg').update(transport.responseText);
				$('submit').enable();
				$('theForm').setOpacity(0);
				$('processing').hide();
				$('thankYou').show();
			}else{
				$('theImage').update(transport.responseText);
				$('submit').enable();
				$('theForm').setOpacity(1);
				$('processing').hide();
				$('captchaError').show();
			}
		}
	});

}

function getArticle(aid){
	
	var url = 'actForms.cfm';
	var pars = '?action=getArticle&aid='+aid;
	
	new Ajax.Request(url, {
		parameters: pars,
		method: 'post',
		onCreate: function(){
			$('articleView').update('<div class="fetch">Fetching Article ...</div>');
		},
		onSuccess: function(transport) {
			$('articleView').update(transport.responseText);
		}
	});

}

function addFileInput(){
	fileCount++;
	$('theFiles').insert('<div id="file' + fileCount + '"><input type="file" name="files' + fileCount + '" style="width:auto;" /> <a href="#" onclick="addFileInput();return false;" class="plus">+</a> <a href="#" onclick="removeFileInput('+ fileCount +');return false;" class="minus">-</a></div>'); 
}

function removeFileInput(theID){
	$('file' + theID).remove();
}

function fileUploadCheck(){
	
	// get all file inputs
	var theFiles = $('fileForm').getInputs('file');
	// by default we set the submit var to false
	var submitForm = false;
	
	// check that at least one file input has a value before submitting
	theFiles.each(function(e, i) { 
		if(e.value != '')
		{
			console.log(e);
			submitForm = true;
			throw $break;
		}
	}); 
	
	if (!submitForm){
		// notify of error
		alert('You must at least select one file to upload');	
	}else{
		// good for upload
		$('fileForm').submit();	
	}
	
	
}


