/*
  Fonctions javascript
  -----------------------------------------
  Module catalogue produit
  Auteur 	: Grégoire BLANCART
  Création	: 07.01.2010
  MàJ		: 05.10.2010
  Mail		: contact _at_ greg-ouaibe.com
*/

/* ----- fonctions standard ----- */
function getScrollXY() {
  var scrOfX = 0;
  var scrOfY = 0;

  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;

  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;

  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;

  }

  return scrOfX + "-" + scrOfY;

}


function getScreenSize() {
  var myWidth = 0;
  var myHeight = 0;

  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;

  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;

  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;

  }

  return myWidth + '-' + myHeight;

}


function getOffsetPosition(inID, inTYPE)
{
	var iVal = 0;
	var oObj = document.getElementById(inID);
	var sType = 'oObj.offset' + inTYPE;

	while (oObj && oObj.tagName != 'BODY') {
		iVal += eval(sType);
		oObj = oObj.offsetParent;
	}
	return iVal;
}

function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }

    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;

        }

    }

}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";

    if (opacity == 0) {
    	document.getElementById(id).style.display='none';

    } else {
		if (document.getSelection) {
			document.getElementById(id).style.display = 'table'; //(document.all && !opera)?'block':'table';

		} else if (document.selection) {
			document.getElementById(id).style.display = 'block';

		}

	}

}


function showFilterBox()
{
	if (document.getElementById('prd_filters').style.visibility != 'visible') {
		if (document.getElementById('prd_sort').style.visibility == 'visible') {
			document.getElementById('prd_sort').style.visibility = 'hidden';
			document.getElementById('prd_sort').style.display = 'none';

			opacity('prd_sort', 100, 0, 300);

		}

		document.getElementById('prd_filters').style.visibility = 'visible';
		document.getElementById('prd_filters').style.display = 'block';
		opacity('prd_filters', 0, 100, 300);

	} else {
		document.getElementById('prd_filters').style.visibility = 'hidden';
		document.getElementById('prd_filters').style.display = 'none';

		opacity('prd_filters', 100, 0, 300);

	}

}


function showSortBox()
{
	if (document.getElementById('prd_sort').style.visibility != 'visible') {
		if (document.getElementById('prd_filters').style.visibility == 'visible') {
			document.getElementById('prd_filters').style.visibility = 'hidden';
			document.getElementById('prd_filters').style.display = 'none';

			opacity('prd_filters', 100, 0, 300);

		}

		document.getElementById('prd_sort').style.visibility = 'visible';
		document.getElementById('prd_sort').style.display = 'block';
		opacity('prd_sort', 0, 100, 300);

	} else {
		document.getElementById('prd_sort').style.visibility = 'hidden';
		document.getElementById('prd_sort').style.display = 'none';

		opacity('prd_sort', 100, 0, 300);

	}

}


function setSort(iOption)
{
	document.getElementById('prd_sortoptions').value = iOption;

	updatePrdList();

}


/* ----- fonctions AJAX ----- */
function deleteCategory(iCatId)
{
	new Ajax.Updater(
		'delete_cat',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'cat', action: 'del', id: iCatId}
		}
	);

	var x = getOffsetPosition('pdc_' + iCatId, 'Left');
	var y = getOffsetPosition('pdc_' + iCatId, 'Top');

	var iTop = y;
	var iLeft = x + 20;

	document.getElementById('delete_cat').style.left = iLeft + 'px';
	document.getElementById('delete_cat').style.top = iTop + 'px';
	document.getElementById('delete_cat').style.visibility = 'visible';
	document.getElementById('delete_cat').style.display = 'block';

	opacity('delete_cat', 0, 100, 300);

}


function changeCatStatus(iCatId)
{
	new Ajax.Updater(
		'cat_picturestatus',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'cat', action: 'cs', id: iCatId}
		}
	);

}


function loadProducts(iCatId)
{
	new Ajax.Updater(
		'prd_productlist',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'lp', id: iCatId}
		}
	);

}


