enableButton = function(button_id) {
	button = document.getElementById(button_id);
	button.className = 'medium';
	button.disabled = '';
},
disableButton = function(button_id) {
	button = document.getElementById(button_id);
	button.className = 'medium disabled';
	button.disabled = 'true';
}

/**
* Shop componenet
* Constructor
*/
shop = function() {
	disableButton('buy_button');
	this.first = true;
	this.name = 'Магазин электронных ваучеров';
	this.url = 'http://www.portmone.com.ua/embedded-payments';
	this.v_section = '/voucher/';
	this.id = -1;
};

/**
* Shop componenet Prototype
*/
shop.prototype = {
/**
* Get data
*/
	getData : function(_url, _onComleteCallback) {
		try {
			if (typeof window.ActiveXObject != 'undefined') {
				var xhr = new ActiveXObject('Microsoft.XMLHTTP');
			} else {
				var xhr = new XMLHttpRequest();
			};
			if(xhr != null){
				xhr.open('GET',_url, true);
				xhr.send(null);
			};
			xhr.onreadystatechange = function() {
				if (xhr.readyState == 4) {
					var items  = new Array();
					if(xhr.status == 200){
						items = xhr.responseXML.getElementsByTagName('response')[0].childNodes;
					}
					_onComleteCallback(items);
				}
			};
		} catch (error){}
	},

	/**
	* Init companies list
	*/
	change : function() {
		if(this.first) {
			this.getData(this.url +this.v_section, this.initCompaniesCallback);
			this.first = false;
		} else {
			this.setId();
			this.getData(this.url +this.v_section + this.id , this.changeCompanyCallback);
		}
	},
	/**
	* Set id
	*/
	setId : function() {
		this.id = document.getElementById('operator_id').value;
	},

	/**
	*  Init companies callback 
	*/
	initCompaniesCallback : function(nodeList) {
		operatorSelect = document.getElementById('operator_id');
		operatorSelect.innerHTML = '';
		for(var i=0; i<nodeList.length; i++){
			if (nodeList[i].nodeName == "item") {
				var option = document.createElement('option');
				var label = document.createTextNode(nodeList[i].getAttribute('label'));
				option.value = nodeList[i].getAttribute('data');
				option.appendChild(label);
				operatorSelect.appendChild(option);
			};
		};
	},
	/**
	* Change company
	*/
	changeCompanyCallback : function(nodeList) {
		flag = false;
		productSelect = document.getElementById('product_id');
		productSelect.disabled = true;
		productSelect.innerHTML = '';
		for(var i=0; i<nodeList.length; i++){
			if (nodeList[i].nodeName == "item") {
				var option = document.createElement('option');
				var label = document.createTextNode(nodeList[i].getAttribute('price') + ' грн.');
				option.value = nodeList[i].getAttribute('data');
				option.appendChild(label);
				productSelect.appendChild(option);
				flag = true;
			};
		};
		if(flag) {
			productSelect.disabled = false;
			enableButton('buy_button');
		} else {
			var option = document.createElement('option');
			var label = document.createTextNode('Нет продукции');
			option.appendChild(label);
			productSelect.appendChild(option);
			productSelect.disabled = true;
			disableButton('buy_button');
		}
	}
};
var shp = new shop();
function select() {
	shp.change();
}
var operator_id = document.getElementById('operator_id');
if (operator_id.addEventListener){
	operator_id.addEventListener("click", select, false);
	operator_id.addEventListener("change", select, false);
	operator_id.addEventListener("keydown", select, false);
	operator_id.addEventListener("keyup", select, false);
} else if (operator_id.attachEvent) {
	operator_id.attachEvent("onclick", select);
	operator_id.attachEvent("onchange", select);
	operator_id.attachEvent("onkeydown", select);
	operator_id.attachEvent("onkeyup", select);
} else {
	operator_id.onclick = select;
	operator_id.onchange = select;
	operator_id.onkeydown = select;
	operator_id.onkeyup = select;
};


//Account replenish
var amount = document.getElementById('amount');
var replenish_form = document.getElementById('replenish_form');

checkReplenishFileds = function(lang) {
	try {

		/** Amount **/
		if(amount.value == '') {
			if(lang == 'uk') {
				error_msg = 'Введіть суму поповнення';
			} else {
				error_msg = 'Введите сумму пополнения';
			}
			throw error_msg;
		}
		if(!amount.value.match(/^[1-9]\d{0,4}$/)){
			if(lang == 'uk') {
				error_msg = 'Неправильний формат суми поповнення';
			} else {
				error_msg = 'Неправильный формат суммы пополнения';
			}
			throw error_msg;
		}
		replenish_form.submit();
	} catch (e) {
		alert(e);
	}
}

/* Mask */
/**
 * 
 * @param phone
 * @return void
 */
function completeInput (phone){
	var wpn = phone.replace('(', '').replace(')', '').replace('-', '').replace('-', '');
	$('#wpn').val(wpn);
};
$('#phone_number').mask('+999(99)999-99-99');
$('#phone_number_travelsim').mask('+99(99)999-99-99');
$('#phone_number').keyup(function(event){
	var val = $('#phone_number').val();
	 if(val.match(/^\+37(.+)/)) {
		$('#phone_number_travelsim').val('+37(__)___-__-__');
		$('#phone_number_travelsim').focus();
		$('#phone_number').hide();
		$('#phone_number_travelsim').show();
	} else if(val.match(/^\+380\(\d{2}\)\d{3}\-\d{2}\-\d{2}$/)) { //If comlete
		completeInput(val);
	}
});
$('#phone_number_travelsim').keyup(function(event){
	var val = $('#phone_number_travelsim').val();
	 if(val.match(/^\+38(.+)/)) {
		$('#phone_number').val('+380(__)___-__-__');
		$('#phone_number').focus();
		$('#phone_number_travelsim').hide();
		$('#phone_number').show();
	} else if(val.match(/^\+37\(\d{2}\)\d{3}\-\d{2}\-\d{2}$/)) { //If comlete
		completeInput(val);
	}
});