function matchingButton(kindOfButton, hover){

	if(kindOfButton == 2){
	
		bt1 = document.getElementById("mpg_bt1");
		bt2 = document.getElementById("mpg_bt2");

		if(hover){
			bt1.className = "mpg_bt1 mpg_bt1_hover";
			bt2.className = "mpg_bt2 mpg_bt2_hover";
			return;
		}

		bt1.className = "mpg_bt1";
		bt2.className = "mpg_bt2";
		return;
	}
	if(kindOfButton == 3){
		bt7 = document.getElementById("mpg_bt7");
		if(hover){
			bt7.className = "mpg_bt2 mpg_bt2_hover";
			return;
		}
		bt7.className = "mpg_bt2";
		return;
	}
	bt3 = document.getElementById("mpg_bt3");
	bt4 = document.getElementById("mpg_bt4");
	bt5 = document.getElementById("mpg_bt5");

	if(hover){
		bt3.className = "mpg_bt3 mpg_bt3_hover";
		bt4.className = "mpg_bt4 mpg_bt4_hover";
		bt5.className = "mpg_bt5 mpg_bt5_hover";
		return;
	}

	bt3.className = "mpg_bt3";
	bt4.className = "mpg_bt4";
	bt5.className = "mpg_bt5";
	return;

}

function matchingButtonEvents(activeYes, activeNo, reset, activeMaybe){

	var bt1 = document.getElementById("mpg_bt1");
	var bt2 = document.getElementById("mpg_bt2");
	var bt3 = document.getElementById("mpg_bt3");
	var bt4 = document.getElementById("mpg_bt4");
	var bt5 = document.getElementById("mpg_bt5");
	var bt7 = document.getElementById("mpg_bt7");


	if(reset){
	
		bt1.onmouseover		= null;
		bt2.onmouseover		= null;
		bt3.onmouseover		= null;
		bt4.onmouseover		= null;
		bt5.onmouseover		= null;
		bt7.onmouseover		= null;


		bt1.onmouseout		= null;
		bt2.onmouseout		= null;
		bt3.onmouseout		= null;
		bt4.onmouseout		= null;
		bt5.onmouseout		= null;
		bt7.onmouseout		= null;
	}

	bt1.onclick		= function(){matchingSystemSubmit(2)};
	bt2.onclick		= function(){matchingSystemSubmit(2)};
	bt3.onclick		= function(){matchingSystemSubmit(1)};
	bt4.onclick		= function(){matchingSystemSubmit(1)};
	bt5.onclick		= function(){matchingSystemSubmit(1)};
	bt7.onclick		= function(){matchingSystemSubmit(3)};


	if(activeNo || (!activeYes && !activeNo) ){

		bt1.onmouseover = function(){matchingButton(2, true)};
		bt1.onmouseout	= function(){matchingButton(2, false)};

		bt2.onmouseover = function(){matchingButton(2, true)};
		bt2.onmouseout	= function(){matchingButton(2, false)};
	}
	if(!activeMaybe){
		bt7.onmouseover = function(){matchingButton(3, true)};
		bt7.onmouseout	= function(){matchingButton(3, false)};
	}

	if(activeYes || (!activeYes && !activeNo)){

		bt3.onmouseover = function(){matchingButton(false, true)};
		bt3.onmouseout	= function(){matchingButton(false, false)};

		bt4.onmouseover = function(){matchingButton(false, true)};
		bt4.onmouseout	= function(){matchingButton(false, false)};

		bt5.onmouseover = function(){matchingButton(false, true)};
		bt5.onmouseout	= function(){matchingButton(false, false)};

	}



	document.getElementById("automaticCheck").onclick	= function(){setAutomatic()};


	//document.onkeydown = matchKeyControlling;

}