function updatePrdList()
{
	// fermeture des boites de dialogue
	if (document.getElementById('prd_filters').style.visibility == 'visible') {
		document.getElementById('prd_filters').style.visibility = 'hidden';
		document.getElementById('prd_filters').style.display = 'none';

		opacity('prd_filters', 100, 0, 300);

	}

	if (document.getElementById('prd_sort').style.visibility == 'visible') {
		document.getElementById('prd_sort').style.visibility = 'hidden';
		document.getElementById('prd_sort').style.display = 'none';

		opacity('prd_sort', 100, 0, 300);

	}

	// image de chargement
	document.getElementById('product_list').innerHTML = "<p align='center'><img src='aic_catalog/design/images/ajax-loader.gif' /></p>";


	// collecte des variables
	var sSort = document.getElementById('prd_sortoptions').value;
	var cat = document.getElementById('prd_catlist').value;
	tbCat = cat.split(',');
	var iTabCount = tbCat.length;
	var sCat = '';

	if (arguments.length > 0) {
		var iPage = arguments[0];

	} else {
		iPage = 1;

	}


	for (i=0;i<iTabCount;i++)
	{

		if (document.getElementById('filter' + tbCat[i]).checked == true) {
			if (sCat.length == 0) {
				sCat = tbCat[i];

			} else {
				sCat = sCat + " " + tbCat[i];

			}

		}

	}


	// updater ajax
	new Ajax.Updater(
		'product_list',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'upl', sort: sSort, id: sCat, page: iPage}
		}
	);
}


function searchProduct()
{
	var sText = document.getElementById('prd_search').value

	if (sText == '') {
		window.alert("Veuillez renseigner un texte ou des mots clé à rechercher");

	} else {
		new Ajax.Updater(
			'product_list',
			'ajax_common.php',
			{
				method: 'POST',
				parameters: {cat: 'prd', action: 'src', text: sText}
			}
		);

	}

}


function addToRelated(iPrdId)
{
	var sRelList = document.getElementById('prd_related').value;
	var tblList = sRelList.split(',');
	var iFound = 0;
	
	for (i=0;i<tblList.length;i++)
	{
		if (tblList[i] == iPrdId) {
			iFound = 1;
			
		}
		
	}

	if (iFound == 0) {
		if (sRelList.length > 0) {
			sRelList = sRelList + ',' + iPrdId;

		} else {
			sRelList = iPrdId;

		}

		document.getElementById('prd_related').value = sRelList;

		new Ajax.Updater(
			'prd_selected',
			'ajax_common.php',
			{
				method: 'POST',
				parameters: {cat: 'prd', action: 'urp', list: sRelList}
			}
		);
		
	}

}


function addToUsed(iPicId)
{
	var sUsedList = document.getElementById('prd_photolist').value;

	if (sUsedList.length > 0) {
		sUsedList = sUsedList + ',' + iPicId;

	} else {
		sUsedList = iPicId;

	}

	document.getElementById('prd_photolist').value = sUsedList;

	new Ajax.Updater(
		'prd_photolistdisp',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'uul', type: 'used', list: sUsedList}
		}
	);

}


function remUsedPic(iPicId)
{
	var sUsedList = document.getElementById('prd_photolist').value;
	var tblUsed = sUsedList.split(',');
	var sList = '';
	
	for (i=0;i<tblUsed.length;i++)
	{
		if (tblUsed[i] != iPicId) {
			if (sList.length == 0) {
				sList = tblUsed[i];
				
			} else {
				sList = sList + ',' + tblUsed[i];
				
			}
			
		}
	
	}

	document.getElementById('prd_photolist').value = sList;

	new Ajax.Updater(
		'prd_photolistdisp',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'uul', type: 'used', list: sList}
		}
	);
	
}


function changeMainPic(iPicId)
{
	var sUsedList = document.getElementById('prd_photolist').value;
	
	new Ajax.Request(
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'cmp', id: iPicId, list: sUsedList}
		}
	);
	
}


function deletePic(iPicId)
{
	var sList = document.getElementById('prd_allphotos').value;
	var sUsedList = document.getElementById('prd_photolist').value;
	var tblUsed = sUsedList.split(',');
	var tblList = sList.split(',');
	var iFound = 0;
	var tmpList = '';
	
	// on vérifie que l'image qu'on veut supprimer n'est pas utilisée pour le produit
	for (i=0;i<tblUsed.length;i++)
	{
		if (tblUsed[i] == iPicId) {
			iFound = 1;
			
		}
	
	}
	
	// suppression de l'image dans la liste
	for (i=0;i<tblList.length;i++)
	{
		if (tblList[i] != iPicId) {
			if (tmpList.length == 0) {
				tmpList = tblList[i];
				
			} else {
				tmpList = tmpList + ',' + tblList[i];
				
			}
			
		}
		
	}

	document.getElementById('prd_allphotos').value = tmpList;

	if (iFound == 1) {
		window.alert("L'image que vous voulez supprimer est actuellement utilisée. Enlevez-la de la liste des images utilisées puis supprimez-la.");
		
	} else {
		new Ajax.Request(
			'ajax_common.php',
			{
				method: 'POST',
				parameters: {cat: 'prd', action: 'pdl', id: iPicId}
			}
		);

		new Ajax.Updater(
			'photo_contents',
			'ajax_common.php',
			{
				method: 'POST',
				parameters: {cat: 'prd', action: 'uul', type: 'all', list: tmpList}
			}
		);
		
	}

}


