function ExtensionName_ClickElement(e) {
	//this function is to capture the photo upload functionality
	try {
		 if( document.createEvent ) {
			var evt = e.ownerDocument.createEvent('MouseEvents');
			evt.initMouseEvent('click', true, true, e.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
			e.dispatchEvent(evt);

		 } else if( document.createEventObject ) {
		 	// for internet explorer
			  var evObj = document.createEventObject();
			   //e.fireEvent("onclick",evObj);
			   //e.fireEvent("onclick");
			   //e.fireEvent("click");
			   e.click();
		}
   
	}
	catch(err) {
		alert("evt="+err.description);
  
	 }
 
}

function startDataCapture() {
	 if (startCapture == false) {
		// first check if the user has selected the type of data they wish to capture
		if (document.forms["processdata"].feature_type[0].checked ) {
			// capturing a point
			startCapture = true;
		}
		if (document.forms["processdata"].feature_type[1].checked ) {
			// capturing a line
			startCapture = true;

		}
		if (document.forms["processdata"].feature_type[2].checked ) {
			// capturing a polygon
			startCapture = true;

		}
		if (startCapture == false) {
			alert("Please select whether you want to capture a point, line or polygon");
			return 0;
		}

	} // start capture = false

	if (isGoogleMap) {
		captureDataGM();
	}
	else {
		captureDataOL();
	}
	
	// add the data capture tip span
	addDataCaptureTip();
	
	try {
		if (document.getElementById('feature_type1').checked ) {
			document.getElementById("datacapturetip").onmouseover = tooltip("Click on the map <b>once</b> to draw the point.  <br> <br> Click <b>NEXT</b> when you have finished");
		}
		if (document.getElementById('feature_type2').checked ) {
			document.getElementById("datacapturetip").onmouseover = tooltip("Click on the map at least <b>twice</b> <br> to draw the line.  <br> <br> Click <b>NEXT</b> when you have finished");
		}
		if (document.getElementById('feature_type3').checked ) {
			document.getElementById("datacapturetip").onmouseover = tooltip("Click on the map at least <b>three</b> <br>times to draw the polygon.  <br><br>  Click <b>NEXT</b> when you have finished");
		}
		document.getElementById("datacapturetip").onmouseout = exit();
	}
	catch (e) {
	}
	// put a tooltip on the mouse depending on whether we are capturing a point line or polygon
	
}

function addDataCaptureTip() {
  var ni = document.getElementById('mapinterface');
  var newdiv = document.createElement('div');
  var divIdName = 'tooltip';
  newdiv.setAttribute('id',divIdName);
  ni.appendChild(newdiv);

}

//function removeCoordinate() {
//	// clear all overlays and start again !
//	// don't remove just the one coordinate
//	clearCaptureOverlays();	
//}


function processUndoMapData() {
	clearCaptureOverlays();

}



function tooltip(tip)

{
    if(!document.getElementById('tooltip')) {
    	newelement('tooltip');
    }
    var lixlpixel_tooltip = document.getElementById('tooltip');
    lixlpixel_tooltip.style.background = "#FFFFFF";
    lixlpixel_tooltip.innerHTML = tip;
    lixlpixel_tooltip.style.display = 'block';
    document.getElementById('map-canvas').onmousemove = getmouseposition;
}
function exit()
{
    document.getElementById('tooltip').style.display = 'none';
}

function checkEnterUserLogin(event) {
	if(event.keyCode==13)
	doLogin();
}
function getPhotoName(photostring) {
	var fullpath = photostring.split("\\");
	var photoname = fullpath[fullpath.length - 1];
	return photoname;
}
function setupStep3() {
	
	// generate the content of the form
	// also need to change the content of controlcontent!
	generateForm();


}
function processStep3Map() {
	//alert("process stepm 3 map");
	var processresult = false;
	if (isGoogleMap === true) {
		//alert("google map");
		processresult = processStep3MapGM();
	}
	else {
		processresult = processStep3MapOL();
	}
	//alert(processresult);
	return processresult;
}
	
	
	
function generateForm(){
	minisitename = getMinisiteName();
	// Yang 
	//alert("generate form "+minisitename);
	var processresult = processStep3Map();
	//alert("post process");
	 if (processresult === true)
		{
			setupStep4();	
		}
		else 
		{
			try { 
				exit();
			} catch (e) {}
			
			// make sure there are no data points left from step 3
			try {
				clearCaptureOverlays();
			} catch (e) {}
		}
	// Yang ends
}


function backToStep2() {
	addContentText3 = document.getElementById('controlcontent').innerHTML;
	resetCursor();
	document.getElementById('controlcontent').innerHTML = addContentText2;
}

function backToStep1() {
	// get the content for the first step
	// store the previous values
	addContentText2 = document.getElementById('controlcontent').innerHTML;
	document.getElementById('controlcontent').innerHTML = addContentText1;	
	
	// clear anything that has been drawn on the map
	clearCaptureOverlays();
	
	// also change the listener and the map control cursor
	resetCursor();
	clearMapListeners();
}



function clearAllAddNewValues() {
	doCloseInfoWindow();
	doCloseFormWindow();
	startCapture = false;
	// make sure the date picker is closed
	try {
	  var pickerDiv = document.getElementById(datePickerDivID);
	  pickerDiv.style.visibility = "hidden";
	  pickerDiv.style.display = "none";
	} catch (e) {}
	// reset to the start of the data captuer process
	clearCaptureOverlays(); // this gets rid of any geometry and also of any values in data_capture_pts[]
	// reset the cursor on the map just in case!
	resetCursor();
	// clear all the hidden values
	// then setup the first step again
	setupStep1();
	
}


function submitVideoFile() {
	// check that the video is not a doc or other format
	var theVideo = document.getElementById('videoname').value;
	
	// clear any previous messages:
	document.getElementById('images_container').innerHTML = "";
	
	
	var node = document.getElementById('videoname');
	var check = node.files[0].fileSize;
	if (check > 450000000) {
			alert('Your file is to large to upload. The maximum limit is 400 MB.');
			return;
	}
	var theType = theVideo.substr(theVideo.length - 3,theVideo.length).toLowerCase();
	if (theType != "avi" && theType != "mov" && theType != "wmv" && theType != "mp4" && theType != "mpg") {
		alert("Please select a .mov, .avi, .wmv, .mpg or .mp4 file to upload");
		document.getElementById('videoname').value = "";
		return;
	}
	try   {
		var submitvideo = document.getElementById('videoaction');
		//try {
		//	submitvideo.click();
		//} catch (e) {
		//	// firefox will work but will raise an error as the click() event is not really supported
		//}
	}
	catch (e) {
		alert("submit video"+e.description);
	}
		ExtensionName_ClickElement(submitvideo);
}
function doOnVideoSubmit() {
	//alert(document.getElementById('videoname').value);
	
	var par = window.document;
	var images = par.getElementById('images_container');
	var new_div = par.createElement('div');
	new_div.id = "progressDiv";

	var new_img = par.createElement('img');
	new_img.src = '../images/indicator2.gif';
	//new_img.style.marginLeft = '33px';
	new_img.style.marginTop = '0px';
	//new_img.style.backgroundPosition='center';
	new_div.appendChild(new_img);
	images.appendChild(new_div);

	
	document.getElementById('video_upload_form').target = 'upload_video_target'; //'upload_video_target' is the name of the iframe
				
}


function submitfile() {
	// check that the photo is not a doc or other format
	var thePhoto = document.getElementById('photoname').value;
	var theType = thePhoto.substr(thePhoto.length - 3,thePhoto.length).toLowerCase();
	if (theType != "png" && theType != "jpg" && theType != "JPG" && theType != "PNG") {
		alert("Please select a png or jpg file to upload");
		document.getElementById('photoname').value = "";
		return;
	}
	try   {
		var submitphoto = document.getElementById('action');
		//try {
		//	submitphoto.click();
		//} catch (e) {
		//	// firefox will work but will raise an error as the click() event is not really supported
		//}
	}
	catch (e) {
		alert("submit photo"+e.description);
	}
		ExtensionName_ClickElement(submitphoto);
	//document.getElementById('file_upload_form').submit();
}
function doOnSubmit() {
		//alert("on photo submit");
		var par = window.document;
		var images = par.getElementById('images_container');
		var new_div = par.createElement('div');
		new_div.id = "progressDiv";

		var new_img = par.createElement('img');
		new_img.src = '../images/indicator2.gif';
		//new_img.style.marginLeft = '33px';
		new_img.style.marginTop = '0px';
		//new_img.style.backgroundPosition='center';
		new_div.appendChild(new_img);
		images.appendChild(new_div);

		
		document.getElementById('file_upload_form').target = 'upload_target'; //'upload_target' is the name of the iframe
				
}




function doCloseVideoPreview() {
	//alert("close the preview window");
	theParent = document.getElementById('videowindow').parentNode 
	var olddiv = document.getElementById('videowindow');
	theParent.removeChild(olddiv);

}


/* =====================================*/
/* FUNCTIONS FOR MAP CONTROL THAT DEPEND ON THE MAP TYPE */
function resetCursor() {
	if (isGoogleMap) {
		resetCursorGM();
	}
	else   {
		resetCursorOL();
	}

}

function clearCaptureOverlays() {
	try {
		if (isGoogleMap) {
			clearCaptureOverlaysGM();
		}
		else {
			clearCaptureOverlaysOL();
		}
	} 
	catch (e) {
	}
}
function clearMapListeners() {
	try   { 
		if (isGoogleMap) {
			clearMapClickListenerGM();
		}
		else  {
			clearListenerOL();
		}
	}
	catch (e) {
		// do nothing - listener has been previously cleared
	}
}
	






function processStep2() {
	if (document.forms["processdata"].capture_type[0].checked ) {
		document.forms["processdata"].selected_capture_type.value = "postcode";
	}
	if (document.forms["processdata"].capture_type[1].checked ) {
		document.forms["processdata"].selected_capture_type.value = "drawonmap";
	}
	if (document.forms["processdata"].selected_capture_type.value == "notselected") {
		alert("Please select the way you wish to capture your data");
	}
	else {
		if (document.forms["processdata"].selected_capture_type.value == "postcode") {
			setupStep3Postcode();
		}
		else {
			setupStep3Map();
		}
	}
}

function setupStep1() {
	// first check if there has been any geometry captured
	if (isGoogleMap) {
		if (processStep3MapGM == false ) {
			return;
		}
	}
	else {
		if (processStep3MapOL == false) {
			return;	  	
		}
	}
	// get rid of the tooltip
	try { 
		exit();
	} catch (e) {}
	
	// make sure there are no data points left from step 3
	clearCaptureOverlays();
	clearMapListeners();
	startCapture = false;
}
function backtoStep3Map() {
	clearCaptureOverlays();
	addContentText3 = document.getElementById('controlcontent').innerHTML;
	document.getElementById('controlcontent').innerHTML = addContentText2;	 	
}



function processSaveUserData() {
	if (validateUserData()) {
		//alert("Your data has been submitted to the moderators for approval.\n\n It will appear on the map once the approval process has been completed.");
		document.getElementById('container2').style.left = "-4000px";
		resetDataCapture();
	}
}
function resetDataCapture() {
		var elSel = document.getElementById('Select1[]');
		for (i=elSel.length-1;i=0;i++) {
			elSel.remove(i);
		}
		setupStep1();
		
}

function newelement(newid) {
    if(document.createElement) {
        var el = document.createElement('div');
        el.id = newid;
        with(el.style)
        {
            display = 'none';
            position = 'absolute';
        }
        el.innerHTML = '&nbsp;';
        document.body.appendChild(el);
    }
}
function getmouseposition(e)
{
    if(document.getElementById)
    {
        var iebody=(document.compatMode &&
        	document.compatMode != 'BackCompat') ?
        		document.documentElement : document.body;
        pagex = (isapple == 1 ? 0:(ie5)?iebody.scrollLeft:window.pageXOffset);
        pagey = (isapple == 1 ? 0:(ie5)?iebody.scrollTop:window.pageYOffset);
        mousex = (ie5)?event.x:(ns6)?clientX = e.clientX:false;
        mousey = (ie5)?event.y:(ns6)?clientY = e.clientY:false;

        var lixlpixel_tooltip = document.getElementById('tooltip');
        lixlpixel_tooltip.style.left = (mousex+pagex+offsetx) + 'px';
        lixlpixel_tooltip.style.top = (mousey+pagey+offsety) + 'px';
    }
}

