/**
 * JS for payment pages
 */
var Payment = {
	formSubmitted: false,
	
	/**
	 * Select payment package
	 */
	sp: function (action, pid, mode, sdp) {
		$('#paymentform').attr('action', action);
		$('#pf_stardollar_package').attr('value', pid);
		$('#pf_mode').attr('value', mode);
		if (sdp) {
			$('#pf_sdp').attr('value', sdp);
		}
		$('#paymentform').submit();
	},
	 
	/**
	 * Open kambi
	 */
	sms: function (country, sdpId) {
		Base.pop('/'+language+'/account/payment/gokambi.php?country='+country+'&sdpId='+sdpId, 'SMSKambi', 310, 350, 'no', 'no');
	},
	
	/** 
	 * Filter keypress events to contain only numbers
	 */
	numbersonly: function (e) {
		var uc = e.charCode ? e.charCode : e.keyCode;
		if (uc != 8 && uc != 9 && uc != 16 && uc != 17 && uc != 39 && uc != 46 && uc != 114 && uc != 116 && uc != 190
			&& uc != 63232 && uc != 63233 && uc != 63234 && uc != 63235 && uc != 46 && uc != 63272) {
			if (uc < 48 || uc > 57) {
				return false;
			}
		}
	},
	
    prepaidCharRe: /[-a-hjkmnp-z0-9]/i,
	/** 
	 * Filter keypress events to contain only walid prepaid code characters
	 */
	filterPrepaidChars: function (e) {
		var uc=(e.charCode ? e.charCode : e.keyCode);
		if (!(uc <= 46 /* control chars */ 
			  || (uc >= 91 && uc <= 93)  /* windows start et al.*/
			  || (uc >= 112 && uc <= 123))) { /* f1-f12*/
			return this.prepaidCharRe.test(String.fromCharCode(uc));
		}
	},
	
	/**
	 * Giftcard
	 */
	giftCard: function (code) {
		Base.pop('/'+language+'/popups/giftCard.php?gcode='+code, 'giftcard', 830, 470);
	},
	
	/**
	 * Toggle display payment methods
	 */
	tm: function () {
		$('#moreProv').fadeOut(1);
		$('#poptions div.pservice.hide').slideDown(300);
	},
	
	/** 
	 * toggle tooltip on landingpage
	 */
	tt: function(cont, e) {
		var tooltip = '#automagiBox';
		if ('mouseleave' == e.type) {
			$(tooltip).hide().remove();
		} else {
			var cont = $('div.img', cont);
			var html = '<div id="automagiBox" class="yass"><p></p><div></div></div>';
			var txt = $('blockquote', cont.parent()).html();
			var head = $('span', cont.parent()).html();
			
			$(cont).append(html);
			
			$(tooltip+' p').html('<b>'+head+'</b>'+txt);
			$(tooltip).css({'bottom':65, 'left':20, 'position':'absolute'});
			$(tooltip).show();
		}
	},
	
	what: function(pspName) {
		Base.pop('/'+language+'/popups/paymentWhatsThis.php?pspName='+pspName, 'WhatsThis', '360', '460', 'yes', 'yes');
	},
	
	creditcard_submit: function(){
		if (Payment.has_run_creditcard) {
			return false;
		}
		Payment.has_run_creditcard = true;
		$('#ccsubmitbtn').toggle();
		$('#loadingpleasewait').toggle();
		return true;
	},	
	
	selectPackage: function(clickedElement, stardollar_package, update_total){
		if (Payment.formSubmitted) {
			return false;
		}		
		frm = Payment.findNearForm(clickedElement);
		frm.find('input[name=\'stardollar_package\']').val(stardollar_package);
		packagesParent = $(clickedElement).parents('.packages');
		packagesParent.find('a').each(function(){
			obj = $(this);
			obj.find('img').hide();
			obj.find('span').show();
		})
		clickedItem = $(clickedElement);
		clickedItem.find('img').show();
		clickedItem.find('span').hide();
		clickedItem.trigger('blur');
		if (update_total) {
			totalsum = $(clickedElement).closest('.package').find('.totalsum');
			fp = frm.find('.fullprice_info');
			if ('' != totalsum.text()) {
				fp.find('.total_price').text(totalsum.text());
				fp.show();
			} else {
				fp.hide();
			}
		}
	},
	
	submitPackage: function(clickedElement, stardollar_package){
		if (Payment.formSubmitted) {
			return false;
		}
		Payment.formSubmitted = true;
		frm = Payment.findNearForm(clickedElement);
		frm.find('input[name=\'stardollar_package\']').val(stardollar_package);
		frm.trigger('submit');
		return false;
	},
	
	fetchIdealProvider: function(stardollar_package, countrycode){
		$.ajax({
			async: false, 
			url: '/'+language+'/ajax/payment/idealIssuers.php',
			type: 'POST',
			data: 'stardollar_package='+stardollar_package+'&country='+countrycode,
			dataType: 'json',
			success: function(json_data) {
				$('#idealIssuer').html('');
				$.each(json_data, function(i, item){
					an_option = $('<option>').attr('value', i).html(item);
					$('#idealIssuer').append(an_option);
				})
			},
			error: function() {
				
			}
		});
	},
	
	submitForm: function(submitForm){
		if (Payment.formSubmitted) {
			return false;
		}
		Payment.formSubmitted = true;
		$('#' + submitForm).trigger('submit').bind('submit',function(){return false}).attr('action','');
		return false;
	},
	
	toPayment: function(){
		$().scrollTop($("#countsel").offset().top);
		return false;
	},
	
	submitNearForm: function(obj){
		frm = Payment.findNearForm(obj);
		Payment.submitForm(frm.attr('id'));
		return false;
	},
	
	findNearForm: function(obj){
		return $(obj).closest('.psp').find('form');
	}
}