
function openNewWindow(theURL,winName,features) {
  
  window.open(theURL,winName,features); 
  
}

function selectTab (tab) {
 
	if(tabs!=false) tabs.select (tab);
  
  	return;
  
}

function selectMiniTab (tab) {
 
	if(tabs2!=false) tabs2.select (tab);
  
  	return;
  
}

Number.implement({
    format: function(kSep, floatsep, decimals){
        decimals = $pick(decimals, 2);
        floatsep = $pick(floatsep, '.');
        kSep = $pick(kSep, ' ');
 
        var parts = this.round(decimals).toString().split('.');
        var integer = parts[0];
        while (integer != (integer = integer.replace(/([0-9])(...($|[^0-9]))/, '$1' + kSep + '$2')));
        if (decimals == 0) return integer;
        return integer + floatsep + ((parts[1] || '') + '0000000000').substr(0, decimals);
    }
});

function calculatePrice() {
    var sum=0, travelers=0, standard=0, upgraded=0, deposit=0, arrival_cost=0, nPrices=7;
    
    // Rules for upgraded accomodation
    var upgr_sum=0;
    var upgr = $$ ('.upgraded').each (function (input) {
    	upgr_sum+=parseInt(input.get('value'));
    });
    
    
    // Calculate and update the total price
    for (var i=1; i<=nPrices; i++) {
    	inp = $$('.i'+i);
    	pr = $$('.p'+i);
    	op = $$('.op'+i);
    	
    	if (inp!=false && pr!=false) {
    		sum+= parseInt(inp.get('value'))*parseInt(pr.get('html'));
    		travelers+=parseInt(inp.get('value'));
    	} 
    }
    
    var single = $$('.upgraded_single').get('html');
    if (single=='yes') {
    	// Do nothing
    } else if (single=='no') {
    	if (upgr_sum>0 && upgr_sum<2) {
    		$$('.upgr_warning').setProperty('style', 'display:table-row;');
    	} else {
    		$$('.upgr_warning').setProperty('style', 'display:none;');
    	}
    } else if ((single - 0) == single && single.length > 0 && single>0) { // is a numeric value
    	if (upgr_sum>0 && upgr_sum<2) {
    		$$('.package_price_amount').set('html', sum.format('','.',2));
    		sum+=parseInt (single);
    		$$('.supplement_cost').setStyle('display', '');
    		$$('.package_price').setStyle('display', '');
    		
    	} else {
    		$$('.supplement_cost').setStyle('display', 'none');
    		$$('.package_price').setStyle('display', 'none');
    	}
    } 

    

    $$('.totaltravelers').set('html', travelers);					

    // Breakdown of total price
    arrival_cost = (sum*0.85).format('','.',2);
    deposit = (sum*0.15).format('','.',2);
    $$('.totalprice').each (function (e) {
    	if (e.hasClass('discounted')) {
    		deposit = (sum*0.10).format('','.',2);
    		$$('.discountamount').set('html', '-'+(sum*0.05).format('','.',2));
    	}
    });
    
    $$('.deposit').set('html', deposit);
    $$('.arrival_cost').set('html', arrival_cost);
    $$('.totalprice').set('html', sum.format('','.',2));
}

window.addEvent ('load', function () {
    calculatePrice();

    $$('.operator').each (function (el) {
    	el.addEvent ('click',function () {
    		var inp, pr, current, nPrices=7;
    	
    		// Retrieve the corresponding input box
    		for (var i=1; i<=nPrices; i++) {
    			if (el.hasClass ('op'+i)) {
    				inp = $$('.i'+i);
    			
    				// Update the input box depending on the packagetype
    				current = parseInt (inp.get('value'));
    				if (el.hasClass ('plus')) 					(el.hasClass('upgraded')) ? current+=1 : current++;
    				if (el.hasClass ('minus') && current!=0) 	(el.hasClass('upgraded')) ? current-=1 : current--;
    				inp.set ('value', current);
    				break;
    			}
    		}
    	
    		calculatePrice();
    	});
    });

});

function createContactPersons (el) {
    var pos=1;

    $each (el.getChildren(), function(e) {
    	e.dispose ();
    });
    
    new Element('option').set('html', 'Choose your contact person').inject(el);

    $$('.firstname').each (function (firstname) {
    	if (firstname.get('value')!="") {
    		var parent = firstname.getParent().getParent().getNext ('tr').getElements('input').each (function (e) {
    			if (e.hasClass ('familyname') && e.get('value')!="") {
    				var newOption = new Element ('option');
    				newOption.set('html', firstname.get('value') + " " + e.get('value'));
    				newOption.set('value', pos);
    				if (pos==$('selected_traveler').get('value')) newOption.set ('selected', 'selected');
    				newOption.inject (el);
    			}
    		});
    	}
    	pos++;
    	
    });

}

window.addEvent ('load', function () {
    
    $$('.leader_names').each (function (el) {
    	createContactPersons (el);
    	
    	$$('.firstname').each (function (e) {
    		e.addEvent ('blur',function () {
    			createContactPersons (el);
    		});			
    	});
    	
    	$$('.familyname').each (function (e) {
    		e.addEvent ('blur',function () {
    			createContactPersons (el);	
    		});			
    	});
    		
    });
    
	

});

