/************************************************
 * 					SIFR
 ***********************************************/
if (typeof sIFR == "function") {
	sIFR.replaceElement(named({
						sSelector:"h2", 
						sFlashSrc:"/images/fonts/avenir-45.swf", 
						sColor: "#3e3d40",
						sWmode: "transparent"
					}));
}

/***** jQuery noConflict *****/
var $j = jQuery.noConflict();

/************************************************
 * 			LIBRARY
 ***********************************************/

/***** SITE *****/

var Site = function () {};

Site.initFormConnection = function () {
	if ($j('#connection').length) {
		$j.form.addField('connection', 'connection_login', {
			required : true,
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('connection', 'connection_password', {
			required : true,
			type : 'string',
			errorMessage : 'Please insert your password.',
			errorContainer : 'error'
		});
		
		$j('#connection').submit(function(){
			if (!$j.form.checkForm('connection')) {
				return false;
			}
		});
	}
};

Site.displayRole = function () {
	var head = $j('input[@name="account\[head\]"][@checked]').val();
	
	switch (head) {
		case 'no':
			$j('td.role').show();
			break;
		case 'yes':
		case undefined:
			$j('td.role').hide();
			break;
	}
};

Site.initFormAccount1 = function () {
	if ($j('#account').length) {
		
		$j.form.correctSelectWithIE('account_title');
		$j.form.correctSelectWithIE('account_school');
		
		/***** DISPLAY ROLE *****/
		Site.displayRole();
		$j('input[@name="account\[head\]"]').click(function(){
			Site.displayRole();
		});
		
		$j.form.addSelect('account', 'account_area', {
			required : true,
			type : 'string',
			tipValue:'Select your area',
			errorMessage : 'Please select your area.',
			errorContainer : 'error'
		});
		
		$j.form.addSelect('account', 'account_country', {
			required : true,
			type : 'string',
			tipValue:'Select your country',
			errorMessage : 'Please select your country.',
			errorContainer : 'error'
		});
		
		$j.form.addSelect('account', 'account_school', {
			required : true,
			type : 'string',
			tipValue:'Select your school',
			errorMessage : 'Please select your school.',
			errorContainer : 'error'
		});
		
		$j('#account').submit(function(){
			if (!$j.form.checkForm('account')) {
				return false;
			}
			
			if (!$j('input[name="account\[head\]"][@checked]').length) {
				$j.debug.displayError('Please tell us if you are the Head of Academic Body.', 'error');
				return false;
			}
			
			var head = $j('input[@name="account\[head\]"][@checked]').val();
			if (head == 'no' && !$j.string.exist($j('#account_title').val()) ) {
				$j.debug.displayError('Please select your title.', 'error');
				return false;
			}
		});
	}
};

Site.initFormAccount2 = function () {
	if ($j('#account2').length) {
		
		$j.form.addField('account2', 'account_last_name', {
			required : true,
			type : 'string',
			defaultValue : $j('#account_last_name').val(),
			errorMessage : 'Please insert your last name.',
			errorContainer : 'error'
		});
		
		$j.form.addField('account2', 'account_first_name', {
			required : true,
			type : 'string',
			defaultValue : $j('#account_first_name').val(),
			errorMessage : 'Please insert your first name.',
			errorContainer : 'error'
		});
		
		$j.form.addField('account2', 'account_billing_adress', {
			required : true,
			type : 'string',
			defaultValue : $j('#account_billing_adress').val(),
			errorMessage : 'Please insert your billing adress.',
			errorContainer : 'error'
		});
		
		$j.form.addSelect('account2', 'account_country', {
			required : true,
			type : 'string',
			tipValue : 'Select your country.',
			errorMessage : 'Please select your country.',
			errorContainer : 'error'
		});
		
/*
		$j.form.addField('account2', 'account_phone_number', {
			required : true,
			type : 'int',
			defaultValue : $j('#account_phone_number').val(),
			errorMessage : 'Please insert your phone number. Ex. 0954332963',
			errorContainer : 'error'
		});
*/
		
		$j.form.addField('account2', 'account_email', {
			required : true,
			type : 'email',
			defaultValue : $j('#account_email').val(),
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('account2', 'account_confirm_email', {
			required : true,
			type : 'email',
			defaultValue : $j('#account_confirm_email').val(),
			errorMessage : 'Please insert a correct confirming email.',
			errorContainer : 'error'
		});
		
		$j('#account2').submit(function(){
			if (!$j.form.checkForm('account2')) {
				return false;
			}
			
			if ($j('#account_email').val() != $j('#account_confirm_email').val()) {
				$j.debug.displayError('An error occured in your email field. Please correct it.', 'error');
				return false;
			}
		});
	}
};

Site.initFormModifyAccount1 = function () {
	if ($j('#modify_account').length) {
		
		$j.form.addField('modify_account', 'modify_account_last_name', {
			required : true,
			type : 'string',
			defaultValue : $j('#modify_account_last_name').val(),
			errorMessage : 'Please insert your last name.',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account', 'modify_account_first_name', {
			required : true,
			type : 'string',
			defaultValue : $j('#modify_account_first_name').val(),
			errorMessage : 'Please insert your first name.',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account', 'modify_account_billing_adress', {
			required : true,
			type : 'string',
			defaultValue : $j('#modify_account_billing_adress').val(),
			errorMessage : 'Please insert your billing adress.',
			errorContainer : 'error'
		});
		
		$j.form.addSelect('modify_account', 'modify_account_country', {
			required : true,
			type : 'string',
			tipValue : 'Select your country.',
			defaultValue : $j('#modify_account_country').val(),
			errorMessage : 'Please select your country.',
			errorContainer : 'error'
		});
		
/*
		$j.form.addField('modify_account', 'modify_account_phone_number', {
			required : true,
			type : 'int',
			defaultValue : $j('#modify_account_phone_number').val(),
			errorMessage : 'Please insert your phone number. Ex. 0954332963',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account', 'modify_account_fax', {
			required : true,
			type : 'int',
			defaultValue : $j('#modify_account_fax').val(),
			errorMessage : 'Please insert your fax number. Ex. 0954332963',
			errorContainer : 'error'
		});
*/
		
		$j.form.addField('modify_account', 'modify_account_email', {
			required : true,
			type : 'email',
			defaultValue : $j('#modify_account_email').val(),
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account', 'modify_account_confirm_email', {
			required : true,
			type : 'email',
			defaultValue : $j('#modify_account_confirm_email').val(),
			errorMessage : 'Please insert a correct confirming email.',
			errorContainer : 'error'
		});
		
		$j('#modify_account').submit(function(){
			if (!$j.form.checkForm('modify_account')) {
				return false;
			}
			
			if ($j('#modify_account_email').val() != $j('#modify_account_confirm_email').val()) {
				$j.debug.displayError('An error occured in your email field. Please correct it.', 'error');
				return false;
			}
			
			if ($j('#modify_account_password').val() != $j('#modify_account_confirm_password').val()) {
				$j.debug.displayError('An error occured in your password field. Please correct it.', 'error');
				return false;
			}
		});
	}
};

Site.initFormModifyAccount2 = function () {
	if ($j('#modify_account2').length) {
		
		$j.form.addSelect('modify_account2', 'modify_account2_civility', {
			required : true,
			type : 'string',
			errorMessage : 'Please select your gender.',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account2', 'modify_account2_last_name', {
			required : true,
			type : 'string',
			errorMessage : 'Please insert your last name.',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account2', 'modify_account2_first_name', {
			required : true,
			type : 'string',
			errorMessage : 'Please insert your first name.',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account2', 'modify_account2_adress', {
			required : true,
			type : 'string',
			errorMessage : 'Please insert your adress.',
			errorContainer : 'error'
		});

		$j.form.addField('modify_account2', 'modify_account2_country', {
			required : true,
			type : 'string',
			errorMessage : 'Please select your country.',
			errorContainer : 'error'
		});		
		
		$j.form.addSelect('modify_account2', 'modify_account2_title', {
			required : true,
			type : 'string',
			errorMessage : 'Please select your title.',
			errorContainer : 'error'
		});
		
/*
		$j.form.addField('modify_account2', 'modify_account2_phone_number', {
			required : true,
			type : 'int',
			errorMessage : 'Please insert your phone number. Ex. 0954332963',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account2', 'modify_account2_fax', {
			required : true,
			type : 'int',
			errorMessage : 'Please insert your fax number. Ex. 0954332963',
			errorContainer : 'error'
		});
*/
		
		$j.form.addField('modify_account2', 'modify_account2_email', {
			required : true,
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('modify_account2', 'modify_account2_confirm_email', {
			required : true,
			type : 'email',
			errorMessage : 'Please insert a correct confirming email.',
			errorContainer : 'error'
		});
		
		$j('#modify_account2').submit(function(){
			if (!$j.form.checkForm('modify_account2')) {
				return false;
			}
			
			if ($j('#modify_account2_email').val() != $j('#modify_account2_confirm_email').val()) {
				$j.debug.displayError('An error occured in your email field. Please correct it.', 'error');
				return false;
			}
		});
	}
};

Site.initFormOrder1 = function () {
	if ($j('#order1').length) {
			
		$j('#order1').submit(function(){
			if ($j("input:radio[@name='bypass'][@checked]").length != 1) {
				$j.debug.displayError('Click Yes to order this product or No to go ahead with the next step.');
				return false;
			}
			
			if ($j('#quantity').length && ($j("input:radio[@name='bypass'][@checked]").val() != '1') && $j('#quantity').val() == 0) {
				$j.debug.displayError('Please select your number of cards.');
				return false;
			}
		});
	}
};

Site.initCommandAdress = function () {
	if ($j('#command_adress').length) {
		
		$j.form.addField('command_adress', 'billing_contact', {
			required : true,
			type : 'string',
			defaultValue : $j('#billing_contact').val(),
			errorMessage : 'Please insert your billing name.',
			errorContainer : 'error'
		});
		
		$j.form.addField('command_adress', 'billing_school', {
			required : true,
			type : 'string',
			defaultValue : $j('#billing_school').val(),
			errorMessage : 'Please insert your billing school.',
			errorContainer : 'error'
		});
		
		$j.form.addField('command_adress', 'billing_address', {
			required : true,
			type : 'string',
			defaultValue : $j('#billing_address').val(),
			errorMessage : 'Please insert your billing adress.',
			errorContainer : 'error'
		});
		
		$j.form.addField('command_adress', 'billing_country', {
			required : true,
			type : 'string',
			defaultValue : $j('#billing_country').val(),
			errorMessage : 'Please insert your billing country.',
			errorContainer : 'error'
		});
		
		$j('#command_adress').submit(function(){
			if (!$j.form.checkForm('command_adress')) {
				return false;
			}
			
			if ($j("input:radio[@name='has_vat'][@checked]").length != 1) {
				$j.debug.displayError('Do you have a VAT number ?');
				return false;
			}
			
/*
			if ( $j("input:radio[@name='has_vat'][@checked]").val() == '1' && !$j.string.exist($j('#billing_vat_number').val()) ) {
				$j.debug.displayError('Please insert a VAT number.');
				$j('#billing_vat_number').focus();
				return false;
			}
*/			
		});
	}
}

Site.initFormProgramNames = function () {
	if ($j('#form_program_names').length) {
		
		$j.form.addField('form_program_names', 'name', {
			required : true,
			type : 'string',
			errorMessage : 'Please insert a name.',
			errorContainer : 'error'
		});
		
		$j('#form_program_names').submit(function(){
			if (!$j.form.checkForm('form_program_names')) {
				return false;
			}
		});
	}
};

Site.initFormAcademicNames = function () {
	if ($j('#form_academic_names').length) {
		
		$j.form.addField('form_academic_names', 'name', {
			required : true,
			type : 'string',
			errorMessage : 'Please insert a name.',
			errorContainer : 'error'
		});
		
		$j('#form_academic_names').submit(function(){
			if (!$j.form.checkForm('form_academic_names')) {
				return false;
			}
		});
	}
};

Site.initFormAssociationNames = function () {
	if ($j('#form_association_names').length) {
		
		$j.form.addField('form_association_names', 'name', {
			required : true,
			type : 'string',
			errorMessage : 'Please insert a name.',
			errorContainer : 'error'
		});
		
		$j('#form_association_names').submit(function(){
			if (!$j.form.checkForm('form_association_names')) {
				return false;
			}
		});
	}
};

Site.initSchoolMainForm = function () {
	if ($j('#school_main_form').length) {
		
		$j.form.addField('school_main_form', 'school_main_email', {
			required : false,
			defaultValue : $j('#school_main_email').val(),
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('school_main_form', 'school_main_nb_students', {
			required : false,
			defaultValue : $j('#school_main_nb_students').val(),
			type : 'int',
			errorMessage : 'Please insert an integer for this field.',
			errorContainer : 'error'
		});
		
		$j.form.addField('school_main_form', 'school_main_nb_teachers', {
			required : false,
			defaultValue : $j('#school_main_nb_teachers').val(),
			type : 'int',
			errorMessage : 'Please insert an integer for this field.',
			errorContainer : 'error'
		});
		
		$j.form.addField('school_main_form', 'school_main_nb_laboratories', {
			required : false,
			defaultValue : $j('#school_main_nb_laboratories').val(),
			type : 'int',
			errorMessage : 'Please insert an integer for this field.',
			errorContainer : 'error'
		});
		
		$j.form.addField('school_main_form', 'school_main_foreign_students_percentual', {
			required : false,
			defaultValue : $j('#school_main_foreign_students_percentual').val(),
			type : 'int',
			errorMessage : 'Please insert an integer for this field.',
			errorContainer : 'error'
		});
		
		$j('school_main_form').submit(function(){
			if (!$j.form.checkForm('school_main_form')) {
				return false;
			}
		});
	}
};

Site.initAcBody = function () {
	if ($j('#ac_body').length) {
		
		$j.form.addField('ac_body', 'academic_email', {
			required : false,
			defaultValue : $j('#academic_email').val(),
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('ac_body', 'academic_contact_email1', {
			required : false,
			defaultValue : $j('#academic_contact_email1').val(),
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('ac_body', 'academic_contact_email2', {
			required : false,
			defaultValue : $j('#academic_contact_email2').val(),
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('ac_body', 'academic_nb_students', {
			required : false,
			defaultValue : $j('#academic_nb_students').val(),
			type : 'int',
			errorMessage : 'Please insert an integer for this field.',
			errorContainer : 'error'
		});
		
		$j.form.addField('ac_body', 'academic_nb_teachers', {
			required : false,
			defaultValue : $j('#academic_nb_teachers').val(),
			type : 'int',
			errorMessage : 'Please insert an integer for this field.',
			errorContainer : 'error'
		});
		
		$j.form.addField('ac_body', 'academic_foreign_students_percentual', {
			required : false,
			defaultValue : $j('#academic_foreign_students_percentual').val(),
			type : 'int',
			errorMessage : 'Please insert an integer for this field.',
			errorContainer : 'error'
		});
		
		$j('ac_body').submit(function(){
			if (!$j.form.checkForm('ac_body')) {
				return false;
			}
		});
	}
};

Site.initFormEditProgram = function () {
  
	if ($j('#form_edit_program').length) {
		
		$j.form.addField('form_edit_program', 'contact_email1', {
			required : false,
			defaultValue : $j('#contact_email1').val(),
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j.form.addField('form_edit_program', 'contact_email2', {
			required : false,
			defaultValue : $j('#contact_email2').val(),
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});

		$j('form_edit_program').submit(function(){
			if (!$j.form.checkForm('form_edit_program')) {
				return false;
			}
		});
	}
};

Site.initAssociation = function () {
	if ($j('#association').length) {
		
		$j.form.addField('association', 'association_email', {
			required : false,
			type : 'email',
			errorMessage : 'Please insert a correct email.',
			errorContainer : 'error'
		});
		
		$j('association').submit(function(){
			if (!$j.form.checkForm('association')) {
				return false;
			}
		});
	}
};

$j(document).ready(function(){
	Site.initFormConnection();
	Site.initFormAccount1();
	Site.initFormAccount2();
	Site.initFormModifyAccount1();
	Site.initFormModifyAccount2();
	Site.initFormOrder1();
	Site.initCommandAdress();
	Site.initFormProgramNames();
	Site.initFormAcademicNames();
	Site.initFormAssociationNames();
	Site.initSchoolMainForm();
	Site.initAcBody();
	Site.initFormEditProgram();
});
