function deleteFromCart(id, priceId)
{
	$('#tr'+id).hide();	
	$.ajax({
		type: "post",
		url: "index.php?action=deletefromcart",
		data: "id="+id,
		success: function(message){
		}
	});
	$('.cart .counter').text( Number($('.cart .counter:first').text())-1 );
	
	var total = Number($('#total').text()) - Number($('#'+priceId).text());
	$('#total').text(total);
}

function setLoginFieldName(name)
{
	$('#webshopLogin .name').text( 'Logget inn som '+name );
}

/* SELECT PARTICIPANTS FROM MODAL DIALOG */
var participantCount = 0;
function initDialog(dialogId, output_id, product_id)
{
	$(document).ready(function(){
		$('#'+dialogId).dialog
		(
			{
				autoOpen: false,
				width: 400,
				height: 200, 
				modal:true,
				buttons:{
			        "Legg til deltaker": function(){
			        	if(isDate($('#'+dialogId+' .participantBorn').val()))
			        	{
			        		addParticipant(-1, $('#'+dialogId+' .participantFirstname').val(), $('#'+dialogId+' .participantLastname').val(), $('#'+dialogId+' .participantBorn').val(), output_id, product_id);
			            	$(this).dialog("close");
			            	$('#'+dialogId+' .participantFirstname').val('');
			            	$('#'+dialogId+' .participantLastname').val('');
			            	$('#'+dialogId+' .participantBorn').val('');
			            }
			        }, 
			        "Avbryt": function(){
			            $(this).dialog("close"); 
		            	$('#'+dialogId+' .participantFirstname').val('');
		            	$('#'+dialogId+' .participantLastname').val('');
		            	$('#'+dialogId+' .participantBorn').val('');
			        }
			    }, 
			    overlay:{ 
			        opacity: 0.5, 
			        background: "white" 
			    } 
			}				
		);
	});
}
function selectParticipants(dialogId)
{
	$('#'+dialogId).dialog('open');
}
function addParticipant(participant_id, firstname, lastname, born, output_id, product_id)
{
	var html = $('#'+output_id).html();
	html += '<div>';
		//html += '<div style="width:412px;float:left;">';
		html += '<div>';
			if(product_id < 0 || product_id == undefined){
				html += '<input type="hidden" ';
				html += 'name="participant_'+participantCount+'" ';
				var dummyDate = "10-10-2000";
				html += 'value="'+participant_id+'::'+firstname+'::'+lastname+'::'+born+'::'+product_id+'"';
				html += '/>';
				html += '<div style="float:left;width:30%">'+firstname+'</div>';
				html += ' ';
				html += '<div style="float:left;width:30%">'+lastname+'</div>';
				html += ' ';
				html += '<div style="float:left;width:30%">'+born+'</div>';
				html += '<a href="javascript:void(0)" onclick="deleteParticipant('+participant_id+', this)" style="float:left;">Slett</a>';
				html += '<div class="clearBoth"></div>';
			}
			/*
			else{
				html += '<input type="checkbox" ';
				html += 'name="participant_'+participantCount+'" ';
				html += 'style="float:left;" ';
				//var dummyDate = "20-10-2000";
				html += 'value="'+participant_id+'::'+firstname+'::'+lastname+'::'+born+'::'+product_id+'" ';
				html += 'id="check'+participantCount+'"';
				html += '/>';
				html += '<label for="check'+participantCount+'" style="float:left;font-weight:normal;text-align:left;">';
				html += firstname;
				html += ' ';
				html += lastname;
				html += '<div class="clearBoth"></div>';
				//html += ' ';
				//html += born;
				html += '</label>';			
			}
			*/

		html += '</div>'
		html += '<div class="clearBoth"></div>';
	html += '</div>';
	$('#'+output_id).html( html );
	participantCount++;
}
function deleteParticipant(participant_id, a)
{
	if(confirm("Er du sikker på at du vil slette denne deltakeren fra systemet?"))
	{
		$(a).parent().html('');
		if(participant_id != -1)
		{
			$.ajax({
				type: "post",
				url: "index.php?action=deleteparticipant",
				data: "participant_id="+participant_id,
				success: function(message){
				}
			});
		}
	}
}




function displayTotals(prodTotalId,add,doAdd)
{
	if(!doAdd){
		add = -add;
	}
	var prodTotal = Number($('#'+prodTotalId).text()) + Number(add);
	$('#'+prodTotalId).text(prodTotal);
	var total = Number($('#total').text()) + Number(add);
	$('#total').text(total);
	
}






/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Følgende dato format må brukes: DD-MM-ÅÅÅÅ")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Vennligst skriv inn en gyldig måned")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Vennligst skriv inn en gyldig dag")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Vennligst skriv inn et gyldig år")
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Vennligst skriv inn en gyldig dato")
		return false
	}
	return true
}









