function getDetails(type, id, site, params)
{
	var url = 'details.php?type='+type+'&id='+id;
	if (site) {
		url += '&site='+site;
	}
	if (params)
	{
		for(var i in params)
		{
			url += '&'+i+'='+params[i];
		}
	}
	popup = Popup.CreateMyPopup({
		name:'details', 
		replace:true, 
		//follow:true, 
		hideElement:false, 
		move:0, 
		ajax:{
			url:url,
			method:'GET'
		}
	});
}

function getHtmlList(ctype, name, id, args)
{
	if(typeof id == 'undefined') id = 0;
	if(typeof args == 'undefined') args = false;

	if(args !== false) URL = 'details.php'+args;
	else URL = 'details.php?type=list_'+ctype+'&name='+name+'&id='+id;

	popup = Popup.CreateMyPopup({
		name:'popup_list', 
		move:0, 
		zIndex:20, 
		startExec:'showMask();',
		closeExec:'hideMask();',
		ajax:{
			url:URL,
			type:false,
			method:'GET'
		}
	});

	var list_a = document.getElementById('popup_list_content').getElementsByTagName('a');
	var list_form = document.getElementById('popup_list_content').getElementsByTagName('form');
	
	for(element in list_a)
	{
		list_a[element].onclick = ajaxGoTo;
	}

	for(element in list_form)
    {
        list_form[element].onsubmit = ajaxFormTo;
    }
}

function ajaxGoTo(e)
{
	if (!e) e = window.event;
	var go = e.currentTarget ? e.currentTarget.search : e.srcElement.search;
	getHtmlList('', '', 0, go);
	return false;
}

function ajaxFormTo(e)
{
	if (!e) e = window.event;
	var formu = e.currentTarget || e.srcElement;
	var go = '?';
	var list_input = formu.getElementsByTagName('input');

	for(i=0;i<list_input.length;i++)
	{
		go += list_input[i].name+'='+list_input[i].value+'&';
	}

	getHtmlList('', '', 0, go);

	return false;
}

function ajaxGiftChoice(type, id)
{
	closePopup('popup_list');
	var url = document.location.pathname;
	var loc = url+'?type='+type+'&gid='+id;
	var site = verifSiteAssoc();
	if (site) {
		loc += '&site='+site;
	}
	document.location = loc;
}
function verifSiteAssoc()
{
	var select = document.getElementById('site_assoc');
	if (select) {
		var site = select.options[select.selectedIndex].value;

		if (!site) {
			alert('Vous devez sélectionner un site sur lequel le cadeau est associé');
			return false;
		}
		return site;
	}
}
function dump(obj)
{
	var text = '';
	for(idd in obj)
	{
		text += idd+' : '+obj[idd]+"\n";
	}
	alert(text);
}

function showMask()
{
	objects = document.getElementsByTagName("object");
	for(i=0;i<objects.length;i++)
	{
		objects[i].style.visibility = 'hidden';
	}
	document.getElementById('mask').style.display = 'block';
}

function hideMask()
{
	document.getElementById('mask').style.display = 'none';
	objects = document.getElementsByTagName("object");
	for(i=0;i<objects.length;i++)
	{
		objects[i].style.visibility = 'visible';
	}
}

var dcurrentCard = 0;
function switchImgOrder(type, oldImgId, newImgId)
{
	if(newImgId !== false && oldImgId != newImgId)
	{
		var myAjax = new MyAjax();
		myAjax.send({url:'drag.php?action=switch&type='+type+'&oldImg='+oldImgId+'&newImg='+newImgId+'&card='+dcurrentCard, method:'get', type:false});

		if(myAjax.returnResponse() == 'OK')
		{
			if(type == 'gift_inter') document.location.reload();

			var oldImg = new Image();
			var newImg = new Image();

			// On interverti les images et les ID
			oldImg.src = document.getElementById(oldImgId).src;
			newImg.src = document.getElementById(newImgId).src;

			document.getElementById(oldImgId).src = newImg.src;
			document.getElementById(newImgId).src = oldImg.src;


			var oldd = oldImgId;
			var neww = newImgId+"a";

			document.getElementById(oldImgId).id = neww;
			document.getElementById(newImgId).id = oldd;
			document.getElementById(neww).id = newImgId;
		}
	}
}

