function listClass() {
}

listClass.prototype.sort = function(sortby) {
    document.listform.sort.value = sortby;
    if (document.listform.sort.value == document.listform.previoussort.value) {
        if (document.listform.sorttype.value == 'ASC') {
            document.listform.sorttype.value = 'DESC';
        } else {
            document.listform.sorttype.value = 'ASC';
        }
    } else {
        document.listform.sorttype.value = 'ASC';
    }
    document.listform.submit();
}

var resultsPagesPerView = 10;
listClass.prototype.paging = function(resultsCount, rowsPerPage, pageIndex, viewIndex) {
    if (rowsPerPage == '') { rowsPerPage = 20 }
    var resultsPages = Math.ceil(resultsCount / rowsPerPage);
    var viewStart;
    var viewEnd;
    if (viewIndex == '') { viewIndex = 0 }
    if (rowsPerPage == '') { rowsPerPage = 20 }
    if (pageIndex == '') { pageIndex = 0 }
    if (pageIndex % resultsPagesPerView == 0) {
        viewStart = Math.ceil(pageIndex);

        document.listform.viewstart.value = viewStart;

    } else {
        viewStart = Math.ceil(viewIndex);
    }

    if (viewStart + resultsPagesPerView <= resultsPages) {
        viewEnd = viewStart + resultsPagesPerView;
    } else {
        viewEnd = resultsPages;
    }
    if (pageIndex != 0) {
        var tempViewStart = viewStart;
        if (Math.abs(viewStart) == Math.abs(pageIndex)) { tempViewStart = Math.abs(viewStart) - resultsPagesPerView }
        document.write('<a href="javascript:document.listform.viewindex.value=' + (Math.abs(tempViewStart)) + ';document.listform.pageindex.value=' + (Math.abs(pageIndex) - 1) + ';document.listform.submit();">&lt; Forrige</a>');
    } else {
        //document.write('&#160;<a href="#" class="active">&lt; Forrige</a');
    }
    var pageNumber = viewStart + 1;
    for (i = viewStart; i < viewEnd; i++) {
        if (pageNumber > 1 || (pageIndex != 0)) {
            document.write('&#160;');
        }

        if (i == pageIndex) {
            document.write('<a href="#" class="active">' + pageNumber + '</a>');
        } else {
            document.write('<a href="javascript:document.listform.viewindex.value=' + (Math.abs(viewStart)) + ';document.listform.pageindex.value=' + i + ';document.listform.submit();">' + pageNumber + '</a>');
        }

        if (Math.abs(pageNumber) != Math.abs(viewEnd)) {
            document.write('&#160;|');

        }
        pageNumber++;


    }
    if (Math.abs(pageIndex) + 1 < Math.abs(resultsPages)) {
        if (Math.abs(pageIndex) == Math.abs(viewEnd) - 1) {
            document.write('&#160;<a href="javascript:document.listform.viewindex.value=' + (Math.abs(viewEnd)) + ';document.listform.pageindex.value=' + (Math.abs(pageIndex) + 1) + ';document.listform.submit();">Næste &gt;</a>');
        } else {
            document.write('&#160;<a href="javascript:document.listform.viewindex.value=' + (Math.abs(viewIndex)) + ';document.listform.pageindex.value=' + (Math.abs(pageIndex) + 1) + ';document.listform.submit();">Næste &gt;</a>');
        }
    } else {
        //document.write('&#160;<a href="#" class="active">Næste &gt;</a>');
    }
}

function ShowTema(sel) {

    var area = sel.options[sel.selectedIndex].value;
    if (area == '{E3AF77D9-C02A-4345-8068-4D80D132004A}' || area == '{017BAA31-2DFB-4E2B-8AB6-745BDDABCB94}' || area == '{0CFFB076-EB49-48AF-AA25-4EFE6263EFBC}' || area == '{33AC9AC8-1096-4E77-B66C-C906F1B15587}') {
        $('#tema').show();
    }
    else {
        $('#tema').hide();
    }
}



function UpdateSearchResult() {

var areaofbusinessguid = $('#areaofbusinessguid').val();
var temaguid = $('#temaguid').val();	
var marketguid = $('#marketguid').val();
var targetgroupguid = $('#targetgroupguid').val();
var priceguid = $('#priceguid').val();
var periodguid = $('#periodguid').val();

var params = '';
if (areaofbusinessguid != '') params += '\nareaofbusinessguid=' + areaofbusinessguid;
if (temaguid != '') params += '\n&temaguid=' + temaguid;
if (marketguid != '') params += '\n&marketguid=' + marketguid;
if (targetgroupguid != '') params += '\n&targetgroupguid=' + targetgroupguid;
if (priceguid != '') params += '\n&priceguid=' + priceguid;
if (periodguid != '') params += '\n&periodguid=' + periodguid;



 	$("#sCount").load("/composite-667.htm?"+params.substr(1,params.length).replace('\n','') +"  #results");

	$('#allresults').hide();
	$('#sCount').show();
	
	if (reCount() == 0) {
		$('#sCount').text()='0 RESULTATER';
	}

}



function PrintPage(url,params){
	var PrintWindow = window.open(url + '?TemplateId=28' + params, 'PrintWindow','width=535,height=450,location=0,menubar=0,scrollbars=0,status=0,toolbar=0,resizable=1');
	
}

function getCurrentDocFilename() {
    var str = location.href;
    var number_1 = str.lastIndexOf('/') + 1
    var myVar = str.substring(number_1)
    return myVar
}

$(document).ready(function() {

    	FavoritList();
    
	$('#allresults').show();
	//$('#sCount').hide();


});

function IsCookiesEnabled() {
   $.cookie('cookietest', 'cookietest', { path: '/', expires: getDate() })
    var cookietest= $.cookie('cookietest');

   if (cookietest==null) {
   	return false;
   }
   return true;
	
}


