
/* ***************************************
		Infos Input System
  * ***************************************/


function restorePicturesInterface(NbPicturesMax)
{
	var iPicturesCount = GetCookie ("AddObjPicturesCount");
	var iUniqueID = GetCookie("AddObjUniqueID")
	
	// on clean
	for (i=NbPicturesMax; i > 0; i--)
	{
		hideThumbnailAfterdeletion(document, i);
	}

	if (iUniqueID && iPicturesCount && iPicturesCount > 0 && iPicturesCount <= NbPicturesMax) 
		for (i=1; i <= iPicturesCount; i++)
			if (i == 1)
				showThumbnailAfterInsertion(window.parent.document, i, "/user/tmp/temporary_" + iUniqueID + "_p.jpg");
			else
				showThumbnailAfterInsertion(window.parent.document, i, "/user/tmp/temporary_" + iUniqueID + "_" + i +"_p.jpg");
}

function restoreDescBoxStatus()
{
	// guarantee
	if (getObj(document, "GuaranteeConditions"))
		getObj(document, "GuaranteeConditions").onchange();

	// shipping
	var shipConditions = getObj(document, "ShippingConditions");

	if (shipConditions)
		shipConditions.onchange();

	// payment
	var payConditions = getObj(document, "PaymentConditions");
	if (payConditions)
		payConditions.onchange();

	// planned
	var chkPlanned = getObj(document, "chkStart1");

	if (chkPlanned)
	{
		// we can plan the auction ?
	    setDivStatus(document, "DivStartDate", chkPlanned.checked);
        if(chkPlanned.checked)
		    getObj(document, "StartMonth").onchange();
	}
}

function restorePageAppearance(restoreCatg, isWinterThur, IsEventCat){
	if (restoreCatg)
	{
		// categories
		restoreCatgListBoxStatus(0, false, true, "", false, IsEventCat);
	}
	
	// promotions & premiums
	restorePromotions();
	
	// pictures
	//restorePicturesInterface(5);

	// description boxes
	restoreDescBoxStatus();

	// shop or not shop ?
	refreshPricingDisplay(IsEventCat);
	
	if (isWinterThur)
	{
		// what to input ?
		changeLanguageInput();
    }
}

function checkSelectConditions(listbox, webIDC)
{
    var selectedValue = listbox.options[listbox.options.selectedIndex].value;

    // display/hide text area for description of corresponding listbox
    var isVisibleDesc = (selectedValue == 0);

	var nbParams = checkSelectConditions.arguments.length;
	var DivToDisplay = null;

	for (i = 2; i < checkSelectConditions.arguments.length; i++)
	{
	    DivToDisplay = checkSelectConditions.arguments[i];
	    setDivStatus(document, DivToDisplay + "Title", isVisibleDesc);
	    setDivStatus(document, DivToDisplay + "Body", isVisibleDesc);
	}

	// disable/enable shipping cost text box
	if (webIDC == 20 && listbox.id == "ShippingConditions") {
	    var ShippingCostInput = document.getElementsByName("ShippingCost");
	    var ShippingCostCentsInput = document.getElementsByName("ShippingCostCents");
	    if (selectedValue == 3) {
	        ShippingCostInput[0].value = "";
	        ShippingCostInput[0].disabled = true;
	        ShippingCostCentsInput[0].value = "";
	        ShippingCostCentsInput[0].disabled = true;
	    }
	    else {
	        ShippingCostInput[0].disabled = false;
	        ShippingCostCentsInput[0].disabled = false;
	    }
	}
}

function enableObjects (isEnabled /* objectName1, objectName2, etc... */)
{
	var nbParams = enableObjects.arguments.length;
	var arrParams = enableObjects.arguments;

	for (i=1; i<nbParams; i++)
		getObj(document, arrParams[i]).disabled = !isEnabled;
}

