// JavaScript Document

var globalAlphabet;
var prevHeight;
var prevWidth;

function updatePayment()
{	

	w=document.getElementById('width').value;
	h=document.getElementById('height').value;
	if(w*h>6)
	{
		var fee = w*h*100*0.5;
		var saving = w*h*100-fee;
		document.getElementById('offer').innerHTML="50% Discount<br>"+"$"+saving+" Saving";
	}
	else
	{
		var fee = w*h*100*0.75;	
		var saving = w*h*100-fee;
		document.getElementById('offer').innerHTML="25% Discount<br>"+"$"+saving+" Saving";
	}
	var payment = fee;
	document.getElementById('totalPayment').value="$"+payment;
	//alert ("payement = " + payment);
}
function addOption(selectbox, text, value)
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

function removeAllOptions(selectbox)
{
    var count;
	var length = selectbox.length;

    for (count = 0 ; count <= length ; count++)
	{		
    	selectbox.remove(0);
    }
}

function availableWidth(alpha, x, y)
{
	var arr = setAlphaVariable(alpha);
	for(var i=x;i<15;i++)
	{
		if(arr[y][i]=='e')
		{
			//alert("checking for X available = "+arr[y][i]+" Y = "+y+" X = "+i);
		}
		else
		{
			break;
		}
	}
	avlblex = i-x;
	return avlblex;
}

function availableHeight(alpha, x, y)
{
	var arr = setAlphaVariable(alpha);
	for(i=y;i<15;i++)
	{
		if(arr[i][x]=='e')
		{
			//alert("checking for Y available = "+arr[i][x]+" Y =  "+i+" X = "+x);
		}
		else
		{
			break;
		}
	}
	avlbley = i-y;
	return avlbley;
}

function setWidth()
{
	//alert("setw -1");
	
	x=document.getElementById('locX').value-1;
	y=document.getElementById('locY').value-1;
	//alert("X = "+x+" Y = "+y);
	w=document.getElementById('width').value-1;
	h=document.getElementById('height').value-1;
	//alert("seWidthvariables ("+x+","+y+","+w+","+h+")");
	clearCells(x,y,prevWidth,prevHeight);
	selectCells();
	//alert("setw 0");	
	
	
	var tmpw=x;
	var tmph=y;
	for(var i=0;i<w;i++)
		tmpw++;
	for(var i=0;i<h;i++)
		tmph++;		
	
	var alpha=document.getElementById('alphabet').value;	
	var minHeight = 16;
	var testHeight;
	
	//alert("setw 1");
	for(var t=x; t<=tmpw; t++)
	{	
		testHeight = availableHeight(alpha,t,y);	
		//alert("available height for "+t+" is "+testHeight);
		if(minHeight > 	testHeight)
			minHeight = testHeight;
	}
	//alert("setw 2");
	removeAllOptions(document.getElementById('height'));
	for(i=1;i<=minHeight;i++)
		addOption(document.getElementById("height"), i, i);	
		
	document.getElementById('height').value=h+1;
	//alert("after seWidth ("+x+","+y+","+w+","+h+")");
	prevWidth = w;
	preHeight = h;
	updatePayment();
	//alert("setw 3");
}
function setHeight()
{
	
	x=document.getElementById('locX').value-1;
	y=document.getElementById('locY').value-1;
	//alert("X = "+x+" Y = "+y);
	w=document.getElementById('width').value-1;
	h=document.getElementById('height').value-1;	
	//alert("seHeightvariables ("+x+","+y+","+w+","+h+")");
	clearCells(x,y,prevWidth,prevHeight);
	selectCells();
	
	tmpw=x;
	tmph=y;
	for(var i=0;i<w;i++)
		tmpw++;
	for(var i=0;i<h;i++)
		tmph++;	
	
	var alpha=document.getElementById('alphabet').value;	
	var minWidth = 16;
	var testWidth;	
	
	for(var t=y; t<=tmph; t++)
	{	
		testWidth = availableWidth(alpha,x,t);
		//alert("available width for "+t+" is "+testWidth);
		if(minWidth > 	testWidth)
			minWidth = testWidth;
	}
	removeAllOptions(document.getElementById('width'));
	for(i=1;i<=minWidth;i++)
		addOption(document.getElementById("width"), i, i);	
	
	document.getElementById('width').value=w+1;
	prevHeight = h;
	prevWidth = w;
	updatePayment();
}

