
// latest version of compete
function complete(){
	
	if (otherSugarIsChosen()){
		return checkCompleteWOther();
	}else{
		return checkCompleteWOOther();
	}
}


// check if the sugar is complete if there is no other sugar
function checkCompleteWOOther(){
	if(checkForBranch() == -1){ // first in case it is unbranched sugar
		return checkTableMiddleIsComplete();	
	}else{ // sugar is branched
		var branchedArr = checkAllForBranch();
		var terminalTopArr = getTerminalTopArr();
		if( branchedArr.length != terminalTopArr.length){
			return false;
		}
		for(var i = 0; i<branchedArr.length; i++){
			
			for( var sq = branchedArr[i]; sq < terminalTopArr[i]; sq++ ){
				if( squares[sq].group == -1){
					return false;
				}
			}
		}
		return checkTableMiddleIsComplete();
	}
}

// check if the sugar is complete if there is other sugar
function checkCompleteWOther(){

	if( checkForBranch() == -1 ){ // if other sugar is only in table middle
		return noGapsInTableMiddle();	
	}else{
		return true; // need to change
	}
}

function noGapsInTableMiddle(){
	// check that there is no gaps in table middle
	var firstEmpty = -1; 
	for( var i=6; i >=0; i--){
		if( squares[i].group == -1 ){
			firstEmpty = i;
		}
	}
	if(firstEmpty != -1){
		for( var i=firstEmpty; i <=6; i++){
			if( squares[i].group != -1 ){
				return false;
			}
		}
	}
	return true;
}

// true if table top contains other sugar
function tableTopWithOther(){
	for(i=7;i<=12;i++){
		if(squares[i].group == 0){
			return true;
		}
	}
	return false;
}

// true if table middle contains other sugar
function tableMiddleWithOther(){
	for(i=0;i<=6;i++){
		if(squares[i].group == 0){
			return true;
		}
	}
	return false;
}

function checkTableMiddleIsComplete(){
		var terminal = getTerminalMiddle();
		if(terminal == -1){
			return false;
		}
		for(var i=1; i < terminal; i++ ){
			if(squares[i].group == -1){
				return false;
			}
		}
	return true;	

}





function toggle(targetId) {
	$("#tableChooseGroup").show();
}

// this function returns true if the sugar is simple
function sugarIsSimple(){
	if((squares[0].sub == 2) && (squares[1].src.indexOf("Terminal") > -1)){
		return true;
	} else{
		return false;
	}
}


// this fuction returns true is there is at least one other sugar in both middle and top tables
function otherSugarIsChosen(){
	for(i=0;i<squares.length;i++){
		if(squares[i].group == 0){
			return true;
		}
	}
	return false;
}

function showTableChooseGroup( sqNum){
	var src = squares[sqNum].src;
	var i;
	
	$("#tablePopUp").hide();
	$("#tableChooseGroup").hide();
	unHighlightSqaure();
	
	/*
	 * The following code makes sure that squares leading to this one are filled
	 */
    if (sqNum <= 6) {
        // If this square is on the first row
        for (i = sqNum - 1; i >= 0; i--) {
            // loop through each square to the right
            if (squares[i].src == "" || squares[i].src.indexOf("Terminal") != -1) {
                // return if any of them is empty or terminal
                hideTableChooseGroup();
				return false;
            }
        }
    } else if (sqNum == 7) {
        // If this square is the first on the top row
        if (squares[1].src == "") {
			// return if the square below is empty
			hideTableChooseGroup();
			return false;
		}
    } else if (sqNum > 7) {
        // If this square is any other square on the top row
        if (squares[sqNum - 6].src == "" || squares[sqNum - 6].src.indexOf("Branch") == -1) {
            // if the square below is empty or not branched
            if (squares[sqNum - 1].src.indexOf("Terminal") != -1) {
                // return if the square to the right is terminal
                hideTableChooseGroup();
				return false;
            } else {
                for (i = sqNum - 1; i >= 7; i--) {
                    // loop through each square to the right
                    if (squares[i].src == "") {
                        // return if any of them is empty
                        hideTableChooseGroup();
						return false;
                    }
                    if (squares[i - 6].src.indexOf("Branch") != -1) {
                        // stop looping if at the start of the branch
                        break;
                    }
                }
            }
        }
    }
	
	if(  src.indexOf("white") == -1){
		$("#calcButton").hide();
		$("#tableChooseGroup").show();
		$("#tablePopUp").hide();
		$("#result").hide();
	}
	return true;
}

function hideTableChooseGroup() {
	$("#tablePopUp").hide();
	$("#tableChooseGroup").hide();
}			
			
function showBranchedTable(){
	var branchedArr = checkAllForBranch();
	for(i=0; i<branchedArr.length;i++){

		if( clickedMain == branchedArr[i]){
			return true;
		}
	}
	return false;
} 

// return true if sugar is complete 
/*
function complete(){
	var terminalNum=0;
	for( sq=0;sq<squares.length;sq++){
		var src = squares[sq].src;
		if ( squares[sq].group != -1 && src.indexOf("Terminal") > -1){	
			terminalNum++;
		}			
	}
	var branchedArr = checkAllForBranch();
	if(terminalNum == branchedArr.length + 1){
		return true;
	}
	return false;
} */
 
function updateTopTableValues( ){
	var src = squares[clickedMain].src;
	// if clicked in table middle and choose not branched sugar then update those at the top table to default unless to the right of clicked Main + 5 is a sugar
	if(src.indexOf("Branch") == -1 && clickedMain > 0 && clickedMain < 7 && squares[clickedMain + 5].group == -1){
		for( sq = clickedMain+6; sq <= 12; sq++){
			$("#square" + sq).attr("src", pathToImage + "default.png");
			squares[sq].group = -1;
			squares[sq].sub = -1;
			squares[sq].branched = 0;
			squares[sq].src = '';
		}
	}
} 

