/**
 * Ajax Functions
 * @author Sebastien <sebastien@sonovente.com>
 */


/**
 * Send To Friend
 */

function send_to_friend(m, i)
{	
	switch ( m )
	{
		// Send
		case "s":
			new Ajax.Updater("send_to_friend", app_absurl + "ajax/send_to_friend.php", {
				method: "get",
				parameters:{
					m:m,
					i:i,
					n:$("stf_name").value,
					e:$("stf_email").value
				},
				onCreate: function() {
					$("send_to_friend").style.display = "block";
				}
			});
			
			break;
			
		// Display
		case "d":
		default:
			new Ajax.Updater("send_to_friend", app_absurl + "ajax/send_to_friend.php", {
				method: "get",
				parameters:{
					m:m,
					i:i
				},
				onCreate: function() {
					//Effect.toggle("send_to_friend", "slide");
					$("send_to_friend").style.display = "block";
				}
			});
			
			break;
	}

	return true;
}

function contact_advisor(i,t,p,e,r)
{
	try {
		myLightWindow.activateWindow({
			href: app_baseurl + "ajax/mail_advisor.php?i=" + i + "&p=" + p + "&r=" + r,
			title: t,
			width: 510,
			height: 305
		});
	} catch(err) {
		document.location.href = "mailto:" + e;
	}
}

function send_Mail_Advisor(id)
{
	$(id).request({
		evalJS:"force",
		onCreate: function() {
			$("adv_subject").style.border = "1px solid #000";	
			$("adv_email").style.border = "1px solid #000";	
			$("adv_text").style.border = "1px solid #000";
			
			$("adv_subject").disabled = true;	
			$("adv_email").disabled = true;	
			$("adv_text").disabled = true;
			$("adv_submit").disabled = true;
		
			$("adv_msg").style.color = "#999";
			$("adv_msg").innerHTML = "<img src=\""+ app_baseurl +"img/gif/working.gif\" style=\"position: relative; top: 3px;\" /> Envois du message en cours ...";
		},
		onComplete: function() {
			$("adv_subject").disabled = false;	
			$("adv_email").disabled = false;	
			$("adv_text").disabled = false;
		}
	});
}

/**
 * Look for a city by postal code
 * @param type integer
 * @return nothing
 */
function city_search(type)
{
	if ( $("cp_" + type).value.length >= 2 )
	{
		new Ajax.Updater("city_list_" + type, app_baseurl + "ajax/city.php", {
			method: "get",
			parameters:{
				c:$("cp_" + type).value,
				t:type
	 		},
	 		onCreate: function() {
	 			if ( $("img_working_" + type) != null )
	 			{
	 				$("img_working_" + type).style.display = "inline";
	 			}
	 		},
	 		onSuccess: function() {
	 			$("city_list_" + type).style.display = "block";
	 			
	 			if ( $("img_working_" + type) != null )
	 			{
	 				$("img_working_" + type).style.display = "none";
	 			}
	 		}
		});
	}
}
 
function city_assign(city_name, city_cp, city_pays, type)
{
	if ( city_cp != null )
	{
		$("cp_" + type).value = city_cp;
	}
	
	if ( city_name != null )
	{
		$("ville_" + type).value = city_name;
	}
	
	if ( city_name != null )
	{
		$("id_pays_" + type).value = city_pays;
		$("id_pays_" + type).style.backgroundImage = $("id_pays_" + type).options[$("id_pays_" + type).selectedIndex].style.backgroundImage;
	}
	
	$("city_list_" + type).style.display = "none";
}

/**
 * 
 */
function paiement(t, d)
{
	if ( typeof(d) == 'undefined' )
	{
		d = '';
	}
	 
	switch ( t )
	{
		case 'cb':
			new Ajax.Request(app_baseurl + 'ajax/paiement.php', {
	 			method:"post",
	 			parameters:{
	 				t:t,
	 				d:d
	 			},
	 			onCreate: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#666666",
	 					"fontStyle": "italic",
		 				"fontWeight": "normal"
	 				});
	 				$('content-submit-' + t).innerHTML   = '<img src="' + app_baseurl + 'img/gif/working.gif" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Enregistrement du mode de paiement ...';
	 			},
	 			onSuccess: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#666666",
	 					"fontStyle": "italic",
	 					"fontWeight": "normal"
	 				});
	 				$('content-submit-' + t).innerHTML   = '<img src="' + app_baseurl + 'img/gif/working.gif" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Redirection vers le serveur de la banque ...';
	 				$('FormulaireEncodeConforme').submit();
	 			},
	 			onFailure: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#ff0000",
	 					"fontWeight": "800"
	 				});
	 				$('content-submit-' + t).innerHTML = '<img src="' + app_baseurl + 'bo/img/icons/silk/error.png" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Une erreur s\'est produite lors de l\'enregistrement de votre mode de paiement.';
	 			}
	 		});

	 		break;
	 		
	 	case 'cheque':
	 	case 'vb':
	 		new Ajax.Request(app_baseurl + 'ajax/paiement.php', {
	 			method:"post",
	 			parameters:{
	 				t:t
	 			},
	 			onCreate: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#666666",
	 					"fontStyle": "italic",
		 				"fontWeight": "normal"
	 				});
	 				$('content-submit-' + t).innerHTML   = '<img src="' + app_baseurl + 'img/gif/working.gif" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Enregistrement du mode de paiement ...';
	 			},
	 			onSuccess: function() {
	 				document.location.href = app_baseurl + 'fin.htm';
	 			},
	 			onFailure: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#ff0000",
	 					"fontWeight": "800"
	 				});
	 				$('content-submit-' + t).innerHTML = '<img src="' + app_baseurl + 'bo/img/icons/silk/delete.png" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Une erreur s\'est produite lors de l\'enregistrement de votre mode de paiement.';
	 			}
	 		});

	 		break;
	 		
	 	case 'rp-comptant':
	 	case 'rp-credit':
	 		new Ajax.Request(app_baseurl + 'ajax/paiement.php', {
	 			method:"post",
	 			parameters:{
	 				t:t,
	 				d:d
	 			},
	 			onCreate: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#666666",
	 					"fontStyle": "italic",
		 				"fontWeight": "normal"
	 				});
	 				$('content-submit-' + t).innerHTML   = '<img src="' + app_baseurl + 'img/gif/working.gif" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Enregistrement du mode de paiement ...';
	 			},
	 			onSuccess: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#666666",
	 					"fontStyle": "italic",
		 				"fontWeight": "normal"
	 				});
	 				$('content-submit-' + t).innerHTML   = '<img src="' + app_baseurl + 'img/gif/working.gif" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Redirection vers le serveur de Fia-Net ReceiveAndPay ...';
	 				$('Formulaire-' + t).submit();
	 			},
	 			onFailure: function() {
	 				$('content-submit-' + t).setStyle({
	 					"color": "#ff0000",
	 					"fontWeight": "800"
	 				});
	 				$('content-submit-' + t).innerHTML = '<img src="' + app_baseurl + 'bo/img/icons/silk/delete.png" alt="" title="" style="border: 0px; width: 16px; height: 16px; position: relative; top: 3px;" /> Une erreur s\'est produite lors de l\'enregistrement de votre mode de paiement.';
	 			}
	 		});

	 		break;
	 }
 }
