// JavaScript Document
//var newWindow;

var newfileName;
var dept;
var pnum;
var qty;
var orderspecs  = "";
var orderspecs2 = "";

function stripPath() {		// strip out path info
	var fileName;
	if (dept == "print") {									

		fileName = document.getElementById("filenameId").value ;
		
		}else{
		
		fileName = document.getElementById("pictureId").value ;
		
		}
		
	var alength = fileName.length;
	var found = 0;
	
	found = fileName.lastIndexOf(":") ;									// search string for ":"
																		// if none job is done
	if (found == -1) {
    	found = fileName.lastIndexOf("/") ;									// search string for "//"
    	if (found == -1) {
			return;						// no ":" OR NO "/" job is done
		}	
	}
										// if some strip out file name start from the back
	newfileName = fileName.slice(found + 1);										
//<form name='cartadd' method='POST' action='https://store.printallimages.com/servlet/Cart' target = 'mainFrame'>
//<form name='cartadd' method='POST' action='http://store.printallimages.com/servlet/Cart' target = 'mainFrame'>
//<form name='cartadd' method='POST' action='http://storesense2.megawebservers.com/HS3314/Cart.bok' target = '_blank'>

}


function setvalue(form) {

	var name;
//	var orderspecs = "";
	

	 dept = document.getElementById("department").value;		// check which department is adding
	 

	 
		if (dept == "frame") {									
			name = document.getElementById("frameId").value;	// frame department
	 

//			document.getElementsByName("qty")[0].value = 1;
	//		name = document.cartadd.color.options[0].value;
	//		document.getElementsByName("product_no")[0].value =  (parent.topFrame.document.getElementById("part_noId").value * 1) ;

	//		pnum = document.getElementsByName("product_no")[0].value;

			pnum = (document.getElementById("part_noId").value * 1) ;
			// contruct order specifications
			// copy out frame info   
			orderspecs = " " ;
  			orderspecs += "Frame Name: "  + document.getElementById("frameId").value;
			orderspecs += ", ";
			orderspecs +=   "Frame Size: " + document.getElementById("frameSizeId").value;
//	  		document.cartadd.size.options[0].text = orderspecs;				   // size option attribute
//		    document.cartadd.size.options[0].value = orderspecs;  

//			document.cartadd.size.selectedIndex = 0;
//			document.cartadd.size.length = 1;

			orderspecs2 = " " ;
			orderspecs2 += "Matte Size: Top [" + document.getElementById("mattetopId").value + "]";
			orderspecs2 += ", ";
			orderspecs2 += "Left ["    + document.getElementById("matteleftId").value   + "]";
			orderspecs2 += ", ";
			orderspecs2 += "Right ["   + document.getElementById("matterightId").value  + "]";
			orderspecs2 += ", " ;
			orderspecs2 += "Bottom ["  + document.getElementById("mattebottomId").value + "]";
			orderspecs2 += ", " ;


			stripPath();			// strip path out
			orderspecs2 += "Image File Name: " + newfileName;


//		    document.cartadd.color.options[0].text = orderspecs;			   // color option attribute	
//	 	   document.cartadd.color.options[0].value = orderspecs;  

			// make change visible
//			document.cartadd.color.selectedIndex = 0;
//			document.cartadd.color.length = 1;
//			document.cartadd.submit();
	
			return;
		}
		
	
		
}

function makeNewWindow() {
// alert("New Window");	
	if (newWindow && !newWindow.closed) {
		newWindow.close();
	}
	
	newWindow = window.open("","", "status, height=600, width=800, menubar,scrollbars, toolbar, resizable");		// open new window
	setTimeout("subWrite()",200);
}