function refreshPricingDisplay(IsEventCat) 
{
    var chkIsClassified = getObj(document, 'chkClassified');

	if (chkIsClassified == null)
	{
		// normal mode
		var chkIsNormalPrice = getObj(document, 'chkShop0');
		var chkIsShop = getObj(document, 'chkShop1');
		var chkLT = getObj(document, 'chkLT');
		var chkST = getObj(document, 'chkST');
		var txthasReserve = getObj(document, 'hasReserveInNormal');
		var txthasBN = getObj(document, 'hasBuyNowInNormal');

		var hasReserve = (txthasReserve && (txthasReserve.value == "1"))
		var hasBuyNow = (txthasBN && (txthasBN.value == "1")) && chkIsNormalPrice.checked
						|| chkIsShop.checked;

		for (i=0; i < 5; i++)
		{
			// switch prices display
			setDivStatus(document, "DivStartPrice" + i, chkIsNormalPrice.checked);

			if (hasReserve)
				setDivStatus(document, "DivReservePrice" + i, chkIsNormalPrice.checked);

			setDivStatus(document, "DivIncrement" + i, chkIsNormalPrice.checked);
			setDivStatus(document, "DivBuyNowPrice" + i, hasBuyNow);
		}

	
		setDivStatus(document, "DivDuration0", chkIsNormalPrice.checked);
		//setDivStatus(document, "DivDuration1", !chkIsNormalPrice.checked && chkST.checked);
		setDivStatus(document, "DivDuration2", chkLT.checked);
		setDivStatus(document, "DivDuration1", chkST.checked);
	}
		
	refreshCosts(IsEventCat);
		
}

function checkPricingMode(isShop, subChoice, common_WebIDC, IsEventCat, isFPBmod) {
   
    var chkIsNormalPrice = getObj(document, 'chkShop0');
	var chkIsShop = getObj(document, 'chkShop1');
	var chkST = getObj(document, 'chkST');
	var chkLT = getObj(document, 'chkLT');
	var chkFPBmod = getObj(document, 'chkFPBmod');
	var divChoiceFP = getObj(document, 'div_ShowHideFPopt');
	var idBuyNowFee = getObj(document, 'id_buyNowFee');

	if (isShop)
	{
	    if (chkIsNormalPrice.disabled)
		{
			chkIsNormalPrice.checked = false;
		}
		else
			chkIsNormalPrice.checked = false ;//!chkIsShop.checked
			
		chkIsShop.checked = true;
		//case 577 - remove FPB 8-12-14-20
		if (common_WebIDC == 2 || common_WebIDC == 8 || common_WebIDC == 12 || common_WebIDC == 14 || common_WebIDC == 20) 
		{
		    if (chkLT.checked && isFPBmod == 1) 
            {}
            else 
            { divChoiceFP.style.display = 'none'; }
		}
        else 
        { divChoiceFP.style.display  = ''; }
		
		if(subChoice == 1)
		{
		    chkST.disabled = false;
		    chkLT.disabled = false;
		    chkST.checked = true;
		    chkLT.checked = false;
		}
		else
		{
		    chkST.disabled = false;
		    chkLT.disabled = false;
		    chkST.checked = false;
		    chkLT.checked = true;
		}
		idBuyNowFee.style.display = 'none';

		//case 577 - ongoing FPB 8-12-14-20 should still be displayed and disabled when they are modified
		if (isFPBmod == 1)
        {
		    chkST.disabled = true;
		    chkLT.disabled = true;
		    chkST.checked = false;
		    chkLT.checked = true;
		}
	}
	else
	{
		if (chkIsShop.disabled)
		{
			chkIsNormalPrice.checked = true;
			chkIsShop.checked = false;
		}
		else
		{
			chkIsShop.checked = !chkIsNormalPrice.checked
			var inputBuyNow = getObj(document, "BuyNowPrice");
			var inputBuyNowCents = getObj(document, "BuyNowPriceCents");

			if (inputBuyNow)
				inputBuyNow.value = "";

			if (inputBuyNowCents)
				inputBuyNowCents.value = "";
		}
        divChoiceFP.style.display  = 'none';
		chkST.checked = false;
		chkLT.checked = false;
		chkST.disabled = true;
		chkLT.disabled = true;
		idBuyNowFee.style.display = '';
	}

	refreshPricingDisplay(IsEventCat);

}

