$(document).ready (function()
{
	$("a[id='dl_link_archiv']").click (function ()
	{
		window.open (this.href, '_blank', 'scrollbars=yes, height=660, width=580');
		return false;
	});
	
	var iDura = 1;
	var iScId = 0;
	 	
	 	$("button[@name='btnShowForm']").click (function ()
	 	{
		  $.get("Controller", { action: "getOffer", actionClass: "lv.ais.proficouk.action.OffercodeAction",  code: $("input[name='so_offercode']")[0].value }, function(XML)
			{
				// IE + FF etc.
				var nodes = XML.firstChild.nextSibling ? XML.firstChild.nextSibling : XML.firstChild;
				// Offercode exists ?
				try {
					var bOK = (nodes.childNodes[0].text ? nodes.childNodes[0].text : nodes.childNodes[0].textContent).length != 0;
					//var bOK = (nodes.nodeValue != null ? nodes.childNodes[0].text : nodes.childNodes[0].textContent).length != 0;
				} catch (e) {
					var bOK = false;
				}
				if (bOK)
				{
					//var code = document.createTextNode (nodes.childNodes[0].text ? nodes.childNodes[0].text : nodes.childNodes[0].textContent);
					var offer = document.createTextNode (nodes.childNodes[1].text ? nodes.childNodes[1].text : nodes.childNodes[1].textContent);
					var sPic1 = nodes.childNodes[2].text ? nodes.childNodes[2].text : nodes.childNodes[2].textContent;
					var sPic2 = nodes.childNodes[3].text ? nodes.childNodes[3].text : nodes.childNodes[3].textContent;
					var pic1 = new Image ();
					pic1.src = 'images/merchandise/' + sPic1;
					var pic2 = new Image ();
					pic2.src = 'images/merchandise/' + sPic2;
					
					if (sPic1 || sPic2)
					{
						var sBonus = document.createElement ("span");
						var txtBonus = document.createTextNode ("Bonus:");
						sBonus.style.fontWeight = 'bold';
						sBonus.appendChild (txtBonus);	
						var divBonus = document.createElement("div");
						var pBonus1 = document.createElement ("p");
						var pBonus2 = document.createElement ("p");
						pBonus2.align = 'center';
						pBonus1.appendChild (sBonus);
						pBonus1.appendChild (offer);
						pBonus1.appendChild (document.createElement ("br"));
						if (sPic1)
							pBonus2.appendChild (pic1);
						if (sPic2)
							pBonus2.appendChild (pic2);
						divBonus.appendChild (pBonus1);
						divBonus.appendChild (pBonus2);
					}
				
					$("td[@id='cont_Viewer']")[0].innerHTML = '';
					$("td[@id='cont_Viewer']")[0].style.backgroundColor = '#FFFBCC';
					$("td[@id='cont_Viewer']")[0].appendChild (divBonus);
				}
				
				$("table[@id='to_view']").show();
			});
			return false;
	 	});
		
		// @obj: RADIOBTNS # @ret: STRING
		function getActive (obj) {
			try {
				for (var i = 0; i < obj.length; i++)
				{
					if(obj[i].checked)
						return obj[i].value;
				}
			} catch (e) {};
		}
		
		$("input[@name='Submit']").click (function ()
		{
			// vars
			var oSubType = $("input[@name='subscription_type']");
			var oDura = $("input[@name='sc_duration']");
			var oDest = $("input[@name='sc_id']");
			var sPrice = 0;
			var iIssues = 0;
			
			if (getActive(oSubType) == 'trial')
			{
				iIssues = 3;
				switch (parseInt (getActive (oDest)))
				{
					case 1: sPrice = '10,00 GBP'; break;
					case 2: sPrice = '15,00 EUR'; break;
					case 4: sPrice = '19,99 USD'; break;
					default:
						sPrice = '#ERROR#';
						iIssues = 0;
						break; 
				}
			} else {
				switch (parseInt (getActive(oDura)))
				{
					case 1:
						iIssues = 11;
						switch (parseInt (getActive (oDest)))
						{
							case 1: sPrice = '51,50 GBP'; break;
							case 2: sPrice = '75,00 EUR'; break;
							case 3: sPrice = '97,00 USD'; break;
							case 4: sPrice = '78,00 USD'; break;
							default: sPrice = '#ERROR#'; iIssues = 0;	break; 
						}
						break;
					case 2:
						iIssues = 22;
						switch (parseInt (getActive (oDest)))
						{
							case 1: sPrice = '92,50 GBP'; break;
							case 2: sPrice = '135,00 EUR'; break;
							case 3: sPrice = '174,00 USD'; break;
							case 4: sPrice = '140,00 USD'; break;
							default: sPrice = '#ERROR#'; iIssues = 0;	break; 
						}
						break;
					case 3:
						iIssues = 33;
						switch (parseInt (getActive (oDest)))
						{
							case 1: sPrice = '123,50 GBP'; break;
							case 2: sPrice = '180,00 EUR'; break;
							case 3: sPrice = '232,00 USD'; break;
							case 4: sPrice = '187,00 USD'; break;
							default: sPrice = '#ERROR#'; iIssues = 0;	break; 
						}
						break;
				}
			}
			
			// Container
			var pic3 = new Image();
			pic3.src = 'magazine/images_mag/pi_cover.jpg';
			var divTextPay = document.createElement ('div');
			var textPay = document.createTextNode ('For the fully tax deductible investment of ' + sPrice + ' you\'ll get ' + iIssues + ' issues of profi Tractors and Farm Machinery');
			var pTextPay = document.createElement ('p');
			pTextPay.appendChild (textPay);
			divTextPay.setAttribute ('class', 'JBoxCont'); // FF
			divTextPay.setAttribute ('className', 'JBoxCont'); // IE
			divTextPay.appendChild (pTextPay);
			divTextPay.appendChild (pic3);
			var btnGoAway = document.createElement("button");
			btnGoAway.innerHTML = 'OK';
			btnGoAway.name = 'btnNext';
			btnGoAway.id = 'btnNext';
			
			var cont = $("div[@id='contViewer']")[0];
			cont.innerHTML = ''; 
			cont.appendChild (divTextPay);
			cont.appendChild (document.createElement("hr"));
			cont.appendChild (btnGoAway);
				
			// Box anzeigen
			jBox.open('jBoxer','div','contViewer','Information','center=true,minimizable=false,resize=false,draggable=false,width=435,height=250,model=true');
				
			// 
			$("button[@id='btnNext']").click (function ()
			{
				document.forms['offer'].submit();
			});
			return false;
		});
	   
	   $("input[@name='sc_duration']").click (function () {
	   	try {
	   	 iDura = this.value;
	   	 document.getElementsByName ('sc_id')[iScId].click();
	   	} catch (e) {};
	   });
	   
	   $("input[@name='sc_id']").click (function () {
	   	try {
		   	iScId = this.value - 1;
		   	if (iDura < 0)
		   		document.getElementsByName('sc_duration')[0].click();
		   
		   	 var field;
		   	 
		   	 switch (parseInt(this.value))
		   	 {
		   	 	case 1: field = MD5('UK') + '_' + iDura; break;
		   	 	case 2: field = MD5('Ireland & Europe') + '_' + iDura; break;
		   	 	case 3: field = MD5('Rest of World by Air mail') + '_' + iDura; break;
		   	 	case 4: field = MD5('Rest of World by Surface mail') + '_' + iDura; break;
		   	 }
		  
		  if (!$("input[@name='subscription_type']").checked)
		  {	 
				var table = document.getElementById('pricelist');
				node_index = table.childNodes.length - 1;
				for (i = 0; i < table.childNodes[node_index].rows.length; i++)
				{
					if (table.childNodes[node_index].rows[i].cells[1].title == '__ident')
					{
						table.childNodes[node_index].rows[i].cells[1].style.textDecoration = 'none';
						table.childNodes[node_index].rows[i].cells[2].style.textDecoration = 'none';
						table.childNodes[node_index].rows[i].cells[1].style.fontWeight = 'normal';
						table.childNodes[node_index].rows[i].cells[2].style.fontWeight = 'normal';
						table.childNodes[node_index].rows[i].cells[1].style.fontSize = 'inherit';
						table.childNodes[node_index].rows[i].cells[2].style.fontSize = 'inherit';
						table.childNodes[node_index].rows[i].cells[1].style.color = 'inherit';
						table.childNodes[node_index].rows[i].cells[2].style.color = 'inherit';
					}
				}
			}
		   	 
		   	 var obj1 = document.getElementById(field + '_1');
		   	 var obj2 = document.getElementById(field + '_2');
		   	 
		   	 obj1.style.textDecoration = 'underline';
		   	 obj1.style.fontWeight = 'bold';
		   	 obj1.style.color = '#AA0000';
		   	 obj1.style.fontSize = '15px';
		   	 obj2.style.textDecoration = 'underline';
		   	 obj2.style.fontWeight = 'bold';
		   	 obj2.style.color = '#AA0000';
		   	 obj2.style.fontSize = '15px';
		   	 //alert (field);
		  } catch (e) {}
	   });
	   $("a[@title='newsletter']").click (function ()
	   		{
						// Box anzeigen
									jBox.open('jBoxer', 'div', 'contNewsletter', 'Newsletter', 'center=true,minimizable=false,resize=false,draggable=false,width=340,height=120,model=true');
											
														return false;
																});
});