function subWrite() {
// alert("subWrite");
	// make new window if someone has closed it
	if (newWindow.closed) {
		makeNewWindow();
	}
	
	// bring window to front
	newWindow.focus();
	
	// assemble content for new window
	var newContent = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
	newContent += '<html xmlns="http://www.w3.org/1999/xhtml">';
	newContent += '<head>';
	newContent += '<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />';
	newContent += '<title>aaadd new window</title>';
	newContent += '</head>' ;

	
	newContent += '<body  bgcolor="#FFFFCC" text="#000000" onload = "document.cartadd.submit();">' ;
	newContent += "<form name='cartadd' method='POST' action='https://store.printallimages.com/servlet/Cart' target = '_self'>";
	newContent += '<input type="hidden" name="smode" value="add">';
	newContent += '<input type="hidden" name="product_no" value="';
	newContent += pnum ;
	newContent += '">';
	newContent += '<input type="hidden" name="basePrice" value="200">';

	newContent += '<table border="0" cellpadding="2" cellspacing="2">';
	newContent += '<tr>';

	newContent += '<td bgcolor="#999999">';
	newContent += '<div align= "center" >';
	newContent += 'ADDING YOUR ITEM TO SHOPPING CART -- PLEASE WAIT';
	newContent += '</div>';
	newContent += '</td>';


 	newContent += '</table>';

	newContent += '<p>';
	newContent += '<font face="Arial, Helvetica, sans-serif" color="black" size="2"><input type="hidden" name="qty" value="1" size="3"></font>';
	newContent += '</p>';

	newContent += '<p>';

	//		document.cartadd.size.selectedIndex = 0;
//		document.cartadd.size.length = 1;
	newContent += '<font face="Arial, Helvetica, sans-serif" color="black" size="2">';
	newContent += '<select name="color"  >';
		newContent += '<option id = "orderText" ';
		newContent += 'selected value="';
		newContent += orderspecs;
		newContent += '">';
		newContent += orderspecs;
		newContent += '</option>';
	newContent += '</select>';
	newContent += '</font>';
			newContent += '&nbsp;&nbsp;';
	
	
		newContent += '<font face="Arial, Helvetica, sans-serif" color="black" size="2">';
		newContent += '<select name="size">';

// alert("orderspecs2: " + orderspecs2);

			newContent += '<option value="Qty">Qty</option>';
			newContent += '<option  selected value="';
			newContent += orderspecs2;
			newContent += '">';
			newContent += orderspecs2;
			newContent += '</option>';
		newContent += '</select>';
		newContent += '</font>';
	
// alert("newContent: " + newContent);
	
	

		newContent += '</p>';

//		newContent += '<p><input type="submit"  border="0" alt="Buy It"></p>';
		newContent += '</form>';

		newContent += '</body>';
		newContent += '</html>';
		
		// write HTML to new window document
		newWindow.document.write(newContent);
		newWindow.document.close();						// close layout stream

}



function hide(lay) {
//alert("hide");
		parent.topFrame.document.getElementById("Layer31").style.visibility = "hidden";
		parent.topFrame.document.getElementById("Layer32").style.visibility = "hidden";
		parent.topFrame.document.getElementById("Layer33").style.visibility = "hidden";
		parent.topFrame.document.getElementById("Layer34").style.visibility = "hidden";
//		parent.topFrame.document.getElementById("Layer31").style.zIndex = "0";

}

function show(lay) {
//alert("show");
		parent.topFrame.document.getElementById(lay).style.visibility = "visible";
}

	var pframe = 0;		// frameing name pointer into array - global
	var pgroup = 0;		// pointer in framing group - global
	var pframeMult = 0;	// frame pointer multiplier in framingArray
	var fName;
	var pNum;
	
	
//	var isIE4 = ((navigator.appName.indexOf("Microsoft") == 0 && parseInt(getIEVersion()) >=4));
//	var isW3C = (document.documentElement) ? true : false;

//function getIEVersion () {

//	var ua = navigator.userAgent;
//	var IEoffset = ua.indexOf("MSIE ");
//	return parseFloat(ua.substring(IEofffset+5, ua.indexOf(";", Ieoffset)));
//}

// custom object constructor