function switchDivOrder(oldId, obj)
{
	var oldInner = document.getElementById(oldId).innerHTML;
	var newInner = obj.element.innerHTML;
	var newId = obj.stopDrag();

	if(newId !== false && oldId != newId)
	{
		var myAjax = new MyAjax();
		myAjax.send({url:'drag.php?action=switch&type=gift_pos&oldImg='+oldId+'&newImg='+newId+'&card='+dcurrentCard, method:'get', type:false});
	
		if(myAjax.returnResponse() == 'OK')
		{
			document.getElementById(oldId).innerHTML = newInner;
			document.getElementById(newId).innerHTML = oldInner;
		}
		document.getElementById(oldId).id = newId+"a";
		document.getElementById(newId).id = oldId;
		document.getElementById(newId+"a").id = newId;
	}
}

function deleteImg(type, card, imgId)
{
	var myAjax = new MyAjax();
	myAjax.send({url:'drag.php?action=del&type='+type+'&card='+card+'&id='+imgId, method:'get', type:false});
	if(myAjax.returnResponse() == 'OK') document.location.reload();
}

function startCrop(type, id, imgId)
{
	$('#cropDialog').html('<iframe src="/ajax-crop.html?type='+type+'&typeId='+id+'&imgId='+imgId+'" marginWidth="0" marginHeight="0" width="100%" frameBorder="0" />').dialog('open');
}


function sendingElement(type, id)
{
	popup = Popup.CreateMyPopup({
		name:'popup_send', 
		move:0, 
		zIndex:20, 
		startExec:'showMask();',
		closeExec:'hideMask();',
		ajax:{
			url:'details.php?type='+type+'&action=send&id='+id,
			method:'GET'
		}
	});
}

var nb_max = 0;
var current_nb = 1;
function addInputFile(list_files)
{
    if(current_nb<nb_max || nb_max==0)
    {
        document.getElementById(list_files+'_div').appendChild(document.createElement('br'));
        newinput = document.createElement('input');
        newinput.setAttribute('name', list_files+'[]');
        newinput.setAttribute('type', 'file');
        newinput.setAttribute('class', 'input_3');
        newinput.setAttribute('onchange', 'addInputFile(\''+list_files+'\');');
        document.getElementById(list_files+'_div').appendChild(newinput);
        current_nb++;
    }
}

function commandTotalProcess(gifts)
{
	var total = 0;
	for(var i=0; i<gifts.length; i++)
	{
		var price = document.getElementById('price_'+gifts[i]).value*1;
		var qtt = document.getElementById('gift_'+gifts[i]).value*1;
		total += (price*qtt);
	}
	document.getElementById('commandTotal').value = Math.round(total*100)/100;
}


function popupButton(text)
{
	popup = Popup.CreateMyPopup({
		name:'infobul', 
		replace:true, 
		follow:true, 
		width:50,
		hideElement:false, 
		move:0, 
		zIndex:20, 
		content:'<nobr>'+text+'</nobr>'
	});
}

function showVideo(typ, skin)
{
	if(typeof skin == 'undefined') skin = 'default';
	popup = Popup.CreateMyPopup({
		name:'popup_video', 
		titre:'Les Personnalisations',
		startExec:'showMask();',
		closeExec:'hideMask();',
		skin:skin,
		ajax:{
			url:'video2.html?type='+typ,
			method:'GET'
		}
	});
}

function propalTotalProcess(mode)
{
	var totalht = 0;
	var totalttc = 0;
	var reduc_rate = 0;
	var reduc_total = 0;

	// Coût des cartes
	for(card_id in cards)
	{
		totalht += document.getElementById('card_qtt['+cards[card_id]+']').value*document.getElementById('card_price['+cards[card_id]+']').value;
	}
	if (mode == 'amount') {
		var reduc_total = document.getElementById('reduc_total').value;
		if(totalht > 0 && reduc_total > 0) reduc_rate = (reduc_total*100/totalht);
	} else {
		var reduc_rate = document.getElementById('reduc_rate').value;
		if(reduc_rate>0) reduc_total = (totalht/100*reduc_rate);
	}
	//else reduc_total = document.getElementById('reduc_total').value;
	//
	// Lignes supplémentaires
	for(i=0;i<10;i++)
	{
		totalht += document.getElementById('other_qtt['+i+']').value*document.getElementById('other_price['+i+']').value;
	}

	// Transport
	for(i=1;i<=4;i++)
	{
		totalht += document.getElementById('lot_qtt['+i+']').value*document.getElementById('lot_price['+i+']').value;
	}

	// Etranger
	for(i=10;i<=13;i++)
	{
		totalht += document.getElementById('lot_qtt['+i+']').value*document.getElementById('lot_price['+i+']').value;
	}

	// Credits
	for(zone_id in zones)
	{
		var qtt = document.getElementById('credit['+zones[zone_id]+']').value;
		totalht += qtt*document.getElementById('credit_price['+zones[zone_id]+']').value;
	}

	totalht = totalht-reduc_total;
	totalttc = totalht+(totalht/100*19.6);

	if (mode == 'amount') {
	document.getElementById('reduc_rate').value = Math.round(reduc_rate*100)/100;
	} else {
	document.getElementById('reduc_total').value = Math.round(reduc_total*100)/100;
	}
	document.getElementById('totalht').value = Math.round(totalht*100)/100;
	document.getElementById('totalttc').value = Math.round(totalttc*100)/100;
}

