function getParameterByName(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
}

$(function() {
	var COOKIE_NAME = 'channel';
	var channels = [
		{'url':/^http(|s):\/\/(\w+\.)?twitter\.com/, 'channel': 'MSCTW'},
		{'url':/^http(|s):\/\/(\w+\.)?facebook\.com/, 'channel': 'MSCFB'},
		{'url':/^http(|s):\/\/(\w+\.)?google\.*/, 'channel': 'MSCGG'},
		{'url':/^http(|s):\/\/(\w+\.)?bing\.*/, 'channel': 'MSCBG'},
		{'url':/^http(|s):\/\/(\w+\.)?yahoo\.*/, 'channel': 'MSCYH'},
		{'url':/^http(|s):\/\/(\w+\.)?syndacast\.com/, 'channel': 'MSCSC'},
		{'url':/^http(|s):\/\/(\w+\.)?wego\.com/, 'channel': 'MSCWG'},
		{'url':/^http(|s):\/\/(\w+\.)?myspace\.com/, 'channel': 'MSCSM'},
		{'url':/^http(|s):\/\/(\w+\.)?linkedin\.com/, 'channel': 'MSCSM'},
		{'url':/^http(|s):\/\/(\w+\.)?digg\.com/, 'channel': 'MSCSM'},
		{'url':/^http(|s):\/\/(\w+\.)?delicious\.com/, 'channel': 'MSCSM'},
		{'url':/^http(|s):\/\/(\w+\.)?sphinn\.com/, 'channel': 'MSCSM'},
		{'url':/^http(|s):\/\/(\w+\.)?stumbleupon\.com/, 'channel': 'MSCSM'},
		{'url':/^http(|s):\/\/(\w+\.)?mixx\.com/, 'channel': 'MSCSM'},
		{'url':/^http(|s):\/\/(\w+\.)?reddit\.com/, 'channel': 'MSCSM'}
	];
	var channel = $.cookie(COOKIE_NAME);
	var referer = document.referrer.toLowerCase();
	
	// remember channel in 1 Hour
	var expire = new Date();
	expire.setTime(expire.getTime() + (1 * 60 * 60 * 1000)); 
	
	// check referer and replace if found in channels
	if(referer > '') {
		for(var i=0; i<channels.length; i++) {
			if(referer.match(channels[i].url)) {
				channel = channels[i].channel;
			}
		}
	}
	
	// pass channel by querystring
	var src = getParameterByName('src');
	if(src != "") {
		channel = src;
	}
	
	// if channel = null set default to MSC
	if(channel == null) {
		channel = 'MSC'; // default
	}
	$.cookie(COOKIE_NAME, channel, { path: '/', expires: expire });
});

function booking(frm) {
	var COOKIE_NAME = 'channel';
	if (frm.arrivee.value == '') {
		alert("Please select your arrival date");
		return false;
	}
	var arrMY = frm.arrivee.value.split('-');
	frm.annee_arrivee.value = arrMY[0];
	frm.mois_arrivee.value = arrMY[1];
	frm.jour_arrivee.value = arrMY[2];
	
	var channel = $.cookie(COOKIE_NAME);
	if(channel == null) {
		channel = 'MSC';
	}
	frm.sourceid.value = channel + '-' + frm.code_hotel.value;
	try {
		pageTracker._trackPageview("/go2accorhotels/");
	} catch(err) {}
	return true;
}