$(document).ready(function() {
	// Calls things after page has successfully loaded
});

/*
 *	Get Content from Server for cubus Extension
 */
function getContent(url,productionUid,mode) {
	$.ajax({
		type: 'POST',
		//add pageNum type
		url: url+"&type=999",
		data: ({productionUid: productionUid, cubusMode: mode}),
		//if success: 
		success: function(html) {
			//write content in inner div an fade it in
			//$("#innerContent").html(html); // jquery doesnt support scripts inside html-text!
			document.getElementById("innerContent").innerHTML = html;
			$("#innerContent").fadeIn();
			$("#closer").fadeIn();
			fleXenv.fleXcrollMain("scrollcontent");
			//location.hash = url;
			
			var element = document.getElementById('innerContent');
			// set the attribute on the DOM Element by hand - will update the innerHTML
			element.setAttribute('value', element.value);
			

			
			}
	});
}

/*
 *	Get Content from Server for cubus Extension
 */
function getContentAudio(url,productionUid,mode) {
	$.ajax({
		type: 'POST',
		//add pageNum type
		url: url+"&type=999",
		data: ({productionUid: productionUid, cubusMode: mode}),
		//if success: 
		success: function(html) {
			//write content in inner div an fade it in
			//$("#innerContent").html(html); // jquery doesnt support scripts inside html-text!
			document.getElementById("innerContent").innerHTML = html;
			$("#innerContent").fadeIn();
			$("#closer").fadeIn();
			fleXenv.fleXcrollMain("scrollcontent");
			
			var element = document.getElementById('innerContent');
			// set the attribute on the DOM Element by hand - will update the innerHTML
			element.setAttribute('value', element.value);
			
			//activate Scripts
			for(var i = 0, e = element.getElementsByTagName("script"); i < e.length; ++i) {
				eval(e[i].innerHTML);
			}
			//embed Players
			initAudioPlayers();
			
			//location.hash = url;
	}
	});
}

/*
 *	Get Content from Server for normal Pages
 */
function getNormalContent(url,noFade) {
	homeLink = url.match(/\bid=28\b/g);
	if (homeLink) {
		window.location.href="/";
		return;
	}
	
	$.ajax({
		type: 'POST',
		//add pageNum type
		url: url+"&type=998",
		//if success: 
		success: function(html) {
			if (!noFade) {
				//write content in inner div an fade it in
				document.getElementById("innerContent").innerHTML = html;
				$("#innerContent").fadeIn();
				$("#closer").fadeIn();
				fleXenv.fleXcrollMain("scrollcontent");
			} else {
				document.getElementById("innerContent").innerHTML = html;
				fleXenv.fleXcrollMain("scrollcontent");
			}
			//location.hash = url;
		}
	});
}

function closeInnerContent() {
	$("#innerContent").fadeOut();
	$("#closer").fadeOut();
}

/**
 * SHOP
 */
function checkPayment() {
	var ch = 41;
	var country;
	
	country = document.getElementById("shopAddressCountry").value;
	if (country != 41) { // nur Kreditkarte ausserhalb
		document.getElementById("shopAddressPaymenttype").options[1].selected = true;
		document.getElementById("shopAddressPaymenttype").options[2].disabled=true;
	} else {
		document.getElementById("shopAddressPaymenttype").options[2].disabled=false;
	}
}

function fadeOutBasketMessages() {
	$('#basketmessages').fadeOut(1000);
}

function add2CubusShop(productionUid) {
	$.ajax({
		type: 'POST',
		//add pageNum type
		url: "index.php?id=shopajax&type=999",
		data: ({productionUid: productionUid, shopmode: "add2Basket"}),
		//if success: 
		success: function(response) {
			$("#basketmessages").fadeIn(1000);
			setTimeout ( 'fadeOutBasketMessages()', 1500 );
		}
	});
}

function updateBasketQty(prodUid) {
	var quantity = document.getElementById("shopcartItemQty_"+prodUid).value;
//	alert("Prod: "+prodUid + ", Qty: "+quantity);
	getNormalContent("index.php?cubusshop&shopmode=update&prodUid="+prodUid+"&qty="+quantity,true);
}


function headToThankYouBillingPage() {
	getNormalContent("index.php?cubusshop&shopmode=thankYouBilling");
}

function headToThankYouCreditCardPage() {
	getNormalContent("index.php?cubusshop&shopmode=thankYouCreditcard");
}

function headToCancelledPayPalPage() {
	getNormalContent("index.php?cubusshop&shopmode=payPalCancelled");
}

function updateShopAddress(e) {
	enableCheckoutButtons();
	
	$.ajax({
		type: 'POST',
		//add pageNum type
		url: "index.php?id=shopajax&type=999",
		data: ({fieldname: e.name, fieldvalue: e.value, shopmode: "updateShopAddress"}),
		//if success: 
		success: function(response) {
			return true;
		}
	});
}