function delRelPrd(iPrdId)
{
	var sList = document.getElementById('prd_related').value;
	var tblUsed = sList.split(',');
	var tmpList = '';
	
	for (i=0;i<tblUsed.length;i++)
	{
		if (tblUsed[i] != iPrdId) {
			if ( tmpList.length == 0) {
				 tmpList = tblUsed[i];
				
			} else {
				 tmpList =  tmpList + ',' + tblUsed[i];
				
			}
			
		}
	
	}
	
	document.getElementById('prd_related').value = tmpList;

	new Ajax.Updater(
		'prd_selected',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'urp', list: tmpList}
		}
	);
	
}


function deleteProduct(iPrdId)
{
	new Ajax.Updater(
		'delete_prd',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'del', id: iPrdId}
		}
	);

	var x = getOffsetPosition('pdp_' + iPrdId, 'Left');
	var y = getOffsetPosition('pdp_' + iPrdId, 'Top');

	var iTop = y;
	var iLeft = x + 20;

	document.getElementById('delete_prd').style.left = iLeft + 'px';
	document.getElementById('delete_prd').style.top = iTop + 'px';
	document.getElementById('delete_prd').style.visibility = 'visible';
	document.getElementById('delete_prd').style.display = 'block';

	opacity('delete_prd', 0, 100, 300);

}


function register()
{
	var sLogin = document.getElementById('us_login').value;
	var sPassword = document.getElementById('us_password').value;
	var sPassConf = document.getElementById('us_passconf').value;
	var sMail = document.getElementById('us_mail').value;
	var sUserName = document.getElementById('us_name').value;
	
	// affichage de l'image loader
	document.getElementById('user_register').innerHTML = "<p align='center'><img src='aic_catalog/design/images/ajax-loader.gif' /></p>";	

	new Ajax.Updater(
		'user_register',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'user', action: 'add', login: sLogin, pass: sPassword, conf: sPassConf, mail: sMail, username: sUserName}
		}
	);
	
}

function userLogin()
{
	var sLogin = document.getElementById('user_login').value;
	var sPassword = document.getElementById('user_password').value;
	
	// affichage de l'image loader
	document.getElementById('cat_tblogin').innerHTML = "<p align='center'><img src='aic_catalog/design/images/ajax-loader-2.gif' /></p>";		

	new Ajax.Updater(
		'cat_tblogin',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'user', action: 'login', login: sLogin, pass: sPassword}
		}
	);
	
}

function userLogout()
{
	
	// affichage de l'image loader
	document.getElementById('cat_tblogin').innerHTML = "<p align='center'><img src='aic_catalog/design/images/ajax-loader-2.gif' /></p>";		

	new Ajax.Updater(
		'cat_tblogin',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'user', action: 'logout'}
		}
	);
	
}

function changeMidpic(iPicId)
{
	new Ajax.Updater(
		'prd_det_midpic',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'catalog', action: 'cpp', id: iPicId}
		}
	);

}


function showPrdPage(iPage)
{
	// affichage de l'image loader
	document.getElementById('product_list').innerHTML = "<p align='center'><img src='aic_catalog/design/images/ajax-loader.gif' /></p>";	
	
	new Ajax.Updater(
		'product_list',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'rpl', page: iPage}
		}
	);

}


function sendIds()
{
	var sId = document.getElementById('user_id').value;
	
	if (sId == "") {
		window.alert("Veuillez renseigner votre adresse mail ou votre login !");
		
	} else {
		new Ajax.Updater(
			'cat_lostid',
			'ajax_common.php',
			{
				method: 'POST',
				parameters: {cat: 'user', action: 'lid', id: sId}
			}
		);	
		
	}

}


function setVote(iPid)
{
	var iMark = document.getElementById('user_mark').value;
	var sComment;

	if (document.getElementById('user_comment') != null) {
		sComment = document.getElementById('user_comment').value;
		
	} else {
		sComment = '';
		
	}

	new Ajax.Updater(
		'prd_eval',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'user', action: 'peval', id: iPid, mark: iMark, comment: sComment}
		}
	);
	
}

function closeElement(sElementId)
{
	opacity(sElementId, 100, 0, 300);
	$(sElementId).style.visibility = "hidden";
	$(sElementId).style.display = "none";

}


function setPopup(iPid)
{
	var iChecked;

	if ($('prd_' + iPid).checked == true) {
		iChecked = 1;
		
	} else {
		iChecked = 0;
		
	}
	
	new Ajax.Updater(
		'prd_popmsg',
		'ajax_common.php',
		{
			method: 'POST',
			parameters: {cat: 'prd', action: 'shp', id: iPid, checked: iChecked}
		}
	);	
	
}