function showPicture(){
	var pname;
	var purl;
	var net; 
	
	purl = document.images["picture"].src;
	pname = document.getElementById("pictureId").value;
	
// alert("pname: " + pname);
	
	net = navigator.userAgent;	//check for Macintosh Netscape 7.01
// alert("net: " + net);

	if (net == "Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01") {
		var regexp = /:/g;
		pname = pname.replace(regexp,"/");	
		regexp = / /g;
		pname = pname.replace(regexp,"%20");
		pname = "/" + pname;	
	}
	 document.images["picture"].src = "file://" + pname;
	// document.images["picture"].src =  "flower_hand_2.jpg";
	 
// alert("document.images: " + document.images["picture"].src);

//		setTimeout("showtime()",12000);

}

function showtime()  {
	 document.images["picture"].src = "file://" + pname;
}
function loadView() {
	var wz;
	wz = 1;
	document.form1.width.focus();
	document.form1.width.select();
}

function buildFrame() {
	var height;
	var width;
	var matte_top;
	var matte_left;
	var matte_right;
	var matte_bottom;
	var offset_top;
	var offset_left;
	var scale;
	
	offset_top = 82;
	offset_left = 10;
	var twidth;
	var theight;
	var fsize;
	var pswitch = 1;	//price calculation switch 0 = real calc 1 = bracketed calc
	
	scale = 10;				//normal scale
	var tbottom;
	

	
//alert(navigator.appName);
//alert(navigator.appName.indexOf("Microsoft"));	
//alert(navigator.userAgent);
	document.getElementById("previewlabel").style.visibility = "hidden";
	tbottom = document.getElementById("Layer66").offsetTop;
// alert("tbottom: " + tbottom);
	//offset_top = tbottom;

	width  = document.getElementById("widthId").value ;
	height = document.getElementById("heightId").value ;
	matte_top    = document.getElementById("mattetopId").value ;
	matte_left   = document.getElementById("matteleftId").value ;
	matte_right  = document.getElementById("matterightId").value ;
	matte_bottom = document.getElementById("mattebottomId").value ;



	fsize = fArray[pframe + pframeMult].size;
	fName = fArray[pframe + pframeMult].frameing;						// record frame name
	document.getElementById("frameId").value  = fName;

	
	
	var pname;
	var purl; 
	var price;
	var bprice;


	
	// set frame picture
	purl = document.images["framing"].src;
	pname = fArray[pframe + pframeMult].largeImage ;
	document.images["framing"].src =  pname;
	


	twidth  = (width * 1)  + (matte_left * 1) + (matte_right * 1)   ;				// calculate finished frame size
	theight = (height * 1) + (matte_top * 1)  + (matte_bottom * 1)  ;

	document.getElementById("frameSizeId").value = "" + format(twidth,2)  + "x" + format(theight,2);	// record frame size
	

	
	if (twidth > 70.00){																			// check for maximum frame width
		alert("Frame Width Exceeds Max Frame Width of 70.00 inches - Please choose an other frame width");
		loadView();
	//	setTimeout("loadView()" ,1000);
		return;

	}
	
	if (theight > 70.00){																			// check for maximum frame width
		alert("Frame Height Exceeds Max Frame Height of 70.00 inches - Please choose an other frame height");
		loadView();
	//	setTimeout("loadView()" ,1000);
		document.form1.height.focus();
		document.form1.height.select();
		return;

	}
		var area ;
		area  = (twidth * theight * 1.00);	
														// area in square inches
	var x;
	var y;
	var alength;
	

	if (pswitch == 0) {
		bprice = fArray[pframe + pframeMult].basePrice;			// real price calculation
		price  = area * bprice;
	
	}else{														// bracketed price calculation
		alength = (fPriceArray.length-1);						// number of price brackets
		
	
		for (x = 0; x <= alength; x++) {
		
	
			if(area <= (fPriceArray[x].area ) * 1.00) {
			

				if(fArray[pframe + pframeMult].type == "metal") {				// whitch type of frame
					price = fPriceArray[x].metal;
					pNum  = fPriceArray[x].p_metal;							// record part no
					document.getElementById("part_noId").value  = pNum;				
//					pNum = x;
					
				}else{
					price = fPriceArray[x].wood;
					pNum  = fPriceArray[x].p_wood;							// record part no
					document.getElementById("part_noId").value  = pNum;			
//		    		pNum = x;
			}
				break;
			}
		}

	}


	price = format(price,2)

	document.getElementById("priceId").value = "" + price;						// record price


	
	twidth  = (width * 1)  + (matte_left * 1) + (matte_right * 1)  + (fsize*2);
	theight = (height * 1) + (matte_top * 1)  + (matte_bottom * 1) + (fsize*2);
	

		var check;
	for (x = 1; x <= 200; x++) {
			scale = scale * (100-((theight - 24) * 0.10))/100 ;	
			check = theight * scale;
		if(check <= 240) {
			for (y = 1; y <= 200; y++) {
				scale = scale * (100-((twidth - 24) * 0.10))/100 ;	
				check = twidth * scale;
				if(check <= 720) {
					break;
				}
			}
			break;
		}
	}

//scale  = parseInt(scale);

	
	//matte_bottom = matte_bottom *scale;
	//matte_top    = matte_top    * scale;
	//matte_left   = matte_left   * scale;
	//matte_right  = matte_right  * scale;
	//width  = width  * scale;
	//height = height * scale;
	//fsize  = fsize  * scale;
	
	matte_bottom = matte_bottom *scale;
//	matte_bottom = rUpDown(matte_bottom);

	matte_top = matte_top    * scale;
	//matte_top = rUpDown(matte_top);

	matte_left   = matte_left   * scale;
	//matte_left = rUpDown(matte_left);

	matte_right  = matte_right  * scale;
	//matte_right = rUpDown(matte_right);

	width  = width  * scale;
	//width = rUpDown(width);

	height = height * scale;
//	height = rUpDown(height);
	
	fsize  = fsize  * scale;
	//fsize = rUpDown(fsize);

	 var w;
	 var h;
	 var l;
	 var t;

offset_left = (760 - (width + matte_left + matte_right + fsize + fsize) )/ 2;

// image 
l = (offset_left + matte_left + (fsize/1)) ;
l = rUpDown(l);		//round number

document.getElementById("Layer1").style.left = "" + l + "px" ;
t = (offset_top  + matte_top + (fsize/1)) ;
t = rUpDown(t);		//round number

document.getElementById("Layer1").style.top  = "" + t + "px" ;
width = rUpDown(width);		//round number

document.images["picture"].width  = width ;
height = rUpDown(height);		//round number

document.images["picture"].height = height;
document.images["picture"].border = 1;

// document.getElementById("Layer1").style.left = "" + (offset_left + matte_left + (fsize/1)) + "px" ;
// document.getElementById("Layer1").style.top  = "" + (offset_top  + matte_top + (fsize/1)) + "px" ;
// document.images["picture"].width  = width ;
// document.images["picture"].height = height;
// document.images["picture"].border = 1;

// matte border
l = (offset_left + (fsize/1));
l = rUpDown(l);		//round number
l = l + 1;

document.getElementById("Layer2").style.left = "" + l + "px" ;
t = (offset_top + (fsize/1));
t = rUpDown(t);		//round number
t = t + 1;

document.getElementById("Layer2").style.top  = "" + t + "px" ;
w = (width ) + (matte_left + matte_right);
w = rUpDown(w);		//round number

document.images["matte"].width  =  w ;
h = (height) + (matte_top + matte_bottom);
h = rUpDown(h);		//round number

document.images["matte"].height = h ;
// document.images["matte"].border = 1;

//document.getElementById("Layer2").style.left = "" + (offset_left + (fsize/1)) + "px" ;
//document.getElementById("Layer2").style.top  = "" + (offset_top + (fsize/1)) + "px" ;
//document.images["matte"].width  = (width ) + (matte_left + matte_right);
//document.images["matte"].height = (height) + (matte_top + matte_bottom);
//document.images["matte"].border = 1;

// frame border
document.getElementById("Layer3").style.left = "" + (offset_left ) + "px" ;
document.getElementById("Layer3").style.top  = "" + (offset_top )  + "px" ;
 
 w = (width ) + (matte_left + matte_right + (fsize*2));
 w = rUpDown(w);		//round number
 
document.images["framing"].width  = "" + w;
// document.images["framing"].width  = (width ) + (matte_left + matte_right + (fsize*2));

h = (height) + (matte_top + matte_bottom + (fsize*2));


h = rUpDown(h);		//round number

document.images["framing"].height = h;
// document.images["framing"].height = (height) + (matte_top + matte_bottom + (fsize*2));
 document.images["framing"].border = 1;
 
 w = parseInt(w);
 //alert("w: " + w)
 
}

