var issueFirstId;
var issueLastId;
var priceCats = new Array();
var priceCatLevels = new Array();
var abonSumTotal = 0;

function initAbonesana() {
	issueFirstId = null;
	issueLastId = null;
	initPriceCats();
	issuesUnselect();
	updateSumTotal();
		
	if(document.pForm['init_ch_lo']) {
		if(document.pForm['init_ch_lo'].value != '')
			iSelect(document.pForm['init_ch_lo'].value);
		if(document.pForm['init_ch_hi'].value != '')
			iSelect(document.pForm['init_ch_hi'].value);
	}
}



function initPriceCats() {
	priceCats = new Array();
	priceCatLevels = new Array();
	var numCats = document.pForm['pCatCount'] ? document.pForm['pCatCount'].value : 0;
	var i, c, j;
	for(i = 0; i < numCats; i++) {
		c = document.pForm['pCat' + i].value;
		c = c.split(",");
		priceCats[i] = new Array();
		priceCatLevels[i] = new Array();;
		for(j = 0; j < c.length; j += 2) {
			priceCats[i][c[j]] = c[j + 1];
			priceCatLevels[i][j / 2] = c[j];
		}
	}
}




function locSelect(locId) {
	var locIds = document.pForm['locIds'].value;
	var lang = document.pForm['lang'].value;
	locIds = locIds.split(',');
	for(var i = 0; i < locIds.length; i++) {
		document.getElementById('im_loc_' + locIds[i]).src = '/layout/ch_' + (locId == locIds[i] ? 'on' : 'off') + '.gif';
	}
	document.pForm['location'].value = locId;
	window.location = "magazines-order.php?lang=" + lang + "&sid=" + document.pForm['sid'].value + "&location=" + locId;
	/*dojo.io.bind({
			url: (lang == 1 ? "/abonesana" : "/en/subscription") + "?ajax=1&lang=" + document.pForm['lang'].value + "&location=" + locId + "&refresh=1",
			load: function(type, data, evt){ locSelectDone(data); },
			mimetype: "text/plain"
	});*/
	
}
/*function locSelectDone(loadedData) {
	document.getElementById('abonContent').innerHTML = loadedData;
	initAbonesana();
}*/



function iSelect(objId) {
	objId = objId.split("_");	// [1] => price cat; [2] => id
	
	if(!issueFirstId) {	// if no boundaries have been selected
		issueFirstId = objId;
		issuesSelect(issueFirstId[1], issueFirstId[2], issueFirstId[2]);
	
	} else if(!issueLastId) {	// if only the first boundary has been selected
		if(issueFirstId[1] == objId[1]) {	// if in the same price cat, then extend the current range
			if(parseInt(objId[2]) > parseInt(issueFirstId[2]))
				issueLastId = objId;
			else {
				issueLastId = issueFirstId;
				issueFirstId = objId;
			}
			issuesSelect(issueFirstId[1], issueFirstId[2], issueLastId[2]);
		} else {	// if in different prace cats, then start a new range.
			issuesUnselect(issueFirstId[1]);
			issueFirstId = objId;
		}
	
	} else { 	// if both boundaries have been selected
		if(issueFirstId[1] == objId[1])	{	// if in the same price cat
			if(parseInt(objId[2]) == parseInt(issueFirstId[2])) {
				if(issueFirstId[2] < issueLastId[2]) issueFirstId[2]++; 
			} else if(parseInt(objId[2]) == parseInt(issueLastId[2])) {
				if(issueLastId[2] > issueFirstId[2]) issueLastId[2]--; 
			} else if(Math.abs(parseInt(issueLastId[2]) - parseInt(objId[2])) <= Math.abs(parseInt(issueFirstId[2]) - parseInt(objId[2])) && objId[2] >= issueFirstId[2])
				// move the last boundary, if the obj is closer to that
				issueLastId = objId;
			else
				// otherwise, move the first boundary
				issueFirstId = objId;	
			
			//alert(issueFirstId + "\n" + issueLastId);
			issuesUnselect(issueFirstId[1]);
			issuesSelect(issueFirstId[1], issueFirstId[2], issueLastId[2]);
			
		} else {	// if in different price cat, then start a new range
			issuesUnselect(issueFirstId[1]);
			issueFirstId = objId;
			issueLastId = null;
			issuesSelect(issueFirstId[1], issueFirstId[2], issueFirstId[2]);
		}
	}
	
	updateSumTotal();
	return false;
}


function issuesSelect(pCat, idLo, idHi) {
	//alert(pCat + ": " + idLo +',' + idHi);
	for(var i = parseInt(idLo); i <= parseInt(idHi); i++) {
		//console.debug(document.pForm['ch_' + pCat + '_' + i]);
		if(document.pForm['ch_' + pCat + '_' + i]) {
			
			document.pForm['ch_' + pCat + '_' + i].value = "1";
			document.getElementById('im_ch_' + pCat + '_' + i).src = '/layout/ch_on.gif';
		}
	}
}


function issuesUnselect(pCat) {
	var catLo = pCat ? pCat : 0;
	var catHi = pCat ? pCat : priceCats.length - 1;
	for(var i = catLo; i <= catHi; i++) {
		for(var j = 1; j <= document.pForm['maxChCount'].value; j++) {
			if(document.pForm['ch_' + i + '_' + j]) {
				document.pForm['ch_' + i + '_' + j].value = "0";
				document.getElementById('im_ch_' + i + '_' + j).src = '/layout/ch_off.gif';
			}
		}
	}
}



function updateSumTotal() {
	var iCount = 0;
	var pCat, iRemain;
	
	if(document.pForm['sum_total']) {
		if(issueFirstId && issueLastId)
			iCount = issueLastId[2] - issueFirstId[2] + 1;
		else if(issueFirstId)
			iCount = 1;
		else {
			document.pForm['sum_total'].value = '0.00';
			return;
		}
		pCat = issueFirstId[1];
		
		//alert(priceCatLevels);
			
		var sumTotal = 0;
		for(var i = priceCatLevels[pCat].length - 1; i >= 0 && iCount > 0; i--) {
			iRemain = iCount % priceCatLevels[pCat][i];
			//alert(priceCatLevels[pCat][i] + "," + priceCats[pCat][priceCatLevels[pCat][i]]);
			sumTotal += (iCount - iRemain) / priceCatLevels[pCat][i] * priceCats[pCat][priceCatLevels[pCat][i]];
			iCount = iRemain;
		}
		sumTotalStr = sumTotal / 100;
		
		if(sumTotal % 100 == 0) 
			sumTotalStr += ".00";
		else if(sumTotal % 10 == 0) 
			sumTotalStr += "0";
		
		document.pForm['sum_total'].value = sumTotalStr;
		document.getElementById('sumTotalValue').innerHTML = sumTotalStr;
		
		abonSumTotal = sumTotal;
	}
}



function pFormSubmit() {
	if(abonSumTotal > 0) {
		document.pForm.submit();
	//} else {
		
	}
}
