/*
####################################################
############Free Prize Draw Entry Code##############
####################################################
*/

function FreePrizeInitialisation()
{
	var liPrizeCounter = 0;
	if(gaPrizeIdList.length==0 && gaPrizeDrawIdList.length==0 && gaPrizeNoList.length==0 && document.getElementById('PrizeIdList').value.length > 0 && ((document.getElementById('PrizeIdList').value.split(",").length==document.getElementById('PrizeDrawIdList').value.split(",").length) && (document.getElementById('PrizeDrawIdList').value.split(",").length==document.getElementById('PrizeNoList').value.split(",").length)))
	{
		gaPrizeIdList = document.getElementById('PrizeIdList').value.split(",")//AddArrayItems(document.getElementById('PrizeIdList').value)
		gaPrizeDrawIdList = document.getElementById('PrizeDrawIdList').value.split(",")//AddArrayItems(document.getElementById('PrizeDrawIdList').value)
		gaPrizeNoList = document.getElementById('PrizeNoList').value.split(",")//AddArrayItems(document.getElementById('PrizeNoList').value)
		
		for(liPrizeCounter=0;liPrizeCounter<gaPrizeIdList.length;liPrizeCounter++)
		{
			if(document.getElementById('Title' + gaPrizeIdList[liPrizeCounter])!=null)
			{
				document.getElementById('Title' + gaPrizeIdList[liPrizeCounter]).style.backgroundImage = "url(/butineo/images/freeprizeentry/selected_entry.gif)"								
				
			} //document.getElementById('Title' + gaPrizeIdList[liPrizeCounter])!=null
			
			if(document.getElementById('chkFreePrizeEntry_' + gaPrizeDrawIdList[liPrizeCounter] + '_' + gaPrizeIdList[liPrizeCounter] + '_' + gaPrizeNoList[liPrizeCounter]) != null)
			{
			    document.getElementById('chkFreePrizeEntry_' + gaPrizeDrawIdList[liPrizeCounter] + '_' + gaPrizeIdList[liPrizeCounter] + '_' + gaPrizeNoList[liPrizeCounter]).checked = true;
			}
			
		} //liPrizeCounter=0;liPrizeCounter<gaPrizeIdList.length;liPrizeCounter++
	} //gaPrizeIdList.length==0 && gaPrizeDrawIdList.length==0 && gaPrizeNoList.length==0 && document.getElementById('PrizeIdList').value.length > 0 && ((document.getElementById('PrizeIdList').value.split(",").length==document.getElementById('PrizeDrawIdList').value.split(",").length) && (document.getElementById('PrizeDrawIdList').value.split(",").length==document.getElementById('PrizeNoList').value.split(",").length))
	
} //FreePrizeInitialisation



//emanuel ddd
function SelectedFreePrizesOptimized(liPrizeId, liPrizeDrawId, liPrizeNo)
{
	var loSelectedCheckBox = document.getElementById('chkFreePrizeEntry_' + liPrizeDrawId + '_' + liPrizeId + '_' + liPrizeNo)
	var loTotalFreePrizeCount = document.getElementById('totalFreePrizeCount')
	var loPrizeIdList = document.getElementById('PrizeIdList')
	var loPrizeDrawIdList = document.getElementById('PrizeDrawIdList')					
	var loPrizeNoList  = document.getElementById('PrizeNoList') 
	var laTempReturnValues = new Array()
	
	if(!loSelectedCheckBox.checked){
		loTotalFreePrizeCount.value--;
		laTempReturnValues = RemoveFromList(new Array(gaPrizeIdList,gaPrizeNoList), liPrizeId)
	
		gaPrizeIdList = laTempReturnValues[0]
		gaPrizeNoList = laTempReturnValues[1]
		laTempReturnValues = RemoveFromList(new Array(gaPrizeDrawIdList), liPrizeDrawId)
		gaPrizeDrawIdList = laTempReturnValues[0]
		
	}
	else //loSelectedTableImage.style.backgroundImage == "url(/butineo/images/freeprizeentry/selected_entry.gif)"
	{
		if(loTotalFreePrizeCount.value < giFreePrizeDrawCount)
		{
			loTotalFreePrizeCount.value++;
			if (!AddToList(gaPrizeIdList,liPrizeId))
			{
				gaPrizeNoList[gaPrizeNoList.length] = liPrizeNo
			}
			AddToList(gaPrizeDrawIdList, liPrizeDrawId)
		}
		else //loTotalFreePrizeCount.value < giFreePrizeDrawCount
		{
			alert(gsFreePrizeDrawEntryMsg);
		} //loTotalFreePrizeCount.value < giFreePrizeDrawCount

	} //loSelectedTableImage.style.backgroundImage == "url(/butineo/images/freeprizeentry/selected_entry.gif)"
	
	loPrizeIdList.value = gaPrizeIdList.join(",")
	loPrizeDrawIdList.value = gaPrizeDrawIdList.join(",")
	loPrizeNoList.value = gaPrizeNoList.join(",")
	
	if(loTotalFreePrizeCount.value == giFreePrizeDrawCount)
	{
		document.forms.CompleteFreePrizeDrawEntryProcess.submit()
	}
	
} //SelectedFreePrizes(liPrizeId, liPrizeDrawId, liPrizeNo)