function FavoritList() {
	
   var FavoriteCampaignCustomer = $.cookie('customerguid');
   var CampaignText="KAMPAGNE(R)";
   
   if ($('#counterBasket').text() == 1) { CampaignText="KAMPAGNE"; }
   else  { CampaignText="KAMPAGNER"; }

   
    
   if (FavoriteCampaignCustomer) {
    	$('#favorites').html(' ' +  CampaignText + ' I <a href="/Kampagner2010?Favorites=' + FavoriteCampaignCustomer + '">FAVORITLISTE</a>');
   }
   
   
  if (!FavoriteCampaignCustomer) {
	  var url= getCurrentDocFilename();
	  var startStr = url.indexOf('?');
	  var IsSendFromFriend = url.substr(startStr+1, 9)
      
	   if (IsSendFromFriend == 'Favorites') {
	   	$('#counterBasket').hide();
	        $('#favorites').text('FAVORIT ' + CampaignText + ' SENDT TIL DIG');
	   }
   }
   

       if (reCount() == 0) {
	   $('#favorites').show();
           $('#favorites').text(' ' + CampaignText + ' I FAVORITLISTE');
       }
}

function generateGuid() {
    var result, i, j;
    result = '';
    for (j = 0; j < 32; j++) {
        if (j == 8 || j == 12 || j == 16 || j == 20)
            result = result + '-';
        i = Math.floor(Math.random() * 16).toString(16).toUpperCase();
        result = result + i;
    }
    return result
}


function addToFavorite(id, thetext, showshorttext, alsoExistsInOfferList, doc_id) {
 if (IsCookiesEnabled()) {
    var Favorite = $.cookie('FavoriteCampaign');
    var displaytext = '» ';
    var displayshorttext = '» ';

    

    // Update cart
    if (!verifyFavoriteContent(id)) {

        var content = Favorite ? Favorite + ';' + id : id;
        $.cookie('FavoriteCampaign', content, { path: '/', expires: getDate() })

        displayshorttext += 'Slet';
        displaytext += 'Slet fra favoritliste';
    }
    else {

        deleteFromFavorite(id);
        displayshorttext += 'Tilføj';
        displaytext += 'Føj til favoritliste';
    }

    
    //Update customer
    VerfifyCustomer();
    
    $.post("/composite-665.htm" );
    
    //Display values to user
    var c = reCount();

    $('#counterBasket').text(c);

    if (alsoExistsInOfferList) {
        if (showshorttext) { $('#' + thetext).text(displayshorttext); } else { $('#' + thetext).text(displaytext); }

        $('#id' + id).text(displaytext);
    }
    else {

        $('#' + thetext).text(displaytext);
        //if ($('#offerid' + id)) $('#offerid' + id).text(displayshorttext);
        if ($('#offerid' + id)) $('#offerid' + id).text(displaytext);
    }


    var i = 0;
    while (i < 3) {
        i++;
        $('#counterBasket').fadeOut('fast');
        $('#counterBasket').fadeIn('fast');
        $('#' + thetext).fadeOut('fast');
        $('#' + thetext).fadeIn('fast');
    }

    FavoritList();
    
    var getPage = getCurrentDocFilename().substr(10, 3);
   
    if (getPage  == '660' || getPage == '662' || doc_id=='660' || doc_id=='662') { document.location.href ='/composite-' +doc_id +'.htm' }
    
  }
   else {
  	$('#counterBasket').hide();
  	$('#favorites').html('<span style="color: red;">Indstil venligst din browser til at acceptere cookies!</span>');

  }
}



function RemoveFromList(id) {
    if (verifyFavoriteContent(id)) deleteFromFavorite(id);
}

function FavoriteCount() {
    var Favorite = $.cookie('FavoriteCampaign');

    if (Favorite) return Favorite.split(';').length;

    return 0;
}

function reCount() {
    return FavoriteCount();
}
function DisplayText(id) {

    var displaytext = '';

    if (verifyFavoriteContent(id)) {
        displaytext += 'Slet fra favoritliste';
    }
    else {
        displaytext += 'Føj til favoritliste';
    }

    return displaytext;
}

function VerfifyCustomer() {
    var FavoriteCampaignCustomer = $.cookie('customerguid');

    if (!FavoriteCampaignCustomer) {
        var CustomerGuid = generateGuid();
        $.cookie('customerguid', CustomerGuid, { path: '/', expires: getDate() })
    }
}

function DisplayTextShort(id) {

    var displaytext = '';

    if (verifyFavoriteContent(id)) {
        displaytext += 'Slet';
    }
    else {
        displaytext += 'Tilføj';
    }

    return displaytext;
}

function deleteFromFavorite(id) {


    var Favorite = $.cookie('FavoriteCampaign');
    var NewList = '';

    if (Favorite) {
        var CurrentList = Favorite.split(';');

        for (i = 0; i < CurrentList.length; i++) {
            if (CurrentList[i] != id) {
                NewList += NewList == '' ? '' : ';';
                NewList += CurrentList[i];
            }
        }
    }

    $.cookie('FavoriteCampaign', null, { path: '/' })
    $.cookie('FavoriteCampaign', NewList, { path: '/', expires: getDate() })

}

function verifyFavoriteContent(id) {

    var Favorite = $.cookie('FavoriteCampaign');

    if (Favorite) {
        var CurrentList = Favorite.split(';');
        for (i = 0; i < CurrentList.length; i++) if (CurrentList[i] == id) return true;
    }

    return false;
}



function getDate() {
    var date = new Date();
    date.setTime(date.getTime() + (3 * 24 * 60 * 60 * 1000));

    return date;
}
