function open_overlay(id_to_open)
{
	jq('#'+id_to_open).dialog({
		width: 460,
		modal: true,
		buttons: {
			Ok: function(){
				jq(this).dialog("close");
			}
		}
	});
}

function open_overlay2(id_to_open, type_action, action)
{
	jq('#'+id_to_open).dialog({
		width: 460,
		modal: true,
		buttons: {
			Ok: function(){
				if (type_action==1)
				{
					jq(this).dialog("close");
					window.location=action;					
				}
			}
		}
	});
}

function open_overlay3(id_to_open, width, height, titre)
{
	jq('#'+id_to_open).dialog({
		width: width,
		height: height,
		closeOnEscape: false,
		modal: true,
		title: titre
	});
	jq('.ui-dialog-titlebar-close').hide();
}

function open_overlay4(id_to_open, width, height, titre)
{
	jq('#'+id_to_open).dialog({
		width: width,
		height: height,
		closeOnEscape: true,
		modal: true,
		title: titre
	});
}

function validate_member_code(){
	var member = jq("#champ_mbr").val();
	jq.ajax({
		type: "POST",
		url: "/GB/Publications/ajax/access_member.asp",
		data: ({member:member}),
		complete: function(){
			jq.unblockUI();
		},
		beforeSend: function(){
			jq.blockUI({
				message: '<h1><img src="/Images/busy.gif" /> Just a moment...</h1>'
			});
		},
		success: function(data){
			if (data == 1)
			{
				//alert("youpi");
				window.location.href=window.location.href;
			}
			else
			{
				//alert("Damn it!!");
				open_overlay('code_member_help');
			}
		}
	});
}

function is_existing(id,value,id_name){
	if(is_valid_mail(value))
	{
		jq.ajax({
			type: "POST",
			url: "/GB/Publications/ajax/form_exist.asp",
			data: ({id:id,value:value}),
			async: false,
			success: function(data){
				if (data==1)
				{
					jq('#alert_mail').remove();
					jq('#validation').removeAttr('disabled');
					jq('#'+id_name).css('background-color','#69ff69');
					mainForPageLoad();
				}
				else
				{
					jq('#alert_mail').remove();
					jq('#validation').attr('disabled','disabled');
					jq('#'+id_name).css('background-color','#ff6969');
					jq('#'+id_name).after('<span id="alert_mail"><br/>This email is already registered. Please use the forgotten password function <a href="/GB/pwdrecovery.asp">here</a></span>');
					mainForPageLoad();
				}
			}
		});
	}
	else
	{
		jq('#alert_mail').remove();
		jq('#'+id_name).after('<span id="alert_mail"><br/>This email is incorrect</span>');
		mainForPageLoad();
	}
}

function is_existing_modif(id,value,id_name,old_val){
	if(is_valid_mail(value))
	{
		jq.ajax({
			type: "POST",
			url: "/Asp/form_modif.asp",
			data: ({id:id,value:value,old_val:old_val}),
			async: false,
			success: function(data){
				if (data==1)
				{
					jq('#alert_mail').remove();
					jq('#validation').removeAttr('disabled');
					jq('#'+id_name).css('background-color','#69ff69');
					mainForPageLoad();
				}
				else if(data!=3)
				{
					jq('#alert_mail').remove();
					jq('#validation').attr('disabled','disabled');
					jq('#'+id_name).css('background-color','#ff6969');
					jq('#'+id_name).after('<span id="alert_mail"><br/>This email is already registered. Please use the forgotten password function <a href="/GB/pwdrecovery.asp">here</a></span>');
					mainForPageLoad();
				}
				else
				{
					jq('#alert_mail').remove();
					jq('#validation').removeAttr('disabled');
					jq('#'+id_name).css('background-color','#ffffff');
					mainForPageLoad();
				}
			}
		});
	}
	else
	{
		jq('#alert_mail').remove();
		jq('#'+id_name).after('<span id="alert_mail"><br/>This email is incorrect</span>');
		mainForPageLoad();
	}
}

function is_valid(type,id){
	var errors = "";
	var flag_nc = 0;
	var errors = "";
	jq('#'+type+' :input').each(function(){
		var val = "";
		switch (jq(this).attr('class'))
		{
			case 'obli' :
				var val = jq(this).val();
				if(val == "")
					errors += '- '+jq('#'+type+' .'+jq(this).attr('id')).text()+' is compulsory.\n';
				break;
			case 'mail' :
				var val = jq(this).val();
				if(val != "")
				{
					if(is_valid_mail(val) == 0) {
						errors += '- '+jq('#'+type+' .'+jq(this).attr('id')).text()+' is incorrect.\n';
					}
				}
				else
				{
					errors += '- '+jq('#'+type+' .'+jq(this).attr('id')).text()+' is compulsory.\n';
				}
				break;
			case 'radio' :
				if(jq(this).is(':checked'))
				{
					var val = jq(this).val();
					if(val != 2)
					{
						flag_nc = 1;
					}
				}
				break;
			case 'nc' :
				if(flag_nc == 1)
				{
					if(jq(this).val() == -1)
					{
						errors += '- The National Committee you choose is incorrect.\n';
					}
				}
				break;
		}
	});
	if (errors)
		alert('ERROR\nFields noted with red star must be filled\n\n'+errors);
	else
		validate_registration(id,type);
}

function is_valid_mail(value){
	var reg_mail = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
	if(reg_mail.test(value) == false) {
		return 0;
	}
	return 1;
}