function SelectedFreePrizes(liPrizeId, liPrizeDrawId, liPrizeNo)
{
	//var loPrizeText = document.getElementById('Title' + liPrizeId)
	var loSelectedTableImage = document.getElementById('Title' + liPrizeId)
	var loSelectedCheckBox = document.getElementById('chkFreePrizeEntry_' + liPrizeDrawId + '_' + liPrizeId + '_' + liPrizeNo)
	var loTotalFreePrizeCount = document.getElementById('totalFreePrizeCount')
	var loPrizeIdList = document.getElementById('PrizeIdList')
	var loPrizeDrawIdList = document.getElementById('PrizeDrawIdList')					
	var loPrizeNoList  = document.getElementById('PrizeNoList') 
	var laTempReturnValues = new Array()
	
	if(loSelectedTableImage.style.backgroundImage == "url(/butineo/images/freeprizeentry/selected_entry.gif)"){
		loTotalFreePrizeCount.value--;
		loSelectedTableImage.style.backgroundImage = "url(/butineo/images/freeprizeentry/free_entry_flash.gif)"
		loSelectedCheckBox.checked = false;
		
		laTempReturnValues = RemoveFromList(new Array(gaPrizeIdList,gaPrizeNoList), liPrizeId)
	
		gaPrizeIdList = laTempReturnValues[0]
		gaPrizeNoList = laTempReturnValues[1]
		laTempReturnValues = RemoveFromList(new Array(gaPrizeDrawIdList), liPrizeDrawId)
		gaPrizeDrawIdList = laTempReturnValues[0]
		
	}
	else //loSelectedTableImage.style.backgroundImage == "url(/butineo/images/freeprizeentry/selected_entry.gif)"
	{
		if(loTotalFreePrizeCount.value < giFreePrizeDrawCount)
		{
			loTotalFreePrizeCount.value++;
			loSelectedCheckBox.checked = true;
			loSelectedTableImage.style.backgroundImage = "url(/butineo/images/freeprizeentry/selected_entry.gif)"
			if (!AddToList(gaPrizeIdList,liPrizeId))
			{
				gaPrizeNoList[gaPrizeNoList.length] = liPrizeNo
			}
			AddToList(gaPrizeDrawIdList, liPrizeDrawId)
			
		}
		else //loTotalFreePrizeCount.value < giFreePrizeDrawCount
		{
			alert(gsFreePrizeDrawEntryMsg);
		} //loTotalFreePrizeCount.value < giFreePrizeDrawCount

	} //loSelectedTableImage.style.backgroundImage == "url(/butineo/images/freeprizeentry/selected_entry.gif)"
	
	loPrizeIdList.value = gaPrizeIdList.join(",")
	loPrizeDrawIdList.value = gaPrizeDrawIdList.join(",")
	loPrizeNoList.value = gaPrizeNoList.join(",")
	
	if(loTotalFreePrizeCount.value == giFreePrizeDrawCount)
	{
		document.forms.CompleteFreePrizeDrawEntryProcess.submit()
	}
	
} //SelectedFreePrizes(liPrizeId, liPrizeDrawId, liPrizeNo)