function checkPersonalPageStatus()
{
	var chkhasPerso = getObj(document, 'hasPersonalHomepage');
	var inputPerso = getObj(document, 'PersonalHomepage');

	if (chkhasPerso && inputPerso)
	 chkhasPerso.checked = (inputPerso.value.length > 0)
}

function checkSellingPossibility(articlegroup, IsEventCat)
{

    var chkIsNormalPrice = getObj(document, 'chkShop0');
	var chkIsShop = getObj(document, 'chkShop1');
	var chkIsClassified = getObj(document, 'chkClassified');
	var chkUserValidCPR_NoAC = getObj(document, 'checkboxUserValidCPR_NoAC');
	var divChoiceFP = getObj(document, 'div_ShowHideFPopt');

	if (chkIsClassified)
	{
		if ((articlegroup & 4) != 4)
		{
			// should not happen
			alert("ERROR 201 : please contact customer care");
		}
	}
	else 
	{
	    if (chkIsNormalPrice && chkIsShop)
		{	
			if (!chkIsNormalPrice.disabled || !chkIsShop.disabled)
			{
				var isOriginalyShop = chkIsShop.checked;
				
				chkIsNormalPrice.disabled = false;
				chkIsNormalPrice.checked = !isOriginalyShop;

				if (chkUserValidCPR_NoAC.value == 1) {
				    chkIsShop.disabled = true;
				    divChoiceFP.style.display = 'none';
				}
				else {
				    chkIsShop.disabled = false;
				}
				chkIsShop.checked = isOriginalyShop;
	
				if (((articlegroup & 1) != 1) && ((articlegroup & 2) != 2))
				{
					// should not happen
					alert("ERROR 202 : please contact customer care");
					if (chkIsNormalPrice)
						chkIsNormalPrice.disabled = true;
					if (chkIsShop)
						chkIsShop.disabled = true;
				}
				else
				{
					if ((articlegroup & 3) != 3)
					{
						// can't be Normal ?
						if ((articlegroup & 1) != 1)
							if (chkIsNormalPrice)
							{
								if (chkIsShop)
									chkIsShop.checked = true;
								else
									alert("ERROR 203 : please contact customer care");
								
								chkIsNormalPrice.checked = false;
								chkIsNormalPrice.disabled = true;
							}

						// can't be shop ?
						if ((articlegroup & 2) != 2)
							if (chkIsShop)
							{
								if (chkIsNormalPrice)
									chkIsNormalPrice.checked = true;
								else
									alert("ERROR 204 : please contact customer care");

								chkIsShop.checked = false;
								chkIsShop.disabled = true;
							}
					}
				}

                refreshPricingDisplay(IsEventCat);
            }
        }
    }

}

function checkStartDateMode(objCheck, activatePannel) {
	// display/hide the planned calendar
	uncheckOthers(objCheck, "chkStart");
	setDivStatus(document, "DivStartDate", activatePannel);
	
	checkSellingDateForCosts();
}

function changeCalendar (CalendarExtension)
{
	// display/hide calendar of this month or next month
	setDivStatus(document, "DivCalendar0", (CalendarExtension==0));
	if (getObj(document, "DivCalendar1"))
		setDivStatus(document, "DivCalendar1", (CalendarExtension==1));
}

function checkSellingDateForCosts()
{
	if (isDivVisible(document, "DivStartDate"))
	{
		var startday = parseInt(getObj(document, "startday").value);
		var startmonth = parseInt(getObj(document, "StartMonth").value);
		var starthourinmin = parseInt(getObj(document, "StartHour").value);
		var startmin = parseInt(getObj(document, "StartMin").value);
	
		var now = new Date();
		var sellingdate = new Date();
	
		sellingdate.setDate(now.getDate() + startday);
		sellingdate.setHours(starthourinmin/60);
		sellingdate.setMinutes(startmin);
		sellingdate.setSeconds(0);
		sellingdate.setMilliseconds(0);

		getObj(document, "SellingDateInSec").value = Math.round((sellingdate-now)/1000);
	}
	else
		getObj(document, "SellingDateInSec").value = 0;
	
    refreshCosts(0);
}

