$(document).ready(function() {

var msgError = '';

var arrEmptyFields = new Array();

arrEmptyFields['ServiceName'] = 0;
arrEmptyFields['Operator'] = 0;
arrEmptyFields['Developer'] = 0;
arrEmptyFields['ContentProvider'] = 0;
arrEmptyFields['NetworkOperator'] = 0;
arrEmptyFields['AutoritingToolProvider'] = 0;
arrEmptyFields['Name'] = 0;
arrEmptyFields['Position'] = 0;
arrEmptyFields['Company'] = 0;
arrEmptyFields['Adress'] = 0;
arrEmptyFields['WebSite'] = 0;
arrEmptyFields['Mail'] = 0;
arrEmptyFields['Phone'] = 0;
arrEmptyFields['Mobile'] = 0;
arrEmptyFields['CompanyBackground'] = 0;
arrEmptyFields['ShortDescription'] = 0;
arrEmptyFields['category'] = 0;
arrEmptyFields['PaymentMethod'] = 0;

function checkInputText(idParam, titleParam){
	
	var text = $('#'+idParam).val();
	
	if(text == ''){
		$('#msg-' + idParam).slideDown('normal');
		$('#' + idParam).addClass('error');
		msgError = msgError +  ' - The field "' + titleParam +'" is wrong.\n';
		arrEmptyFields[idParam] = 1;
	}
	else {
		$('#msg-' + idParam).slideUp('fast');
		$('#' + idParam).removeClass('error');
		arrEmptyFields[idParam] = 0;
	}
}

function checkInputWebSite(idParam, titleParam){
	
	var text = $('#'+idParam).val();
	var regex = /^[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
	
	if(!text.match(regex)){
		$('#msg-' + idParam).slideDown('normal');
		$('#' + idParam).addClass('error');
		msgError = msgError +  ' - The field "' + titleParam +'" is wrong.\n';
		arrEmptyFields[idParam] = 1;
	}
	else if(text == ''){
		$('#msg-' + idParam).slideDown('normal');
		$('#' + idParam).addClass('error');
		msgError = msgError +  ' - The field "' + titleParam +'" is wrong.\n';
		arrEmptyFields[idParam] = 1;
	}
	else {
		$('#msg-' + idParam).slideUp('fast');
		$('#' + param).removeClass('error');
		arrEmptyFields[idParam] = 0;
	}
}

function checkInputMail(idParam, titleParam){
	
	var text = $('#'+idParam).val();
	var regex = /^[a-zA-Z0-9._-]+@[a-z0-9._-]{2,}\.[a-z]{2,4}$/;
	
	if(!text.match(regex)){
		$('#msg-' + idParam).slideDown('normal');
		$('#' + idParam).addClass('error');
		msgError = msgError +  ' - The field "' + titleParam +'" is wrong.\n';
		arrEmptyFields[idParam] = 1;
	}
	else if(text == ''){
		$('#msg-' + idParam).slideDown('normal');
		$('#' + idParam).addClass('error');
		msgError = msgError +  ' - The field "' + titleParam +'" is wrong.\n';
		arrEmptyFields[idParam] = 1;
	}
	else {
		$('#msg-' + idParam).slideUp('fast');
		$('#' + param).removeClass('error');
		arrEmptyFields[idParam] = 0;
	}
}

function checkInputPhone(idParam, titleParam){
	
	var text = $('#'+idParam).val();
	var regex = /^[0-9]{4,}$/;
	
	if(!text.match(regex)){
		$('#msg-' + idParam).slideDown('normal');
		$('#' + idParam).addClass('error');
		msgError = msgError +  ' - The field "' + titleParam +'" is wrong.\n';
		arrEmptyFields[idParam] = 1;
	}
	else if(text == ''){
		$('#msg-' + idParam).slideDown('normal');
		$('#' + idParam).addClass('error');
		msgError = msgError +  ' - The field "' + titleParam +'" is wrong.\n';
		arrEmptyFields[idParam] = 1;
	}
	else {
		$('#msg-' + idParam).slideUp('fast');
		$('#' + param).removeClass('error');
		arrEmptyFields[idParam] = 0;
	}
}

function checkInputCategoryChecked(){
	
	if($('#Category input:checked').length == 0){
		arrEmptyFields['Category'] = 1;
		msgError = msgError + ' - Please choose a category.\n';
	}
	else {
		arrEmptyFields['Category'] = 0;
	}
	
}

function checkInputPaymentMethodChecked(){
	
	if($('#PaymentMethod input:checked').length == 0){
		arrEmptyFields['PaymentMethod'] = 1;
		msgError = msgError + ' - Please choose a Payment Method.\n';
	}
	else {
		arrEmptyFields['PaymentMethod'] = 0;
	}
	
}

// Appel de la fonction en blur
	// Application Form
	// FormDescLeft	
	$('#ServiceName').blur(function() {
		checkInputText('ServiceName');
	});
	$('#Operator').blur(function() {
		checkInputText('Operator');
	});
	$('#Developer').blur(function() {
		checkInputText('Developer');
	});
	$('#ContentProvider').blur(function() {
		checkInputText('ContentProvider');
	});
	$('#NetworkOperator').blur(function() {
		checkInputText('NetworkOperator');
	});
	$('#AutoritingToolProvider').blur(function() {
		checkInputText('AutoritingToolProvider');
	});
	$('#Name').blur(function() {
		checkInputText('Name');
	});
	$('#Position').blur(function() {
		checkInputText('Position');
	});
	
	// FormDescRight
	$('#Company').blur(function() {
		checkInputText('Company');
	});
	$('#Adress').blur(function() {
		checkInputText('Adress');
	});
	$('#WebSite').blur(function() {
		checkInputWebSite('WebSite');
	});
	$('#Mail').blur(function() {
		checkInputMail('Mail');
	});
	$('#Phone').blur(function() {
		checkInputPhone('Phone');
	});
	$('#Mobile').blur(function() {
		checkInputPhone('Mobile');
	});
	$('#CompanyBackground').blur(function() {
		checkInputText('CompanyBackground');
	});
	$('#ShortDescription').blur(function() {
		checkInputText('ShortDescription');
	});
	
// Fin de la fonction en blur

// Debut de la fonction pour verif le form
	$('#SignupForm').submit(function() {
		
		msgError = '';
		
		// Application Form
		checkInputText('ServiceName','Nom du service / Service Name');
		checkInputText('Operator','Operateur / Operator');
		checkInputText('Developer','Developpeur / Developer');
		checkInputText('ContentProvider','Fournisseur de contenu / Content Provider');
		checkInputText('NetworkOperator','Operateur de diffusion / Network  Operator');
		checkInputText('AutoritingToolProvider','Fournisseur de l\'outil de dev / Autoring Tool Provider');
		checkInputText('Name','Nom du contact / Name');
		checkInputText('Position','Position');
		checkInputText('Company','Societe / Company');
		checkInputText('Adress','Adresse / Adress');
		checkInputText('WebSite','Site Web / Web Site');
		checkInputText('Mail','E-Mail / Mail');
		checkInputText('Phone','Telephone / Phone');
		checkInputText('Mobile','Mobile');
		checkInputText('CompanyBackground','Company Background');
		checkInputText('ShortDescription','Short Description of the ITV Service');
		
		// Category
		checkInputCategoryChecked();
		
		// Payment Method
		checkInputPaymentMethodChecked();

		if (arrEmptyFields['ServiceName'] == 0 && arrEmptyFields['Operator'] == 0 && arrEmptyFields['Developer'] == 0 && arrEmptyFields['ContentProvider'] == 0 && arrEmptyFields['NetworkOperator'] == 0 && arrEmptyFields['AutoritingToolProvider'] == 0 && arrEmptyFields['Name'] == 0 && arrEmptyFields['Position'] == 0 && arrEmptyFields['Company'] == 0 && arrEmptyFields['Adress'] == 0 && arrEmptyFields['ShortDescription'] == 0 && arrEmptyFields['CompanyBackground'] == 0 && arrEmptyFields['Phone'] == 0 && arrEmptyFields['Mail'] == 0 && arrEmptyFields['Mobile'] == 0 && arrEmptyFields['WebSite'] == 0 && arrEmptyFields['Category'] == 0 && arrEmptyFields['PaymentMethod'] == 0){
			alert('Your registration has been completed.\n Sending your files to our server can be long.\n Wait for the full display of the next page please.');
			return true;
		}
		else {
			alert(msgError);
			return false;
		}
	});

});