function AddToList(laList,liAdd)
{
	var bFound = false;
	for(var i=0; i<laList.length; i++)
	{
		if(laList[i]==liAdd)
		{
			bFound = true;
			break;
		} //laList[i]==liAdd
		
	} //var i=0; i<laList.length; i++
	
	if(!bFound)
	{
		laList[laList.length] = liAdd
	} //!bFound
	
	return bFound;
	
} //AddToList

function RemoveFromList(laList,liAdd)
{
	var laTempList = new Array();
	for(var x=0; x < laList.length; x++)
	{								
		if(laTempList[x] == null)								
		{
			laTempList[x] = new Array();
		} //laTempList[x] == null
		
	} //var x=0; x < laList.length; x++
			
	var i=0, y=0;

	for(i=0; i<laList[0].length; i++)
	{
		if(laList[0][i]==liAdd)
		{
			laList[0][i] = null;
			if(laList.length != 1)
			{	
				for (var x=1; x < laList.length; x++)
				{
					laList[x][i] = null;
				} //var x=1; x < laList.length; x++
				
			} //laList.length != 1
			
			break;
		}
		else //laList[0][i]==liAdd
		{
			for(var x=0; x < laList.length; x++)
			{
				laTempList[x][i]=laList[x][i];
			} //var x=0; x < laList.length; x++
			
		} //laList[0][i]==liAdd
		
	} //i=0; i<laList[0].length; i++

	for(var x=0; x < laList.length; x++ )
	{	
		for(y=i+1; y<laList[x].length; y++)
		{
			laTempList[x][y-1]=laList[x][y];						
		} //y=i+1; y<laList[x].length; y++
		
	} //var x=0; x < laList.length; x++ 
	
	return laTempList;
} //RemoveFromList

function ShowPopUnder()
{
    ShowPopUnder2("MyOffers", "131");
}

function ShowPopUnder2(lsSite, liZID)
{
	var expirydate = new Date();
	expirydate.setTime(expirydate.getTime()+(60*60*24*1000));
	
	if(location.href.indexOf("nopopup=1") > -1)
	{
		document.cookie=lsSite+"lsSitePopUnderShown=1; expires="+expirydate.toGMTString()+"; path=/";
	}
	else if(GetCookie(lsSite+"PopUnderShown") == null)
	{
		document.cookie=lsSite+"PopUnderShown=1; expires="+expirydate.toGMTString()+"; path=/";
		
		var bust = Math.floor(89999999*Math.random()+10000000);
		var millis = new Date().getTime();
		var loPopUnderWindow, loPopUnderDocument
		
		loPopUnderWindow = window.open("", "popunder", "height=330,width=740,left=0,top=0,location=no,menubar=no,toolbar=no,status=no")
		loPopUnderDocument = loPopUnderWindow.document
		window.focus()
		
		loPopUnderDocument.writeln('<IFRAME src="http://ipt.advertserve.com/advertpro/servlet/view/banner/html/zone?zid='+liZID+'&pid=0&random='+bust+'&millis='+millis+'" height="300" width="720" hspace="0" vspace="0" frameborder="0" marginwidth="0" marginheight="0" scrolling="no">');
		loPopUnderDocument.writeln('<A href="http://ipt.advertserve.com/advertpro/servlet/click/zone?zid='+liZID+'&pid=0&lookup=true&random='+bust+'&millis='+millis+'" target="_top">');
		loPopUnderDocument.writeln('<IMG src="http://ipt.advertserve.com/advertpro/servlet/view/banner/image/zone?zid='+liZID+'&pid=0&random='+bust+'&millis='+millis+'" height="300" width="720" hspace="0" vspace="0" border="0" alt="Click Here!">');
		loPopUnderDocument.writeln('</A>');
		loPopUnderDocument.writeln('</IFRAME>');
	}
}