function clearCells(x,y,w,h)
{
	//alert("ClearCells ("+x+","+y+","+w+","+h+")");
	var alpha=document.getElementById('alphabet').value;
	var arr = setAlphaVariable(alpha);
	var tmpw=x;
	var tmph=y;
	for(var i=0;i<w;i++)
		tmpw++;
	for(var i=0;i<h;i++)
		tmph++;	
		
	for(var i=y;i<=tmph;i++)
	{
		for(var j=x;j<=tmpw;j++)
		{	
			if(arr[i][j]=='e'){
				var hreftxt = "'"+alpha+"',"+j+","+i;
				//document.getElementById(alpha+'_'+(i)+'_'+(j)).innerHTML = "<a href=\"javascript:displayForm(\"'\" +alpha+ \"',\" +i+ \",\" +j)\"><img src=\"adsimages/empty.jpg\" border=0></a>";
				//alert("Clearing ("+j+","+i+")");
				document.getElementById(alpha+'_'+(i)+'_'+(j)).innerHTML = "<a href=\"javascript:displayForm("+hreftxt+")\"><img src=\"adsimages/empty.jpg\" border=0></a>";
			}
		}
	}
}

function setAlphaVariable(alpha)
{
	var arr;
	if(alpha=='A')
	{	arr = A;	}
	else if(alpha=='B')
	{	arr = B;	}
	else if(alpha=='C')
	{	arr = C;	}
	else if(alpha=='D')
	{	arr = D;	}
	else if(alpha=='E')
	{	arr = E;	}
	else if(alpha=='F')
	{	arr = F;	}
	else if(alpha=='G')
	{	arr = G;	}
	else if(alpha=='H')
	{	arr = H;	}
	else if(alpha=='I')
	{	arr = I;	}
	else if(alpha=='J')
	{	arr = J;	}
	else if(alpha=='K')
	{	arr = K;	}
	else if(alpha=='L')
	{	arr = L;	}
	else if(alpha=='M')
	{	arr = M;	}
	else if(alpha=='N')
	{	arr = N;	}
	else if(alpha=='O')
	{	arr = O;	}
	else if(alpha=='P')
	{	arr = P;	}
	else if(alpha=='Q')
	{	arr = Q;	}
	else if(alpha=='R')
	{	arr = R;	}
	else if(alpha=='S')
	{	arr = S;	}
	else if(alpha=='T')
	{	arr = T;	}
	else if(alpha=='U')
	{	arr = U;	}
	else if(alpha=='V')
	{	arr = V;	}
	else if(alpha=='W')
	{	arr = W;	}
	else if(alpha=='X')
	{	arr = X;	}
	else if(alpha=='Y')
	{	arr = Y;	}
	else if(alpha=='Z')
	{	arr = Z;	}
	return arr;
}

function overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
//	$("#overlay").css({"height":yScroll +"px", "width":xScroll +"px"});
	document.getElementById('overlay').style.height = yScroll +"px";
	document.getElementById('overlay').style.width = xScroll +"px";
	
}

function displayForm(alpha, x, y)
{
//	alert(x+" "+y);
	globalAlphabet = setAlphaVariable(alpha);
	
	var avlblex, avlbley;
	
	avlblex = availableWidth(alpha,x,y);	
	avlbley = availableHeight(alpha,x,y);
	
	tmpw=x;
	tmph=y;
	for(var i=0;i<avlblex;i++)
		tmpw++;
	for(var i=0;i<avlbley;i++)
		tmph++;
	/*
	for(var i=y;i<tmph;i++)
	{
		for(var j=x;j<tmpw;j++)
		{	
			if(globalAlphabet[i][j]=='e')
				document.getElementById(alpha+'_'+(i)+'_'+(j)).innerHTML = "<img src=\"adsimages/empty.jpg\">";		
		}
	}
	*/
//	alert(arr);
	removeAllOptions(document.getElementById('width'));
	removeAllOptions(document.getElementById('height'));
	for(i=1;i<=avlblex;i++)
		addOption(document.getElementById('width'), i, i);
	
	for(i=1;i<=avlbley;i++)
		addOption(document.getElementById('height'), i, i);
	
	prevWidth = 1;
	prevHeight = 1;
	//alert(x+" "+y+" "+arr[x][y]+" "+avlblex+" "+avlbley);
	
	document.getElementById('alphabet').value=alpha;
	document.getElementById('locX').value=(x+1);
	document.getElementById('locY').value=(y+1);
	document.getElementById('width').value=1;
	document.getElementById('height').value=1;
	document.getElementById('totalPayment').value='$75';
//	alert(x+" "+y);
	selectCells();
	document.getElementById('form_div').style.display='';
		
	
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	
	var toppx = screen.height/2 - 275 ;
	var leftx = screen.width/2 - 200;
	
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){	 // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}

	leftx = w/2 - 200;
	topx = yScrolltop + h/2 - 175;//2-275;
	//alert ("left "+leftx+" top " + topx);

	document.getElementById('form_div').style.top = topx+"px" ;	
	document.getElementById('form_div').style.left = leftx+"px" ;
	
	overlaySize();
	
}

