// JavaScript Document
function PeriodAction(start, end) {
	var startPeriodObj = document.getElementById('startPeriod');
	var endPeriodObj = document.getElementById('endPeriod');
	var sButton = document.getElementById('submitButton');
	startPeriodObj.value = start;
	endPeriodObj.value = end;
	sButton.value = '1';
	var form = document.forms['pointsForm'].submit();
}

function parseStartPeriod() {
	var textObject = document.getElementById('startPeriod');
	
	var year = document.getElementById('start_year').value;
	var month = document.getElementById('start_month').value;
	//var mt = document.getElementById('start_mt').value;
	var day = document.getElementById('start_day').value;
	
	textObject.value = year+"."+month+"."+day;
	//textObject.value = mt+"."+day;
}

function parseEndPeriod() {
	var textObject = document.getElementById('endPeriod');
	
	var year = document.getElementById('end_year').value;
	var month = document.getElementById('end_month').value;
	//var mt = document.getElementById('end_mt').value;
	var day = document.getElementById('end_day').value;

	textObject.value = year+"."+month+"."+day;
	//textObject.value = mt+"."+day;
}

function CheckRegistrationForm(form) {
	var phone = form.elements['phone'];
	var email = form.elements['email'];
	var sendvia_sms = form.elements['sendvia_sms'];
	var sendvia_email = form.elements['sendvia_email'];
	
	var sms = (phone.value != "" && sendvia_sms.checked);
	var email = (email.value != "" && sendvia_email.checked);
	
	if (sms != true && email != true) {
		alert(_REG_ERR_6);
		return false;
	} else {
		return true;
	}
}

function ShopRemoveFromCart(id) {
	var table = document.getElementById('products');
	var row = document.getElementById("product["+id+"]");
	var price = document.getElementById("price["+id+"]");
	var total_price = document.getElementById("total_price");
	if( total_points )
	    total_points -= Math.floor(price.innerHTML);
	total_price.innerHTML = Math.floor(total_price.innerHTML) - Math.floor(price.innerHTML);
	table.deleteRow(row.rowIndex);
	RemoveFromCart(id);
}

function onlyDigits(e) {
	var key = (IE) ? window.event.keyCode : e.which;
	var obj = (IE) ? event.srcElement : e.target;
	var isNum = (key > 47 && key < 58) ? true:false;
	if (key < 32)
	   return true;
	return isNum;
}

function change_icon(img_id, type) {
	var img = document.getElementById(img_id);
	var a_link = document.getElementById("a_"+img_id);
	
	if (type != null) {
		a_link.className = type;
		img.src = "loyalty/images/icon."+img_id+"."+type+".png";
	} else {
		img.src = "loyalty/images/icon."+img_id+".png";
		a_link.className = '';
	}
}

function createPopup(src, target, width, height, params) {
	var target = target || "PopupWindow";
	dialogParams = params;
	var top = (screen.height-height)/2;
	var left = (screen.width-width)/2;
	popupWindow = window.open(src, target, "toolbar=no. location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyright=no, top="+top+", left="+left+", width="+width+", height="+height+"");
	popupWindow.focus();
}

function preview(filename, w, h) {
	if (filename != "")
		createPopup("loyalty/preview.php?src="+filename, "ImagePreview", w, h, null);
}

function TempPassStatus(request) {
	var obj = document.getElementById('sendpass_status');
	var xml = request.responseXML;
	
	var StatusMsg = xml.getElementsByTagName("Status");
	var Msg = StatusMsg[0].firstChild.nodeValue;
	
	obj.innerHTML = Msg;
}

function GetTempPass() {
	var obj = document.getElementById('sendpass_status');
	var card_id = document.getElementById('card_id');
	var sendTypes = document.getElementsByName('sendpass');
	var sendType = null;
	
	if (card_id.value != null && card_id.value != "") {
		for(var i=0; i<sendTypes.length; i++) {
			if (sendTypes[i].checked) {
				sendType = sendTypes[i].value;
				break;
			}
		}
		
		if (i == 2 && sendType == null) {
			alert(_REG_ERR_6);
		} else {
			//alert("Siunciame "+card_id.value+" laikina slaptazodi"+sendType);
			obj.innerHTML = _LOGIN_SENDING_TEMP_PASS;
			var ajax = new AJAX();
			ajax.OnStateChange(TempPassStatus);
			ajax.Open("POST", "loyalty/ajax/getpass.php?"+Math.floor(Math.random()*11), true);
			ajax.Send("card_id="+card_id.value+"&sendpass="+sendType);
		}
	} else {
		alert(_REG_ERR_1);
	}
}

function XMLResponse(request) {
}

function SetCardId(obj) {
	var card_id = obj.value;
	var ajax = new AJAX();
	ajax.OnStateChange(XMLResponse);
	ajax.Open("POST", "loyalty/ajax/set_card_id.php?"+Math.floor(Math.random()*11), true);
	ajax.Send("card_id="+card_id);
}

function yearSelect( type, year ) {
	var html = "";
	html += "<select id=\"" + type + "_year\" name=\"" + type + "_year\" onchange=\"changeYear('" + type + "', this.value);\">";
	for( var i in years )
	    html += "<option " + ((year == i)?"selected":"") + " value=\"" + i + "\">" + i + "</option>";
	html += "</select>";
	return html;
}

function monthSelect( type, year, month ) {
	var html = "";
	html += "<select id=\"" + type + "_month\" name=\"" + type + "_month\" onchange=\"changeMonth('" + type + "', '" + year + "', this.value);\">";
	for( var i in years[year] )
	    html += "<option " + ((month == i)?"selected":"") + " value=\"" + i + "\">" + months[i] + "</option>";
	html += "</select>";
	return html;
}

function daySelect( type, year, month, day ) {
	var html = "";
	html += "<select id=\"" + type + "_day\" name=\"" + type + "_day\" onchange=\"changeDay('" + type + "');\">";
	var days = years[year][month];
	for( var i=1; i<=days; i++ ) {
	    var xday = (i<10)?("0"+i):i;
	    html += "<option " + ((day == xday)?"selected":"") + " value=\"" + xday + "\">" + xday + "</option>";
	}
	html += "</select>";
	return html;
}

function changeYear( type, year ) {
	var td = document.getElementById( type + "_date" );
	var month = "01";
	for( var i in years[year] ) {
	    month = i;
	    break;
	}
	var html = "";
	html += yearSelect( type, year );
	html += monthSelect( type, year, month );
	html += daySelect( type, year, month, "01" );
	td.innerHTML = html;
	changeDay( type );
}

function changeMonth( type, year, month ) {
	var td = document.getElementById( type + "_date" );
	var html = "";
	html += yearSelect( type, year );
	html += monthSelect( type, year, month );
	html += daySelect( type, year, month, "01" );
	td.innerHTML = html;
	changeDay( type );
}

function initDateSelection( type, year, month, day ) {
	var td = document.getElementById( type + "_date" );
	var html = "";
	html += yearSelect( type, year );
	html += monthSelect( type, year, month );
	html += daySelect( type, year, month, day );
	td.innerHTML = html;
	changeDay( type );
}

function changeDay( type ) {
	if( type == "start" )
	    parseStartPeriod();
	else
	    parseEndPeriod();
}