function ExitPopUnder(url, title, width, height, toolbar, titlebar, status)
{
	//alert('open ' + url);
	window.open(url, title, 'width=' + width + ',height=' + height + ',toolbar=' + toolbar + ',titlebar=' + titlebar + ',status=' + status + '');
	window.focus();
}

function CheckExitPopUnder(url)
{
	if(window.opener != null)
	{
		try
		{
			//alert(window.opener.location);
			
			if (window.opener.location.toString().indexOf(url) >= 0)
			{
				//alert('Still On Site');
				self.close();
			}
			else
			{
				//alert('Left Site');
			}
		}
		catch(e)
		{
			//alert('Left Site');
		}
	}
	else
	{
		//alert('No Source Site');
	}
}

/*
####################################################
########### Start Web Brands Entry Code#############
####################################################
*/
function selectAllWebbrandsImageClk(oElem)
{	
	oElem.checked = !oElem.checked;
	selectAllWebbrands(oElem);
}


function selectAllWebbrands(oElem){
		
	var laWebbrandsCheckboxes = document.getElementById('selectAll').getElementsByTagName('input');
	for(var i=0;i<laWebbrandsCheckboxes.length;i++)
	{
		
		if(!laWebbrandsCheckboxes[i].checked)
		{
			if (laWebbrandsCheckboxes[i].id =="Webbrands116108")
			{
				laWebbrandsCheckboxes[i].checked=false;
			}
			else
			{
				WebrandsPrizeEntryCount(laWebbrandsCheckboxes[i].name);
				laWebbrandsCheckboxes[i].checked = true;	
			}
		}
		else
		{	
			laWebbrandsCheckboxes[i].checked = false;
		}		
	}
	
	if(!oElem.checked)
	{
		for(var i=0;i<gaQuestionList.length;i++)
			gaQuestionList[i]=null;
			
		document.getElementById("PrizeDrawEntryCount").value=1;
	}
	
	if(document.getElementById("SubmitQuestionnaire") != null && oElem.checked)
	    document.getElementById("SubmitQuestionnaire").focus();
	
}

var gaQuestionList = new Array()

function WebrandsPrizeEntryCount(liQuestionId)
{		
	var loPrizeDrawEntryCount = document.getElementById("PrizeDrawEntryCount");
	var lbFoundQuestionId = false;
	var i = 0, liCheckedQuestionCount = 0;
	
	
	for(i=0; i<gaQuestionList.length; i++)
	{
		if(gaQuestionList[i]==liQuestionId)
		{
			lbFoundQuestionId = true;
			break;
		}
	}
	//javascript:alert(document.getElementById('selectAll').getElementsByTagName('input')[1].name.toString())
	if(!lbFoundQuestionId)
		gaQuestionList[gaQuestionList.length] = liQuestionId;
	else
		gaQuestionList[i] = null;
		
	for(var x=0;x<gaQuestionList.length;x++)
	{
		if(gaQuestionList[x]!=null)
			liCheckedQuestionCount += 1;
	}
	
	loPrizeDrawEntryCount.value= liCheckedQuestionCount+1;
}

function WebbrandsToggle(lsID)
{
	loCheckBox = document.getElementById(lsID)
	
	if(loCheckBox)
	{
		loCheckBox.checked = !loCheckBox.checked;					
	}
}

/*
####################################################
########### End Web Brands Entry Code###############
####################################################
*/

// Functions for hiding and showing client info ("who we are" and "what we do") on questionnaire
function AutoHide(hide1,hide2) {
	hide1.style.display="none";
	hide2.style.display="none";
}

function ManualHideShow(vhide, vlink){
    if (!document.getElementById)
        return;
    if (vhide.style.display=="block")
	    vhide.style.display="none";
    else
	    vhide.style.display="block";
	vlink.style.textDecoration="none";
}

