/**
 * Global JS functions
 */

/**
 * Include CSS code
 * @param string cssCode
 * @return bool
 */
$.includeCssCode = function(cssId, cssCode){
	var included = false;
	if(!$('head style#'+cssId).is('style')){
		var style = document.createElement('style');
		style.setAttribute('type', 'text/css');
		style.setAttribute('id', cssId);
		document.getElementsByTagName('head')[0].appendChild(style);
		if(style.styleSheet){
			style.styleSheet.cssText = cssCode;
		}
		else{
			content = document.createTextNode(cssCode);
			style.appendChild(content);
		}
		included = true;
	}
	return included;
};

/**
 * Include CSS file content
 * @param string cssFileUrl
 * @return bool
 */
$.includeCssFile = function(cssFileUrl){
	var included = false;
	var cssId = cssFileUrl.replace(new RegExp("(/|\\.)", "g"), '-');
	if(!$('head style#'+cssId).is('style')){
		$.ajax({
			url: cssFileUrl+'?_='+Math.random(),
			dataType: 'html',
			async: false,
			complete: function(jqXHR){
				included = $.includeCssCode(cssId, jqXHR.responseText);
			}
		});
	}
	return included;
};

/**
 * @param string jsCode
 * @return void
 */
$.includeJsCode = function(jsCode){
	if(jsCode && jsCode.length > 0){
		try{
			eval(jsCode);
		}
		catch(err){
			$.log('EVAL failed\nName: '+err.name+'\nMsg: '+err.message+'\n'+jsCode);
		}
	}
};

/**
 * Include & execute JS file
 * @param string jsFileUrl
 * @return bool
 */
$.includeJsFile = function(jsFileUrl){
	var included = false;
	$.ajax({ url: jsFileUrl, dataType: 'script', async: false, complete: function(){ included = true; }});
	return included;
};

/**
 * @param string tagId
 * @param int pos [optional]
 * @return string
 */
$.extractId = function(tagId, pos){
	var pos = (pos) ? pos: 5;
	return tagId.substring(pos, tagId.length);
}

/**
 * @return array
 */
$.fn.getFormElements = function(){
	var elements = {};
	$(this).find('input:not(input[type="submit"],input[type="reset"],input[type="button"],input[type=radio]:not(:checked)), textarea, select,').each(function(index, element){
		elements[index] = $(element);
	});
	return elements;
}

/**
 * @return array
 */
$.fn.getFields = function(){
	var fields = {};
	var elements = $(this).getFormElements();
	for(index in elements){
		var element = elements[index];
		fields[$(element).attr('name')] = $(element).val();
	}
	return fields;
}

/**
 * @return array
 */
$.fn.checkFields = function(){
	var checked = true;
	var elements = $(this).getFormElements();
	var firstInvalid = false;
	for(index in elements){
		var element = elements[index];
		var hasError = false;
		var value = $(element).val();
		if($(element).hasClass('check-require')){
			if(($(element).attr('type') == 'checkbox') && !$('#estimation-field-cgu').attr('checked')){
				hasError = true;
			}
			else if(value.length == 0){
				hasError = true;
			}
		}
		if(!hasError && (value.length > 0)){
			if($(element).hasClass('check-string')){
				var stringReg = /^([\w- áàâäåéèêëíìîïóòôöúùûüçÿø]+)$/;
				if(!stringReg.test(value)){
					hasError = true;
				}
			}
			if($(element).hasClass('check-mail')){
				var mailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!mailReg.test(value)){
					hasError = true;
				}
			}
			else if($(element).hasClass('check-int')){
				var intReg = /^([0-9]+)$/;
				if(!intReg.test(value)){
					hasError = true;
				}
			}
			else if($(element).hasClass('check-phone')){
				var intReg = /^([0-9]+)$/;
				if(!intReg.test(value) || (value.length != 10)){
					hasError = true;
				}
			}
			else if($(element).hasClass('check-date')){
				var intReg = /^([0-9]{2}\/[0-9]{2}\/[0-9]{4})$/;
				if(!intReg.test(value) || (value.length != 10)){
					hasError = true;
				}
			}
			else if($(element).hasClass('check-time')){
				var intReg = /^([0-9]{2}\:[0-9]{2})$/;
				if(!intReg.test(value) || (value.length != 5)){
					hasError = true;
				}
			}
		}
		if(hasError){
			checked = false;
			$(element).css('border-color', 'red');
			if(firstInvalid == false){
				firstInvalid = element;
			}
		}
		else{
			$(element).css('border-color', '#c0c0c0');
		}
	}
	if(firstInvalid !== false){
		$(firstInvalid).focus();
	}
	return checked;
}