function rUpDown(h) {

	var dif;
	var intx;
	var carry = 0;
	var mult = 10;
	var digit;
	

	intx = parseInt(h);
	dif = h-intx;						// .123456
	dif = dif * 1000000;
	dif = parseInt(dif);
	for (i = 1; i <= 6; i++ ) {
		dif = dif/10;
									// isolate last right digit
		digit = dif - (parseInt(dif));
		if(digit < .5) {
			dif = parseInt(dif);
		}else{
			dif = parseInt(dif) + 1;
		}

	}
	if(dif < .5) {
		return intx;
	}else{
		return intx + 1;	
	}
}

function pickFrame() {


  document.getElementById("Layer4").style.visibility = "visible";
  document.getElementById("Layer5").style.visibility = "visible";


}

function hmap(quad) {
	var pname;
	
	
	pframe = (quad *1) ;		//record globbaly frame filepointer into array 
	pname = fArray[pframe + pframeMult].largeImage ;		//get frame file name
	
	fName = fArray[pframe + pframeMult].frameing;						// record frame name
	document.getElementById("frameId").value  = fName;

//	pNum = fArray[pframe + pframeMult].partNo;							// record part no
//	document.getElementById("part_noId").value  = pNum;		

	//pname = frameingArray[pframe-1].largeImage ;
	document.images["framing"].src =  pname;
	document.getElementById("Layer4").style.visibility = "hidden";
	document.getElementById("Layer5").style.visibility = "hidden";
	buildFrame();
}