// Functions for hiding and showing client info ("who we are" and "what we do") on questionnaire
function HideAndShow(hide,show) {
	hide.style.display="none";
	show.style.display="block";
}


/**************************************************************************************************

Automated Questionnaire Hide Show Library

**************************************************************************************************/

//Hide question and reset value
function hideQElement(sElement)
{
    var fset = document.getElementById("fd" + sElement);
    var quest = document.getElementById("Q" + sElement);
    var passcon = document.getElementById("Q" + sElement + 'Confirm');
    var collection = document.getElementsByName("Q" + sElement);
    fset.style.display = "none";
    
    if(quest==null)
        quest = collection[0];
    
    try
    {
        if(quest.type == "text")    
        {
            quest.value="";
        }
        else if(quest.type == "password")    
        {
            quest.value="";
            passcon.value="";
        }
        else if(quest.type == "select-one")
        {
           quest[0].selected = true;
        }
        else if(quest.type == "select-multiple")
        {
            for(var i=0; i<quest.length; i++)
            {
                quest[i].selected = false;
            }
        }
        else if(quest.type == "checkbox" || quest.type == "radio")
        {
           for(var i=0; i<collection.length; i++)
            {
                collection[i].checked = false;
            }
        }
    }
    catch(e)
    {
    
    }
}

/**************************************************************************************************/

//Show question
function showQElement(sElement)
{
    var fset = document.getElementById("fd" + sElement);
    fset.style.display = "block";
}

/**************************************************************************************************/

//Compare question value
function compareQValue(sElement, sCompareValue)
{
    var collection = document.getElementsByName("Q" + sElement);
    var quest = document.getElementById("Q" + sElement);
    var comparison = false;
    
    if(quest==null)
        quest = collection[0];
        
    try
    {
        if(quest.type == "text")    
        {
            comparison = (quest.value == sCompareValue);
        }
        else if(quest.type == "select-one" || quest.type == "select-multiple")
        {
            for(var i=0; i<quest.length; i++)
            {
                if(quest[i].value == sCompareValue && quest[i].selected)
                    comparison = true;
            }
        }
        else if(sCompareValue == "" &&  quest.type == "radio")
        {
             comparison = true;
            
            for(var i=0; i<collection.length; i++)
            {
                if(collection[i].checked)
                    comparison = false;
            }
        }
        else if(quest.type == "checkbox" || quest.type == "radio")
        {
           for(var i=0; i<collection.length; i++)
            {
                if(collection[i].value == sCompareValue && collection[i].checked)
                    comparison = true;
            }
        }
    }
    catch(e)
    {
    
    }
    
    return comparison;
}

/**************************************************************************************************/

//Uncheck all check boxes if last value is non applicable (i.e. all but last one)
function checkedQNA(oElement)
{
    var collection=document.getElementsByName(oElement.name); 
    
    for(var i = 0; i < collection.length-1; i++) 
    { 
        collection[i].checked = false; 
    }
}
/**************************************************************************************************/
//Uncheck non applicable checkbox (i.e. last one)
function checkedQ(oElement)
{
    var collection=document.getElementsByName(oElement.name); 
    collection[collection.length-1].checked = false; 
}
/**************************************************************************************************/
function CheckFreePrizeEntryCount()
{
	if(document.getElementById('totalFreePrizeCount').value==giFreePrizeDrawCount)
	{
		return true;
	}
	else
	{
	    if(giFreePrizeDrawCount == 1)
	    {
		    void(alert("Veuillez sélectionner votre participation gratuite"))
		}
		else if(giFreePrizeDrawCount == 2)
		{
		    void(alert("Veuillez sélectionner vos deux participations gratuites"))
		}
		else if(giFreePrizeDrawCount == 2)
		{
		    void(alert("Veuillez sélectionner vos trois participations gratuites"))
		}
		else 
		{
		    void(alert("Veuillez sélectionner vos" + giFreePrizeDrawCount + " participations gratuites"))
		}
		
		return false;
	}
	
	
	return true;
}