function chooseAdate (index, maxDays)
{
	// change appearance of the clicked day in the calendar
	for (i=0; i < maxDays; i++)
	{
		var linkcal = getObj(document, "LinkCal" + i);
		if (linkcal)
			linkcal.className = "";
	}

	var link = getObj(document, "LinkCal" + index);
	link.className = "n_StartDay";
	link.blur();
	
	getObj(document, "startday").value = index;

	checkSellingDateForCosts();
}


function checkOption(i)
{

      var arrOpt = new Array();

      arrOpt[0] = getObj(document, "PremiumListing");
      arrOpt[1] = getObj(document, "PremiumCategory");
      arrOpt[2] = getObj(document, "PremiumHomePage"); 
        
      arrOpt[3] = getObj(document, "OptionThumbnail");
      if(arrOpt[i])
        {	    
            arrOpt[3].disabled = arrOpt[i].checked;    
                
            arrOpt[1].disabled = arrOpt[2].checked;
            
            
	        for (var n = 0; n < arrOpt.length-1; n++)
	        {
		        if (arrOpt[n])
		            { 
			        arrOpt[n].checked = (arrOpt[i].checked && (n == i));
			        }
			 }
			         
			        
			var ctr = 0; 
			var ctr2 = 0; 
			if( arrOpt[1])
			{      
                ctr ++;
                if(!arrAllowOptions[1])
                {   
                ctr2++;
                    arrOpt[1].disabled = true;
                }
            }
            if( arrOpt[2])
			{      
                ctr ++;
                if(!arrAllowOptions[2])
                {
                    ctr2 ++;
                    arrOpt[2].disabled = true;
                 }
            }
            if( arrOpt[0])
			{      
                ctr ++;
                if(!arrAllowOptions[0])
                {
                    ctr2 ++;
                    arrOpt[0].disabled = true;
                 }
            }
            
            if(ctr == ctr2)
            {
                arrOpt[3].disabled = false;
             }
	    }
}

function restorePromotions()
{
	var arrOpt = new Array();

	arrOpt[0] = getObj(document, "PremiumListing");
	arrOpt[1] = getObj(document, "PremiumCategory");
	arrOpt[2] = getObj(document, "PremiumHomePage");

	for (var n = 0; n < arrOpt.length; n++)
		if (arrOpt[n] && arrOpt[n].checked)
			checkOption(n);
}

function CheckBackGroundOption() 
{
    //Subtitle
    if ($('#hasSubtitle').is(':checked')) {
        $('#hasSubtitleContainer').addClass('selected');
    }
    //Bold
    if ($('#OptionBold').is(':checked')) {
        $('#OptionBoldContainer').addClass('selected');
    }
    //Fluo
    if ($('#OptionFluo').is(':checked')) {
        $('#OptionFluoContainer').addClass('selected');
    }
    //Thumbnail
    if ($('#OptionThumbnail').is(':checked')) {
        $('#OptionThumbnailContainer').addClass('selected');
    }
    //Premium Listing
    if ($('#PremiumListing').is(':checked')) {
        $('#OptionThumbnailContainer').addClass('selected');
        $('#OptionThumbnail').attr('disabled', true);
        $('#OptionThumbnail').attr('checked', '');
        $('#PremiumListingContainer').addClass('selected');
    }
    //Premium Category
    if ($('#PremiumCategory').is(':checked')) {
        $('#OptionThumbnailContainer').addClass('selected');
        $('#OptionThumbnail').attr('disabled', true);
        $('#OptionThumbnail').attr('checked', '');
        $('#PremiumListingContainer').addClass('selected');
        $('#PremiumListing').attr('disabled', true);
        $('#PremiumListing').attr('checked', '');
        $('#PremiumCategoryContainer').addClass('selected');
    }
    //Premium HomePage
    if ($('#PremiumHomePage').is(':checked')) {
        $('#OptionThumbnailContainer').addClass('selected');
        $('#OptionThumbnail').attr('disabled', true);
        $('#OptionThumbnail').attr('checked', '');
        $('#PremiumListingContainer').addClass('selected');
        $('#PremiumListing').attr('disabled', true);
        $('#PremiumListing').attr('checked', '');
        $('#PremiumCategoryContainer').addClass('selected');
        $('#PremiumCategory').attr('disabled', true);
        $('#PremiumCategory').attr('checked', '');
        $('#PremiumHomePageContainer').addClass('selected');
    }

}