function validate_registration(type,id){
	var values = jq('#'+id+' :input').serialize();
	if (type==1)
	{
		jq.ajax({
			type: "POST",
			url: "/Asp/register.asp",
			async: false,
			data: ({values:values}),
			success: function(data){
				if(data==1)
				{
					open_overlay2('registration_complete',1,'/GB/Publications/delivery_validation.asp');
				}
				else
				{
					//alert(data)
					alert("An error appears. Please contact the webmaster!");
				}
			}
		});	
	}
	if (type==2){
		jq.ajax({
			type: "POST",
			url: "/GB/publications/ajax/update_member.asp",
			async: false,
			data: ({values:values}),
			success: function(data){
				if(data==1)
				{
					window.location="purchase.asp";
				}
				else
				{
					alert("An error appears. Please contact the webmaster!");
				}
			}
		});	
	}
	if (type==3){
		jq.ajax({
			type: "POST",
			url: "/GB/publications/ajax/update_member_invoice.asp",
			async: false,
			data: ({values:values}),
			success: function(data){
				if(data==1)
				{
					window.location="purchase.asp";
				}
				else
				{
					alert("An error appears. Please contact the webmaster!");
				}
			}
		});	
	}
	if (type==4)
	{
		jq.ajax({
			type: "POST",
			url: "/Asp/register.asp",
			async: false,
			data: ({values:values}),
			success: function(data){
				if(data==1)
				{
					open_overlay2('registration_complete',1,'account.asp');
				}
				else
				{
					//alert(data)
					alert("An error appears. Please contact the webmaster!");
				}
			}
		});	
	}
	if (type==5)
	{
		//jq('#'+id+' :input').each(function(){(jq(this).val()).replace('&','&amp;');});
		var values = jq('#'+id+' :input').serialize();
		jq.ajax({
			type: "POST",
			url: "/Asp/modify.asp",
			async: false,
			data: ({values:values}),
			success: function(data){
				if(data==1)
				{
					open_overlay2('registration_complete',1,'my_account.asp');				
				}
				else
				{
					//alert(data)
					alert("An error appears. Please contact the webmaster!");
				}
			}
		});	
	}
}

function payment(type)
{
	var shipping;
	//alert(jq('.radio_delivery:checked').val());
	if(jq('.radio_delivery:checked').val()==1)
		shipping = 1;
	else
		shipping = 0;
	//alert(shipping);
	jq.ajax({
		type: "POST",
		url: "/GB/Publications/ajax/commande.asp",
		async: "false",
		data: ({type:type,shipping:shipping}),
		success: function(data){
			if (type==2)
			{
				jq('.num_commande').text(data);
				open_overlay3('payment_by_cheque_or_bank_transfer',800,800, 'Payment by check or bank tranfer');
			}
			else
			if (type==1)
			{
				//open_overlay3('/icold_cigb_payment/call_request.asp?'+data,800,800, 'Payment by credit card');
				//window.open('/icold_cigb_payment/call_request.asp?'+data,'Secure CB Payment','directories=0,location=0,menubar=0,toolbar=0');
				open_overlay_ajax2('div_overlay_mercanet', '/icold_cigb_payment/call_request.asp?'+data, 'Payment by credit card', 800, 600);
				//location.href='/icold_cigb_payment/call_request.asp?'+data;
			}
		}
	});
}

function pwd_recovery()
{
	var pwd_mail = jq('#chp_password').val();
	if (pwd_mail == "")
		return alert("The field is empty, please enter your email.\nLe champ est vide, veuillez entrer votre email");
	if (!is_valid_mail(pwd_mail))
		return alert("The email is not valid.\nL'email n'est pas valide.");
	jq.ajax({
		type: "POST",
		url: "/Asp/change_password.asp",
		async: "false",
		data: ({pwd_mail:pwd_mail}),
		success: function(data){
			if(data==1)
				alert("Your password was send to your email");
			else
			if(data==2)
				alert("An error occurs with your password. A new password was send to your email.");
			else
			if(data==3)
				alert("Your mail doesn't exist in our member database. Create an account.");
		}
	});
}

function end_of_transaction(type_transaction,name_of_overlay,langue){
	jq.ajax({
		type: "POST",
		url: "/GB/Publications/ajax/transaction_closure.asp",
		async: "false",
		data: ({type_transaction:type_transaction}),
		success: function(data){
			jq('#'+name_of_overlay).dialog('close');
			if (langue == 1)
				window.location="/home.asp";
			else if (langue == 3)
				window.location="/homeF.asp";
		}
	});
}

function update_shipping_fees(montant,poids,type_envoi)
{
	jq.ajax({
		type: "POST",
		url: "/GB/Publications/ajax/update_shipping_fees.asp",
		data: {
			montant:montant,poids:poids,type_envoi:type_envoi
		},
		async: false,
		success: function(data){
			var tab_result=data.split(":");
			jq('.fees').text(tab_result[0]);
			jq('.montant_and_fees').text(tab_result[1]);
		}
	});
}

function export_pdf(script_to_use,parameters)
{
	jq.ajax({
		type: "POST",
		url: script_to_use,
		data: ({parameters:parameters}),
		success: function(data){
			window.open("/userfiles/files/share/TC_"+data+"_directory.pdf","_blank");
		}
	});
}

function change_password(oldpwd, newpwd, confpwd)
{
	if(newpwd.match(confpwd)!=null)
	{
		jq.ajax({
			type: "POST",
			url: '/Asp/new_password.asp',
			data: ({oldpwd:oldpwd,newpwd:newpwd}),
			success: function(data){
				if(data==1)
					alert('Your password was successfully changed. You will receive an email with your new password.');
				else if(data==2)
					alert('Error with your password. Please check your enter the correct password.');
				jq('#div_overlay_changepwd').dialog("close");
			}
		});
	}
	else
	{
		alert("Error on confirmation password. Please enter the same password.");
		jq('#div_overlay_changepwd').dialog("close");
	}
}
