var xmlHttp;
var xmlDoc;
var isSaving = 1;

function turnOn()
{
   isSaving = 1;
}

function strokeWidth(width) {
	var sym = document.getElementById("widthpicker");
	sym.style.visibility = 'hidden';
	theEditor.setWidth(width);
}

function stopDrawing() {
	if (   (toolSet.activeTool == toolSet.gfxToolSet.polyLine)
			|| (toolSet.activeTool == toolSet.gfxToolSet.freehand)){
		if (toolSet.activeTool.closeShapeArbitrary() > 0)
			return;
	}
}

function postSVG() {
	dojo.debug("clicked postSVG");
	
  if (isSaving==1)
  {
   isSaving = 0;
   var t=setTimeout("turnOn()",3000);	
	// make sure any open lines are closed
	
	stopDrawing();
		
//	var key = dojo.byId("storageKey").value;
	var formstruct2 = dojo.byId("svgPoly");

//	formstruct1.value = key;
	
	svgFile = createSVGHeader();
	svgFile += addSVGGroupStart();
	svgFile += createSVGString();
	svgFile += addSVGGroupEnd();
	svgFile += createSVGFooter();  
  	formstruct2.value = svgFile;

	dojo.debug("svgstatement to submit" + svgFile);
  	
// 	dojo.debug("form ident to submit has:" + formstruct1.name + formstruct1.value);
  	dojo.debug("form poly to submit has:" + formstruct2.name + formstruct2.value);
  	
  	document.theForm.submit();
  	
//	var formstruct1 = dojo.byId("svgId");
//  	document.theReturnForm.submit();
  	}
}

function createSVGHeader() {
 return "<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">";
}

function addSVGGroupStart() {
	return "<g id=\"theshape\">";	
}

function addSVGGroupEnd() {
	return "</g>";
}

function createSVGFooter() {
	return "</svg>";
}

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}
    
function createSVGString() {
	var theSVGString = "";
	for (key in gShapes) {
		theSVGString += gShapes[key].toSVG();
	} 
	return theSVGString;
}

function saveObjectbyPOST() {
    createXMLHttpRequest();
    
    var url = "../../ic/saveshape.jsp" 
//    var url = "p4.imagechef.com/ic/saveshape.jsp" 
//    + "?timeStamp=" + new Date().getTime();
    var svgobj = createSVGString();

  	dojo.debug("POST has:" + svgobj);
    
    xmlHttp.open("POST", url, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");    
    xmlHttp.send(svgobj);
}
    
function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            parseResults();
        }
    }
}

function parseResults() {
    var responseDiv = document.getElementById("serverResponse");
    if(responseDiv.hasChildNodes()) {
        responseDiv.removeChild(responseDiv.childNodes[0]);
    }
    
    var responseText = document.createTextNode(xmlHttp.responseText);
    responseDiv.appendChild(responseText);
}

function loadSVG(symbolPath) {
//	serverPath = "/symbol/temp/sym271b4613c755c137.svg"
//	dojo.debug("server path is: " + serverPath);
	
	var serverPath = "/symbol/preset/shipshape";
	if (symbolPath != "") {
		if (symbolPath.indexOf("temp") == -1) {
			serverPath = "/ic/draw/get-symbol-data.jsp?symid=" + symbolPath;
		} else {
			serverPath = "/symbol/" + symbolPath + ".svg";
		}
	}

//		serverPath = "/symbol/" + symbolPath + ".svg";
// var kw = {
	dojo.io.bind({
		url:	serverPath,
		mimetype: "text/xml",
		load: function(type, xml, e) { 
			dojo.debug("data is:" + xml);
      var polygonArray = xml.getElementsByTagName('polygon');

// 						if (dojo.render.html.ie) {
 							// SVG is a pain for IE 
			var j;
			for (j = 0; j < polygonArray.length; j++) {
				id = "shape_" + (gShapeCounter++);
				gShapes[id] = new imgchf.polyline(id);
				gShapes[id].load(polygonArray[j]);
	    }
			if (bDev)	{        	        	
				var pathArray = xml.getElementsByTagName('path');
				for (j = 0; j < pathArray.length; j++) {
					id = "shape_" + (gShapeCounter++);
					gShapes[id] = new imgchf.path(id);
					gShapes[id].load(pathArray[j]);
				}

				var gArray = xml.getElementsByTagName("g");
				for (j = 0; j < gArray.length; j++) {
					if (gArray[j].getAttribute("id") == "imgchfrect") {
						var gnode = gArray[j];
						
						var rect0 = gnode.getElementsByTagName("rect");
						id = "shape_" + (gShapeCounter++);
						gShapes[id] = new imgchf.rectangle(id, 0, 0, 1, 1);
						gShapes[id].load(gnode);
						
						dojo.debug("Hi There");
//						document.body.innerHTML += html.firstChild.nodeValue;
						
//            dojo.debug("gnode " + gnode.getAttribute("transform"));
//            dojo.debug("gnode " + rect0[0].getAttribute("x"));

//            var rectarray = gnode.childNodes[0];
//            dojo.debug("rect attribute " + rectarray.nodeValue);
					}
				}
			}                    
//				method: "get"
    }       
	});
//           	}
//	dojo.io.bind(kw);
}