function checkBuyNowNQty()
{
    var inputQty = getObj(document, "InitialQuantity");
	var inputStart = getObj(document, "StartPrice");
	var inputStartCents = getObj(document, "StartPriceCents");
	var inputBuyNow = getObj(document, "BuyNowPrice");
	var inputBuyNowCents = getObj(document, "BuyNowPriceCents");	
    var isShop = (getObj(document, "chkShop1") ? getObj(document, "chkShop1").checked : false);

	var result = true;

	if (!isShop && inputQty && inputBuyNow && inputBuyNowCents && inputStart && inputStartCents && !isNaN(parseInt(inputQty.value)))
	{
		result = !(inputQty.value > 1
					&& (inputBuyNow.value != "" || inputBuyNowCents.value != "")
					&& (inputStart.value != "" || inputStartCents.value != ""));
	}
	
	return result;		
}

function changeLanguageInput()
{
	var selLang = getObj(document, "Language");
	var valLang = null;
	var isFrench = false;
	
	if (selLang)
	{
		valLang = selLang.options[selLang.selectedIndex].value;
		isFrench = (valLang == 3)
	}
	
	setDivStatus(document, 'DivTitleLang', !isFrench);
	setDivStatus(document, 'DivTitleLangFrench', isFrench);
	setDivStatus(document, 'DivDescriptionLang', !isFrench);
	setDivStatus(document, 'DivDescriptionLangFrench', isFrench);
	setDivStatus(document, 'DivAccessoriesDescriptionLang', !isFrench);
	setDivStatus(document, 'DivAccessoriesDescriptionLangFrench', isFrench);
	setDivStatus(document, 'DivShipDescLang', !isFrench);
	setDivStatus(document, 'DivShipDescLangFrench', isFrench);
	setDivStatus(document, 'DivPayDescLang', !isFrench);
	setDivStatus(document, 'DivPayDescLangFrench', isFrench);
}


function enableTags() 
{

	var Input = document.getElementsByTagName('input');
	var Select = document.getElementsByTagName('select');
	var Textarea = document.getElementsByTagName('Textarea');

	for (i = 0; i < Input.length; i++) {
		Input[i].disabled = false;
	}

	for (i = 0; i < Select.length; i++) {
		Select[i].disabled = false;
	}

	for (i = 0; i < Textarea.length; i++) {
		Textarea[i].disabled = false;
	}
}

/* ***************************************
		Pictures System
  * ***************************************/
function enableHasNewPictures (targetDocument)
{
	var newPicInput = getObj(targetDocument, "hasnewpictures");

	if (newPicInput)
		newPicInput.value = "1";				
}

function openPictureInputBox(targetdocument, pictureIndex, action, randomNumber)
{
	
    var clickedLink = getObj(targetdocument, "LinkPic" + action + pictureIndex);
 
    if (clickedLink)
    {
        // is link active ?
	    if (clickedLink.className != "n_PictureToChooseOff")
	    {
		    var isVisible = isDivVisible(targetdocument, 'DivpicInput');

		    var objPicIframe = getObj(targetdocument, 'picturesiframe');
		    var newLocation = "/form/addimageFrame.asp?index=" + pictureIndex + "&actiontype=" + action + "&rnd=" + randomNumber;
		    var oldLen = objPicIframe.src.length;
		    var newLen = newLocation.length;
		    var oldLocation = objPicIframe.src.substring(oldLen-newLen, oldLen);
		    var isSameLocation = (oldLocation == newLocation);

		    //if (!isSameLocation)
		    objPicIframe.src = newLocation;

		    // we show the div
		    setDivStatus(targetdocument, 'DivpicInput', !(isSameLocation && isVisible));
	    }
	    else
		    if (action == "add")
			    alert(lng_error_bad_picadd);
		    else
			    alert(lng_error_bad_picrem);

	    // remove selection
	    clickedLink.blur();
    }
}