function resellerPropalTotalProcess(mode)
{
	var totalht = 0;
	var totalttc = 0;
	var reduc_rate = 0;
	var reduc_total = 0;

	// Coût des cartes
	$('input[name^=card_qtt]').each(function(){
		totalht += $(this).val() * $('input[name="'+$(this).attr('name').replace(/^card_qtt/,'card_price')+'"]').val();
	});

	// options supplémentaires
	$('select[name^=extra]').each(function(){
		var val = parseFloat($(this).val());
		if (val > 0)
		{
			totalht += val;
		}
	});

	if (mode == 'amount') {
		reduc_total = $('#reduc_total').val();
		if(totalht > 0 && reduc_total > 0) { reduc_rate = (reduc_total*100/totalht); }
	} else if (mode == 'rate') {
		reduc_rate = $('#reduc_rate').val();
		if(reduc_rate>0) { reduc_total = (totalht/100*reduc_rate); }
	} else {
		reduc_total = $('#reduc_total').val();
		reduc_rate = $('#reduc_rate').val();
	}

	// Coût des lots
	$('input[name^=lot_qtt]').each(function(){
		totalht += $(this).val() * $('input[id="'+$(this).attr('name').replace(/^lot_qtt/,'lot_price')+'"]').val();
	});

	// Crédits
	$('input[id^="credit["]').each(function(){
		totalht += $(this).val() * $('input[id="'+$(this).attr('id').replace(/^credit/,'credit_price')+'"]').val();
	});

	totalht = totalht-reduc_total;
	totalttc = totalht+(totalht/100*19.6);

	if (mode == 'amount') {
	$('#reduc_rate').val( Math.round(reduc_rate*100)/100);
	} else if (mode == 'rate') {
	$('#reduc_total').val(Math.round(reduc_total*100)/100);
	}
	$('#totalht').val(Math.round(totalht*100)/100);
	$('#totalttc').val(Math.round(totalttc*100)/100);
}
function mentionToggle()
{
	$('#mention').toggle();
}
function photoZoom(src, skin)
{
	if(typeof skin == 'undefined') skin = 'default';
	var size = 400;
	popup = Popup.CreateMyPopup({
		name:'popup_zoom', 
		zIndex:15,
		skin:skin,
		align:'center',
		content:'<img src="'+src+'" alt="photo" width="'+size+'" height="'+size+'" /><br/><a href="#" onclick="closePopup(\'popup_zoom\');return false;">Fermer le zoom</a>'
	});
}

function DeliveryCond()
{
	popup = Popup.CreateMyPopup({
		name:'popup_conditions', 
		zIndex:15,
		skin:'cartels',
		ajax:{
			url:'details.php?type='+type+'&action=send&id='+id,
			method:'GET'
		}
	});
}

function showErrors(errors, skin)
{
	if(typeof skin == "undefined") skin = 'default';

    popup = Popup.CreateMyPopup({
        name:'popup_errors',
        skin:skin,
		titre:'Erreurs',
		startExec:'showMask();',
        closeExec:'hideMask();',
		content:errors
    });
}

function viewSpeedSend(skin)
{
	if(typeof skin == 'undefined') skin = 'default';
	popup = Popup.CreateMyPopup({
        name:'popup_speedsend',
        titre:'Option de livraison en 24h',
        startExec:'showMask();',
        closeExec:'hideMask();',
		zIndex:1010,
		skin:skin,
		ajax:{
			url:'details.php?type=speedsend',
			method:'GET'
		}
    });
}