function closeMyDiv(div)
{
	//alert("close called");
	var alpha,x,y,w,h;
	
	alpha=document.getElementById('alphabet').value;
	
	x=document.getElementById('locX').value-1;
	y=document.getElementById('locY').value-1;
	//alert("X = "+x+" Y = "+y);
	w=document.getElementById('width').value-1;
	h=document.getElementById('height').value-1;
	//alert("Width = "+w+" Height = "+h);
	
	var tmpw,tmph;
	tmpw=x;
	tmph=y;
	for(var i=0;i<w;i++)
		tmpw++;
	for(var i=0;i<h;i++)
		tmph++;
		
	//alert("close called befor loop");
//	alert(x+" "+y+" "+w+" "+h+" "+tmpw+" "+tmph);   'A',0,7
	for(var i=y;i<=tmph;i++)
	{
		for(var j=x;j<=tmpw;j++)
		{	
			var hreftxt = "'"+alpha+"',"+j+","+i;
			//document.getElementById(alpha+'_'+(i)+'_'+(j)).innerHTML = "<a href=\"javascript:displayForm(\"'\" +alpha+ \"',\" +i+ \",\" +j)\"><img src=\"adsimages/empty.jpg\" border=0></a>";
			document.getElementById(alpha+'_'+(i)+'_'+(j)).innerHTML = "<a href=\"javascript:displayForm("+hreftxt+")\"><img src=\"adsimages/empty.jpg\" border=0></a>";
		}
	}
	
		//alert("close called after loop");
	
	removeAllOptions(document.getElementById('width'));
	removeAllOptions(document.getElementById('height'));
	document.getElementById(div).style.display='none';
		//alert("close called at end");
	
	document.getElementById('overlay').style.height = 0+"px" ;	
	document.getElementById('overlay').style.width = 0+"px" ;
}

function selectCells()
{
	var alpha,x,y,w,h;
	
	alpha=document.getElementById('alphabet').value;
	
	x=document.getElementById('locX').value-1;
	y=document.getElementById('locY').value-1;
	
	w=document.getElementById('width').value-1;
	h=document.getElementById('height').value-1;
	var tmpw,tmph;
	tmpw=x;
	tmph=y;
	for(var i=0;i<w;i++)
		tmpw++;
	for(var i=0;i<h;i++)
		tmph++;
	//alert(x+" "+y+" "+w+" "+h+" "+tmpw+" "+tmph);
	for(var i=x;i<=tmpw;i++)
	{
		for(var j=y;j<=tmph;j++)
		{
/*			alert(document.getElementById(alpha+'_'+j+'_'+i).innerHTML+"  "+"<a href=\"javascript:displayForm('"+alpha+"',"+i+","+j+")\"><IMG src=\"adsimages/empty.jpg\" border=0></a>");
			alert(document.getElementById(alpha+'_'+j+'_'+i).innerHTML == "<a href=\"javascript:displayForm('"+alpha+"',"+i+","+j+")\"><IMG src=\"adsimages/empty.jpg\" border=0></a>");
/*			if(document.getElementById(alpha+'_'+j+'_'+i).innerHTML == "<a href=\"javascript:displrForm('"+alpha+"',"+j+","+i+")\" ><img src=\"adsimages/empty.jpg\" border='0' ></a>")
			{*/
				document.getElementById(alpha+'_'+(j)+'_'+(i)).innerHTML = "<img src=\"adsimages/select.jpg\">";
			/*}
			else
			{
				alert("Not Available");
				displrForm(alpha, x, y);
				break;
			}*/
		}
	}
}