function showThumbnailAfterInsertion(thTargetDocument, pictureIndex, picturePath)
{
	// hidding picture insertion div
	setDivStatus(thTargetDocument, "DivpicInput", false);
	
	//setting up new pic link to img object
	var picture = getObj(thTargetDocument, "picThumb" + pictureIndex);
	var tmpSrc = picturePath + "?" + Math.random();
	var setcookie = true;
	
	picture.src = tmpSrc;

	
	// showing picture display div
	setDivStatus(thTargetDocument, "DivPicAdd" + pictureIndex, false);
	setDivStatus(thTargetDocument, "DivPicShow" + pictureIndex, true);

	var lastLink = getObj(thTargetDocument, "LinkPicdel" + (pictureIndex-1));
	var currentLink = getObj(thTargetDocument, "LinkPicdel" + pictureIndex);
	var nextLink = getObj(thTargetDocument, "LinkPicadd" + (pictureIndex+1));
	
	if (lastLink)
		lastLink.className = "n_PictureToChooseOff";

	if (currentLink)
	{
		if (nextLink && nextLink.className == "n_PictureToChoose")
		{
			setcookie = false;
			currentLink.className = "n_PictureToChooseOff"; // no remove if other pic over it
		}
		else
			currentLink.className = "n_PictureToChoose";
	}

	if (nextLink)
		nextLink.className = "n_PictureToChoose";
	
	if (setcookie)
	{
		// we update the picture count
		getObj(thTargetDocument, "picturescount").value = pictureIndex;
		SetCookie ("AddObjPicturesCount", pictureIndex);
	}
}

function hideThumbnailAfterdeletion(thTargetDocument, pictureIndex,randomNumber)
{
	// hidding picture insertion div
	//setDivStatus(thTargetDocument, "DivpicInput", false);
	
	//setting up new pic link to img object
	getObj(thTargetDocument, "picThumb" + pictureIndex).src = blankPicPath;

	// hiding picture display div
	setDivStatus(thTargetDocument, "DivPicShow" + pictureIndex, false);
	setDivStatus(thTargetDocument, "DivPicAdd" + pictureIndex, true);

	var lastLink = getObj(thTargetDocument, "LinkPicdel" + (pictureIndex-1));
	var currentLink = getObj(thTargetDocument, "LinkPicdel" + pictureIndex);
	var nextLink = getObj(thTargetDocument, "LinkPicadd" + (pictureIndex+1));
	
	if (lastLink)
		lastLink.className = "n_PictureToChoose";

	if (currentLink)
		currentLink.className = "n_PictureToChooseOff";

	if (nextLink)
		nextLink.className = "n_PictureToChooseOff";
		
	// we update the picture count
	getObj(thTargetDocument, "picturescount").value = pictureIndex-1;
	SetCookie ("AddObjPicturesCount", pictureIndex-1);
}

// [PP] - 26/04/2007
function Fill_Availability() 
{ 	
   if (getObj(document, 'chkAvl0').checked) 
   //"On Stock" is checked
   { 
		getObj(document, 'Availability').value = 0; 
   } 
   else if (getObj(document, 'chkAvl1').checked) 
   // "Availability" is check --> get drop down value
   { 
		var SelectedIndex;

		//Get drop down selected index
		SelectedIndex = getObj(document, 'DD_Availability').selectedIndex;

		getObj(document, 'Availability').value = SelectedIndex + 1; 
   }
   else 
   // Nothing is checked --> [Availability] = "-1"
   {
		getObj(document, 'Availability').value = -1;
   }
}  

// (PP] - 26/06/2007
function uncheckOthersAvailability(checkboxUsed, namePattern)
{

	var isChecked = checkboxUsed.checked;
	
	var chkIndex = 0;
	var objCheck = getObj(document, namePattern + chkIndex);
	
	while (objCheck)
	{
		objCheck.checked = false;
		objCheck = getObj(document, namePattern + ++chkIndex);
	}
	
	if (isChecked)
	{
		checkboxUsed.checked = 1;
	}
}