function forTestVar(){
	testVar = "in forTestVar function";
}
 
 // function to indicate whether any of the squares after clicked main is not empty. row 1 or 2: 1 to indecate tableMiddle 2 to indicate tableTop
function emptyF(row){
	
	if(row == 1){
		for(sq = clickedMain+1; sq <= 6; sq++){
			if( squares[sq].group != -1){
				return false;
			}
		}
		/*for(sq = 6; sq > clickedMain; sq--){
			if( squares[sq].sub != -1){
				return false;
			}
		}*/
	return true;
	}else if(row == 2){
		for(sq = 12; sq > clickedMain; sq--){
			if( squares[sq].sub != -1){
				return false;
			}
		}
	return true;
	}
}

function firstNonEmpty(row){
	/*if(row == 1){
		for(sq = 6; sq > clickedMain; sq--){
			if( squares[sq].sub != -1){
				return false;
			}
		}
	return true;
	}else*/ 
	if(row == 2){
		for(sq = 12; sq > 7; sq--){
			if( squares[sq].sub != -1){
				return sq;
			}
		}
	return -1;
	}
}
 
function checkForBranch(){
    for (i = 1; i <= 6; i++) {
        var src = squares[i].src;
        if (src.indexOf("Branch") != -1) {
            return i;
        }
        if (src.indexOf("Terminal") != -1) {
            return -1;
        }
    }
    return -1;
}

// return array of sq number of those that are just above branched sugars
function checkAllForBranch(){
	var ret = new Array();
	counter=0;
	for( sq = 1; sq <= 6; sq++ ){
		var src = squares[sq].src;
		if ( src.indexOf("Branch") > -1){
			ret[counter] = sq + 6;	
			counter++;
		}
	}
	return ret;
}
 
function assignHoverN( squareN){
	hoverN = squareN;
}

function show(){
	var coor = GetAbsCoordsForElement("square" + hoverN);
	document.getElementById("big").style.left = coor[0] - 48 + 'px';
	document.getElementById("big").style.top = coor[1] - 210 + 'px';
	var src = $("#square" + hoverN).attr("src");
	$("#big").attr("src", src);
	if(  src.indexOf("white") == -1 && src.indexOf("default") == -1 ){
		document.getElementById("big").style.display = 'inline';
	}
}


function hide(){
	document.getElementById("big").style.display = 'none';
}

 function getTerminalMiddle(){	
	for( sq = 1; sq <=6; sq++ ){
		var src = squares[sq].src;
		if ( src.indexOf("Terminal") > -1){	
			return sq;
		}
	}
	return -1;
 }
 
 function getTerminalTop(){	
	for( sq = 7; sq <=12; sq++ ){
		for( i=0;i<terminalArr.length;i++){
			
			if (squares[sq].src == pathToImage + terminalArr[i] + '.png'){
				return sq;
			}
						
		}
	}
	return -1;
 }
 
  function getTerminalTopArr(){	
	var ret = new Array();
	var counter = 0;
	var src;
	
	for( sq = 7; sq <=12; sq++ ){
		src = squares[sq].src;
		
		if(  src.indexOf("Terminal") != -1 ){
			ret[counter] = sq;
			counter++;
		}
	}
	return ret;
 }
 
 function GetAbsCoordsForElement(elName){
                  var obj=document.getElementById(elName);
				  var arr = new Array();
                  //testfrm.absXcoord.value = getAbsX(obj);
                  //testfrm.absYcoord.value = getAbsY(obj);
				  arr[0] = getAbsX(obj);
				  arr[1] = getAbsY(obj);
				  return arr;
}
            
function getAbsX(obj)
            {
                  var leftOffset = 0;
                  if (obj.offsetParent)
                  {
                        while (obj.offsetParent)
                        {
                              leftOffset += obj.offsetLeft;
                              obj = obj.offsetParent;
                        }
                  }
                  else if (obj.x) //for NN4
                  {
                        leftOffset = obj.x;
                  }
                  return leftOffset;
            }
            
            function getAbsY(obj)
            {
                  var topOffset = 0;
                  if (obj.offsetParent)
                  {
                        while (obj.offsetParent)
                        {
                              topOffset += obj.offsetTop;
                              obj = obj.offsetParent;
                        }
                  }
                  else if (obj.y) // for NN4
                  {
                        topOffset = obj.y;
                  }
                  return topOffset;
            }
			
function highlightSquare(which){
	var highlighter = $("#highlighter");
	if (which != undefined) {
		var square = $("#square" + which);
		var pos = square.offset();
		var width = square.width();
		var height = square.height();
		highlighter.css({
			"left": (pos.left - 10) + "px",
			"top": (pos.top - 10) + "px",
			"width": width + "px",
			"height": height + "px"
		});
	}
	highlighter.show();
}

function unHighlightSqaure() {
	var highlighter = $("#highlighter");
	highlighter.hide();
}

function hideTableChooseGroup(){
    $("#tableChooseGroup").hide();
    if (otherSugarIsChosen()) {
        $("#forUserNotes").show();
    } else {
        $("#forUserNotes").hide();
    }
    if (complete()) {
        if (!otherSugarIsChosen()) {
            $("#calcButton").show();
            document.getNotes.notes.value = ""; // get rid of old notes
        } else {
            $("#saveButton").show();
            $("#addToCartButton").show();
        }
    }
}
