function cP(row, col) {
	$("#r"+row+"c"+col).removeClass('divPix');
	$("#r"+row+"c"+col).addClass('divPixOver');
}

function cPB(row,col){
	$("#r"+row+"c"+col).removeClass('divPixOver');
	$("#r"+row+"c"+col).addClass('divPix');
}

function btnP(row,col,linkTo){
	if($.inArray("r"+row+"c"+col, takenBoxes)<0) {
		if(colSel==-1){
			HideQuestionDiv()
			colSel = col;
			rowSel = row;
			highestRow = row>highestRow?row:highestRow;
			highestCol = col>highestCol?col:highestCol;
			ResetPixels();
			BtnPressColor(row, col);
		} else {
			SelectAll(row,col, colSel, rowSel);
			highestRow = row>highestRow?row:highestRow;
			highestCol = col>highestCol?col:highestCol;
			ShowQuestionDiv(row, col);
            colSel = -1;
			rowSel = -1;
		}
	} else {
		if(colSel==-1){
			window.location = linkToBoxes["r"+row+"c"+col];
		}
	}
}

function ShowQuestionDiv(row, col){
	if(canGo) {
	    oriz = Math.abs(rowSel - row)+1;
        vert = Math.abs(colSel - col)+1;
        total = oriz * vert;        
        totalStr = total==1?"o singura zona":total+" zone";
        $("#nrSelectat").text(totalStr);
        $("#totalSel").val(total);
		
        $("#colStart").val(colSel);
        $("#colEnd").val(col);
        $("#rowStart").val(rowSel);
        $("#rowEnd").val(row);
        
        $("#QuestionDiv").slideDown();       
	}
    else
    {
        $("#NotGoodDiv").slideDown();
    }
}

function HideQuestionDiv() {
	$("#QuestionDiv").slideUp();
    $("#NotGoodDiv").slideUp();
    $("#DoneDiv").slideUp();
}

function BtnPressColor(row, col){
	if($.inArray("r"+row+"c"+col, takenBoxes)<0){
		$("#r"+row+"c"+col).removeClass('divPix');
		$("#r"+row+"c"+col).addClass('divPixPressed');			
	} else {
		canGo = false;
		$("#r"+row+"c"+col).removeClass('divPix');
		$("#r"+row+"c"+col).addClass('divPixPressedRed');
	}
}

function BtnPressColorReset(row, col)
{
	$("#r"+row+"c"+col).removeClass('divPixPressed');
	$("#r"+row+"c"+col).removeClass('divPixPressedRed');
	$("#r"+row+"c"+col).addClass('divPix');
}

function ResetPixels(){
	canGo = true;
	for(i=0;i<=highestRow;i++) {
		for(j=0;j<=highestCol;j++){
			BtnPressColorReset(i,j);
		}
	}
}

function SelectAll(rowFin, colFin, colFirst, rowFirst){
	if(rowFin>=rowFirst) {
		if(colFin>=colFirst){
			for(i=colFirst;i<=colFin; i++){
				for(j=rowFirst; j<=rowFin; j++){
					BtnPressColor(j,i);
				}
			}
		} else {
			for(i=colFin;i<=colFirst;i++){
				for(j=rowFirst; j<=rowFin; j++){
					BtnPressColor(j,i);
				}
			}
		}
	} else {
		if(colFin>=colFirst){
			for(i=colFirst;i<=colFin; i++){
				for(j=rowFin;j<=rowFirst;j++){
					BtnPressColor(j,i);
				}
			}
		} else {
			for(i=colFin;i<=colFirst;i++){
				for(j=rowFin;j<=rowFirst;j++){
					BtnPressColor(j,i);
				}
			}
		}
	}
}

function showDetailsDiv(){
    $("#messageBox").css("top", ($(window).height() - 280) / 2 + $(window).scrollTop() + "px");
    $("#messageBox").css("left", ($(window).width() - 600) / 2 + $(window).scrollLeft() + "px");
    $("#messageBoxInside").css("top", ($(window).height() - 270) / 2 + $(window).scrollTop() + "px");
    $("#messageBoxInside").css("left", ($(window).width() - 590) / 2 + $(window).scrollLeft() + "px");
    $("#messageBox").show();
    $("#messageBoxInside").show();
    $("#detailsDiv").show();
}

function HideMessageBox(){
    $("#detailsDiv").hide();
}

function SendForm(){
    nume = $("#numeClient").val();
    adresa = $("#emailClient").val();
    website = $("#siteClient").val();
    colSt = $("#colStart").val();
    colFn = $("#colEnd").val();
    rowFn = $("#rowStart").val();
    rowSt = $("#rowEnd").val();
    total = $("#totalSel").val();
    if(nume!="" && adresa!="" && website!="") {
        $.getJSON('/send_form.php?nume='+nume+'&email='+adresa+'&website='+website+'&col_st='+colSt+'&row_st='+rowSt+'&col_fn='+colFn+'&row_fn='+rowFn+'&total='+total);
        HideQuestionDiv();
        $("#DoneDiv").slideDown();
    }
    
}