function hnav(step) {
	var fileName;
	var glength;
	
	glength = fgArray.length;
	if(step == -2) {						// -2 means previous
		if(pgroup > 0) {
			pgroup = pgroup -1;				// count down pointer
			pframeMult = pframeMult - 6;	// bump frame pointer multiplier		
		}
	}
	if(step == -1) {						// -1  means next
		if(pgroup < (glength-1)) {
			pgroup = pgroup + 1;			// count up pointer
			pframeMult = pframeMult + 6;	// bump frame pointer multiplier		
	}
	}
//	pgroup = step							// record pointer into group array globally
fileName = fgArray[pgroup].file_name;
	document.images["selectFrame"].src =  fileName;		// bring in newe group file

}
function format(expr,decplaces) {			
											// convert number to tring 
											// add decimal point
											// x number of zero after decimal point
											// raise incoming value by power of 10 times the
											// number of decimal places; round to an integer; convert to string

	var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
											// pad small value strings with zeros to the left  of rounded number
	while (str.length <= decplaces) {
		str = "0" + str;
	}										
											// establish location of decimal point
	var decpoint = str.length - decplaces;
											// assemble final result from: 
											// (a) the string up to the position of the decimal point;
											// (b) the decimal point;
											// (c) the balance of the string;
											// return finished product
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);																					
}

function addFraming() {
	var ff;
	
alert("framing");
ff = parent.mainFrame.location.href ;

alert(ff);

//	parent.mainFrame.location.replace('clean.html') ;
	
alert("framing 2");
	parent.mainFrame.location.href = 'add_framing.html';

ff = parent.mainFrame.location.href ;
alert("after add" + ff);}

function addcart() {

	setvalue(this.form);
	makeNewWindow();
//	subWrite();
}