function matchingSystemSubmit(yes){

	var automaticCheck = document.getElementById("automaticCheck");

	var btYes	= document.getElementById("ms_btYes");
	var btNo	= document.getElementById("ms_btNo");
	var btMaybe	= document.getElementById("ms_btMaybe");

	var preBtYes	= btYes.value;
	var preBtNo		= btNo.value;
	var preBtMaybe	= btMaybe.value;


	if(automaticCheck.disabled){
		if(yes == 2){
			addRegisterLayer('matchYes');
			return false;
		}
		if(yes == 3){
			addRegisterLayer('matchMaybe');
			return false;
		}
		return false;
	}

	if(yes == 2){
		btYes.value = "1";
		btNo.value	= "0";
		btMaybe.value = "0";
	}else if(yes == 3){
		btYes.value = "0";
		btMaybe.value = "1";
		btNo.value	= "0";
	}else{
		btYes.value = "0";
		btMaybe.value = "0";
		btNo.value	= "1";
	}
	

	var params = "?ms_matchingID="+document.getElementById("lastID").value+"&matchingSrvAddy="+document.getElementById("matchingSrvAddy").value+"&matchingSesIDB="+document.getElementById("matchingSesIDB").value+"&ms_btYesNo=";
	
	var changes = false;

	if(yes == 2 && preBtYes==0){
		params = params + "1";
		matchingButton(2,true);
		matchingButton(1,false);
		matchingButton(3,false);
		matchingButtonEvents(1,0,true,0);
		changes = true;
	}else if(yes == 1 && preBtNo==0){
		params = params + "0";
		matchingButton(2,false);
		matchingButton(1,true);
		matchingButton(3,false);
		matchingButtonEvents(0,1,true,0);
		changes = true;
	}else if(yes == 3 && preBtMaybe == 0){
		params = params + "2";
		matchingButton(2,false);
		matchingButton(1,false);
		matchingButton(3,true);
		matchingButtonEvents(0,0,true,1);
		changes = true;
	}


	if(changes){
		var scriptElem = document.createElement('script');
		scriptElem.setAttribute('language','javascript');
		scriptElem.setAttribute('type','text/javascript');
		scriptElem.setAttribute('charset','UTF-8');
		scriptElem.setAttribute("src","/js/ajax/matchingSystem.php"+params);
		document.getElementsByTagName('head')[0].appendChild(scriptElem);
	}

}

function setAutomatic(){
	var autoForwarding = document.getElementById("ms_btAutoForward");
	var automaticCheck = document.getElementById("automaticCheck");

	if(automaticCheck.disabled){
		return;
	}
	
	var preAutoForwarding = autoForwarding.value;


	if(automaticCheck.checked){
		autoForwarding.value = "1";
	}else{
		autoForwarding.value = "0";
	}

	if((preAutoForwarding == "0" && automaticCheck.checked) || preAutoForwarding == "1" && automaticCheck.checked==false){
		var lastID = document.getElementById("lastID").value;
		var scriptElem = document.createElement('script');
		scriptElem.setAttribute('language','javascript');
		scriptElem.setAttribute('type','text/javascript');
		scriptElem.setAttribute('charset','UTF-8');
		scriptElem.setAttribute('src',"/js/ajax/matchingSystemJs.php?lastID="+lastID+"&checked="+autoForwarding.value+"&matchingSrvAddy="+document.getElementById("matchingSrvAddy").value+"&matchingSesIDB="+document.getElementById("matchingSesIDB").value);
		document.getElementsByTagName('head')[0].appendChild(scriptElem);
	}

}

function matchKeyControlling (event) {

	if(!event){
		event = window.event;
	}

	if(event.which){
		keycode = event.which;
	}else if(event.keyCode){
		keycode = event.keyCode;
	}

	if(keycode==74 || keycode==37){
		matchingButton(2, true);
		matchingButton(false, false);
		matchingSystemSubmit(true);
	}else if(keycode==78 || keycode==39){
		matchingButton(2, false);
		matchingButton(false, true);
		matchingSystemSubmit();
	}else if(keycode==65){
		var automaticCheckbox = document.getElementById("automaticCheck");

		if(automaticCheckbox.checked){
			automaticCheckbox.checked = false;
		}else{
			automaticCheckbox.checked = true;
		}
		
		setAutomatic();

	}

}