function changeCheckoutLayer(e) {
	var checkoutHtml = "";
	
	enableCheckoutButtons();
	
	if (e.value == 0) { // Creditcard
		checkoutHtml = $('#checkout_kk').html();
	} else {
		checkoutHtml = $('#checkout_billing').html();
	} 
	$('#cubusShopCheckout').html(checkoutHtml);
	$('#cubusShopCheckout').fadeIn();
}

/**
 * Generel Check of Addresdata
 * @return
 */
function enableCheckoutButtons() {
	b_set2True = true;
	if (!$('#shopAGBRead').is(':checked')) {
		b_set2True = false;
	}
	if ($('#shopAddressPaymenttype').val() == -1) {
		b_set2True = false;
		$('#shopAddressPaymenttype').css({'border': '1px dotted #CBD420','background-color': '#D8DE54','color': 'black'});
	} else {
		$('#shopAddressPaymenttype').css({'border': '1px dotted #e4e4e4','background-color': '#277596','color': 'white'});
	}
	
	company = $('#shopAddressCompany').val();
	name = $('#shopAddressName').val();
	firstname = $('#shopAddressFirstname').val();
	street = $('#shopAddressStreet').val();
	zip = $('#shopAddressZip').val();
	city = $('#shopAddressCity').val();
	email = $('#shopAddressEmail').val();
	
	if ( name.length < 3) {
		b_set2True = false;
		$('#shopAddressName').css({'border': '1px dotted #CBD420','background-color': '#D8DE54','color': 'black'});
	} else {
		$('#pp_last_name').val(name);
		$('#shopAddressName').css({'border': '1px dotted #e4e4e4','background-color': '#277596','color': 'white'});
	}
	
	if ( firstname.length < 3) {
		b_set2True = false;
		$('#shopAddressFirstname').css({'border': '1px dotted #CBD420','background-color': '#D8DE54','color': 'black'});
	} else {
		$('#pp_first_name').val(firstname);
		$('#shopAddressFirstname').css({'border': '1px dotted #e4e4e4','background-color': '#277596','color': 'white'});
	}
	
	if ( street.length < 3) {
		b_set2True = false;
		$('#shopAddressStreet').css({'border': '1px dotted #CBD420','background-color': '#D8DE54','color': 'black'});
	} else {
		$('#pp_address1').val(street + " " + $('#shopAddressStreetno').val());
		$('#shopAddressStreet').css({'border': '1px dotted #e4e4e4','background-color': '#277596','color': 'white'});
	}

	if ( zip.length < 4) {
		b_set2True = false;
		$('#shopAddressZip').css({'border': '1px dotted #CBD420','background-color': '#D8DE54','color': 'black'});
	} else {
		$('#pp_zip').val(zip);
		$('#shopAddressZip').css({'border': '1px dotted #e4e4e4','background-color': '#277596','color': 'white'});
	}
	
	if ( city.length < 2) {
		b_set2True = false;
		$('#shopAddressCity').css({'border': '1px dotted #CBD420','background-color': '#D8DE54','color': 'black'});
	} else {
		$('#pp_city').val(city);
		$('#shopAddressCity').css({'border': '1px dotted #e4e4e4','background-color': '#277596','color': 'white'});
	}
	
	if (!checkemail(email)) {
		b_set2True = false;
		$('#shopAddressEmail').css({'border': '1px dotted #CBD420','background-color': '#D8DE54','color': 'black'});
	} else {
		$('#pp_email').val(email);
		$('#shopAddressEmail').css({'border': '1px dotted #e4e4e4','background-color': '#277596','color': 'white'});
	}
	
//	alert(b_set2True);

	setCheckoutButtons(b_set2True);
}

function setCheckoutButtons(b_show) {
	if (b_show == true) {
		$('#shopCheckoutKK').attr('disabled',false);
		$('#shopCheckoutKK').css({'border': '3px solid #e4e4e4', 'font-weight':'bold','cursor':'pointer'}); 
		$('#shopCheckoutBilling').attr('disabled',false); 
		$('#shopCheckoutBilling').css({'border': '3px solid #e4e4e4', 'font-weight':'bold', 'cursor':'pointer'}); 
	} else {
		$('#shopCheckoutKK').attr('disabled',true); 
		$('#shopCheckoutKK').css({'border': '1px dotted #e4e4e4','font-weight':'normal'}); 
		$('#shopCheckoutBilling').attr('disabled',true); 
		$('#shopCheckoutBilling').css({'border': '1px dotted #e4e4e4','font-weight':'normal'}); 
	}
}

function checkemail(emailaddress){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(emailaddress)) {
		return true;
	}
	else{
		
		return false;
	}
}

function saveOrderData(b_showThankYouBillingPage) {
	$.ajax({
		type: 'POST',
		//add pageNum type
		url: "index.php?id=shopajax&type=999",
		data: ({shopmode: "saveOrderData"}),
		//if success: 
		success: function(response) {
			if (b_showThankYouBillingPage) { // If billingmode
				headToThankYouBillingPage();
			} else { // if credit card mode > submit the form
				$('#checkout_kkForm').submit();
			}
			return true;
		}
	});
}
