﻿/************************************************************
*	Développeur :	Manuel Santisteban						*
*	Contact :		manuel.santisteban@orange.fr			*
*	Site Internet :	http://www.xenero-developpement.com		*
*															*
*	Copyright 2009 Xen3r0 Développement.					*
************************************************************/

function getXhr() {
	var xhr = null; 
	
	if(window.XMLHttpRequest) {
		xhr = new XMLHttpRequest(); 
	}
	else if(window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else {
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
	
	return xhr
}

function is_int(mixed_var) {
	var y = parseInt(mixed_var * 1);

	if (isNaN(y)) {
		return false;
	}

	return mixed_var == y && mixed_var.toString() == y.toString(); 
}

function confirmDelete() {
	return confirm("Êtes-vous sûr de vouloir supprimer cet élément ?");
}

function ajaxLoadReg(codePays, codeReg, codeDept) {
	var xhr = getXhr();
	
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			document.getElementById('formAddListReg').innerHTML = xhr.responseText;
		}
	}
	
	xhr.open('GET','../inc/ajax.php?op=ajaxLoadReg&codePays='+escape(codePays)+'&codeReg='+escape(codeReg)+'&codeDept='+escape(codeDept), true);
	xhr.send(null);
}

function ajaxLoadDept(codeReg, codeDept) {
	var xhr = getXhr();
	
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			if(document.getElementById('formAddListDept')) {
				document.getElementById('formAddListDept').innerHTML = xhr.responseText;
			}
		}
	}
	
	xhr.open('GET','../inc/ajax.php?op=ajaxLoadDept&codeReg='+escape(codeReg)+'&codeDept='+escape(codeDept), true);
	xhr.send(null);
}

function ajaxLoadCommune(codePostal, codeCom) {
	var xhr = getXhr();
	
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			if(document.getElementById('formCartePostalAjax')) {
				document.getElementById('formCartePostalAjax').innerHTML = xhr.responseText;
			}
		}
	}
	
	xhr.open('GET','../inc/ajax.php?op=ajaxLoadCommune&codePostal='+escape(codePostal)+'&codeCom='+escape(codeCom), true);
	xhr.send(null);
}

function verifForm(form, listField) {
	var text = "";
	
	for(i=0;i<listField.length;i++) {
		if(form.elements[listField[i]]) {
			text += form.elements[listField[i]].value+"\n";
		}
	}
	
	alert(text);
	return false;
}

function verifNumeric(champs) {
	if(champs.value == "" || champs.value < 1) {
		champs.value = 1;
	}
	
	while(!is_int(champs.value)) {
		chaine = champs.value;
		chaine = chaine.substr(0, (chaine.length - 1));
		champs.value = chaine;
	}
}

function addToCart(idProd, formulaire) {
	var arrayRadio = getElementsByName_iefix('input', 'dim');
	var nbRadio = arrayRadio.length;
	var selectedRadio = 0;
	
	for(i=0; i<nbRadio; i++) {
		if(arrayRadio[i].checked) {
			selectedRadio = arrayRadio[i].value;
		}
	}
	
	if(selectedRadio == 0) alert("Aucune dimension n'a été choisie");
	else {
		var xhr = getXhr();
		
		xhr.onreadystatechange = function() {
			if(xhr.readyState == 4 && xhr.status == 200) {
				if(document.getElementById('linkPanierView')) {
					jQuery.facebox(xhr.responseText);
				}
			}
		}
		
		xhr.open('GET', './inc/ajax.php?op=addToCart&idProd='+escape(idProd)+'&dimension='+escape(selectedRadio)+'&quantite='+escape(formulaire.quantite.value), true);
		xhr.send(null);
		
		majTxtPanier();
	}
}

function majTxtPanier() {
	var xhr = getXhr();
	
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			document.getElementById('linkPanierView').innerHTML = xhr.responseText;
		}
	}
	
	xhr.open('GET', './inc/ajax.php?op=majTxtPanier', true);
	xhr.send(null);
}

function setEnvoi(idPanier, mode) {
	var xhr = getXhr();
	
	xhr.onreadystatechange = function() {
		if(xhr.readyState == 4 && xhr.status == 200) {
			window.location.reload(true);
		}
	}
	
	xhr.open('GET', './inc/ajax.php?op=setEnvoi&id='+escape(idPanier)+'&mode='+escape(mode), true);
	xhr.send(null);
}

function  afficheLinkCP(carte) {
	document.getElementById('linkCP_'+carte).style.display = 'block';
}

function  cacheLinkCP(carte) {
	document.getElementById('linkCP_'+carte).style.display = 'none';
}

function getElementsByName_iefix(tag, name) {
	var elem = document.getElementsByTagName(tag);
	var arr = new Array();
	
	for(i = 0,iarr = 0; i < elem.length; i++) {
		att = elem[i].getAttribute("name");
		if(att == name) {
			arr[iarr] = elem[i];
			iarr++;
		}
	}
	return arr;
}

function redirect(url){  
	window.location.href = url;
}

window.onload = function() {
	loadSpoiler();
}

