//var cal = new CalendarPopup();
//cal.setReturnFunction("setDate");
function setDate(y,m,d) {
		document.getElementById('date_y').value=y;
		document.getElementById('date_m').value=LZ(m);
		document.getElementById('date_d').value=LZ(d);
		document.getElementById('data').innerHTML=y+'-'+LZ(m)+'-'+LZ(d);
		updateStats();
}

function partnerPopup(address, windowWidth, windowHeight) {
    PosX = 0; PosY= 0;
    ScrWidth = 640; ScrHeight = 480;
    if (window.screen) {ScrWidth = top.screen.width; ScrHeight = top.screen.height}

    PosX = Math.round((ScrWidth - windowWidth)/2);
    PosY = Math.round((ScrHeight - windowHeight)/2);

    var s = new Date();
    win_name = s.getMinutes();
    win_name += s.getSeconds();

    window.open(address, win_name, "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+windowWidth+",height="+windowHeight+",left="+PosX+",top="+PosY);
}

function postToPartnerPopup(f, windowWidth, windowHeight) {
    PosX = 0; PosY= 0;
    ScrWidth = 640; ScrHeight = 480;
    if (window.screen) {ScrWidth = top.screen.width; ScrHeight = top.screen.height}

    PosX = Math.round((ScrWidth - windowWidth)/2);
    PosY = Math.round((ScrHeight - windowHeight)/2);

    var s = new Date();
    win_name = s.getMinutes();
    win_name += s.getSeconds();

    window.open(null, win_name, "menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width="+windowWidth+",height="+windowHeight+",left="+PosX+",top="+PosY);	
    
    t = f.target;
    
    f.target=win_name;
    f.submit();
    
    f.target = t;    
}

var current_typ_id = 0;

function changetype() {
		if(current_typ_id) {			
				document.getElementById('variablesDivTyp' + current_typ_id).style.display = 'none';
				document.getElementById('variablesDivTyp' + current_typ_id).style.visibility = 'hidden';
				document.getElementById('variablesDivTyp' + current_typ_id).style.height = '0px';
		}
	
		typ_id = document.getElementById('typ_id').options[document.getElementById('typ_id').selectedIndex].value;			
		
		document.getElementById('variablesDivTyp' + typ_id).style.display = 'block';
		document.getElementById('variablesDivTyp' + typ_id).style.visibility = 'visible';
		document.getElementById('variablesDivTyp' + typ_id).style.height = '';
		
		current_typ_id = typ_id;
}		
	
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
	
var x, y;

document.onmousemove=mouseMove;
window.onscroll=windowScroll;

function mouseMove(e) {
    if (!e) var e = window.event;
    if (!isNS4) {
        x=e.clientX + document.body.scrollLeft;
        y=e.clientY + document.body.scrollTop;
    } else {
        x=e.pageX;
        y=e.pageY;
    }
}

function windowScroll(e) {

  if (!e) var e = window.event;
  if (e.pageY) {
    posy = e.pageY;
  } else if (e.clientY) {
    posy = document.body.scrollTop;
  } else {
    posy = 0;
  }
  y = y + posy;

}

function checkIfInt(obj,value) {
		var reInteger = /^\d+$/
		
		if(obj.value == '' || !reInteger.test(obj.value))	{
				obj.value = value;
		}
}
	
var perline = 9;
var divSet = false;
var curId;
var colorLevels = Array('0', '3', '6', '9', 'C', 'F');
var colorArray = Array();

function pickColor(id) {
		if (!divSet) { setDiv(); }
		var picker = getObj('colorpicker');     	
		 	
 		curId = id;

 		picker.style.top = y;
 		picker.style.left = x;
		picker.style.display = 'block';
}

function setDiv() {     
		if (!document.createElement) { return; }
    var elemDiv = document.createElement('div');
    if (typeof(elemDiv.innerHTML) != 'string') { return; }
    genColors();
    elemDiv.id = 'colorpicker';
  	elemDiv.style.position = 'absolute';
    elemDiv.style.display = 'none';
    elemDiv.style.border = '#000000 1px solid';
    elemDiv.style.background = '#F1EEE9';
    elemDiv.innerHTML = '<span style="font-family:Verdana; font-size:10px;">Wybierz kolor: <br>' 
    + getColorTable() 
    + '</span>';
    document.body.appendChild(elemDiv);
    divSet = true;
}

function getObj(id) {		
		return document.getElementById(id);
}

function addColor(r, g, b) {
	 	var red = colorLevels[r];
	 	var green = colorLevels[g];
	 	var blue = colorLevels[b];
	 	addColorValue(red, green, blue);
}

function addColorValue(r, g, b) {
		colorArray[colorArray.length] = '#' + r + r + g + g + b + b;
}
 
function setColor(color) {
		var picker = getObj('colorpicker');   
		document.getElementById('variables' + curId).value = color;
		document.getElementById('variables_c' + curId).style.backgroundColor = color;		
		picker.style.display = 'none';
		changetype();
}
    
function genColors() {
		addColorValue('0','0','0');
		addColorValue('3','3','3');
		addColorValue('6','6','6');
		addColorValue('8','8','8');
		addColorValue('9','9','9');                
		addColorValue('A','A','A');
		addColorValue('C','C','C');
		addColorValue('E','E','E');
		addColorValue('F','F','F');                                
		
		for (a = 1; a < colorLevels.length; a++)
		addColor(0,0,a);
		for (a = 1; a < colorLevels.length - 1; a++)
		addColor(a,a,5);
		
		for (a = 1; a < colorLevels.length; a++)
		addColor(0,a,0);
		for (a = 1; a < colorLevels.length - 1; a++)
		addColor(a,5,a);
		
		for (a = 1; a < colorLevels.length; a++)
		addColor(a,0,0);
		for (a = 1; a < colorLevels.length - 1; a++)
		addColor(5,a,a);
		
		
		for (a = 1; a < colorLevels.length; a++)
		addColor(a,a,0);
		for (a = 1; a < colorLevels.length - 1; a++)
		addColor(5,5,a);
		
		for (a = 1; a < colorLevels.length; a++)
		addColor(0,a,a);
		for (a = 1; a < colorLevels.length - 1; a++)
		addColor(a,5,5);
		
		for (a = 1; a < colorLevels.length; a++)
		addColor(a,0,a);			
		for (a = 1; a < colorLevels.length - 1; a++)
		addColor(5,a,5);
		
		return colorArray;
}

function getColorTable() {
		var colors = colorArray;
		var tableCode = '';
		tableCode += '<table border="0" cellspacing="1" cellpadding="1">';
		for (i = 0; i < colors.length; i++) {
		    if (i % perline == 0) { tableCode += '<tr>'; }
		    tableCode += '<td bgcolor="#000000"><a style="outline: 1px solid #000000; color: ' + colors[i] + '; background: ' + colors[i] + ';font-size: 10px;" href="javascript:setColor(\'' + colors[i] + '\');">&nbsp;&nbsp;&nbsp;</a></td>';
		    if (i % perline == perline - 1) { tableCode += '</tr>'; }
		}
		if (i % perline != 0) { tableCode += '</tr>'; }
		tableCode += '</table>';
		return tableCode;
}

function updateStats() {
		address = 'partner.php?UniqueName=partner&action=statistic_engine&';	
		address += 'par_guid=' + document.getElementById('par_guid').value + '&'; 
		address += 'ban_guid=' + document.getElementById('ban_guid').value + '&'; 
		address += 'typ_id=' + document.getElementById('typ_id').value + '&'; 
		address += 'date=' + document.getElementById('date_y').value + '-' + document.getElementById('date_m').value + '-' + document.getElementById('date_d').value + '&'; 
		address += 'result=' + document.getElementById('result').options[document.getElementById('result').selectedIndex].value + '&'; 
		address += 'type=' + document.getElementById('type').options[document.getElementById('type').selectedIndex].value; 	
		
		document.getElementById('statsFrame').src = address;
}

function copyToClipboard(copy, text)
{
	if(window.clipboardData) {
		window.clipboardData.setData('Text',copy);
		alert(text);
	} else {
		alert('Twoja przeglądarka nie obsługuje schowka.');
	}
}

function changeValue(id,value) {
		document.getElementById(id).value = value;	
}

function changeInnerHTML(id,value) {
		document.getElementById(id).innerHTML = value;	
}

function loadSelectProduct(id) {
		r = document.getElementById('variables' + id).value;
				
		delimiter = ',';
  	tmpArray = new Array();
  	i=0;

	  while (r.indexOf(delimiter)>0) {
		    tmpArray[i]=r.substr(0,r.indexOf(delimiter));
		    r=r.substr(r.indexOf(delimiter)+1,r.length-r.indexOf(delimiter)+1); 
		    i++;
	  }

  	tmpArray[i]=r;

		category = tmpArray[0] + ',' + tmpArray[1];
		
		for(i=0;i<document.getElementById('tmp' + id + '[0]').options.length;i++) {
				if(category == document.getElementById('tmp' + id + '[0]').options[i].value) {
						document.getElementById('tmp' + id + '[0]').selectedIndex = i;
						break;	
				}	
		}
			
		if(tmpArray[2]==1) {
				document.getElementById('tmp' + id + '[1]').checked = true;		
		} else {
				document.getElementById('tmp' + id + '[1]').checked = false;	
		}
		
		document.getElementById('tmp' + id + '[2]').value = (tmpArray[3] > 0) ? tmpArray[3] : '1';
}

function updateSelectProduct(id) {
		r = document.getElementById('tmp' + id + '[0]').options[document.getElementById('tmp' + id + '[0]').selectedIndex].value;
		r += (document.getElementById('tmp' + id + '[1]').checked) ? ',1' : ',0';
		
		v = document.getElementById('tmp' + id + '[2]').value;
		
		if(v < 1) {
				r += ',1';	
				document.getElementById('tmp' + id + '[2]').value = 1;
		} else if(v > 20) {
				r += ',20';	
				document.getElementById('tmp' + id + '[2]').value = 20;
		} else {
				r += ',' + document.getElementById('tmp' + id + '[2]').value;	
		}

		document.getElementById('variables' + id).value = r;
}
