// JavaScript Document

function mapUpload(){
	
	var mapId=document.getElementById("mapId").value;
	var mapUploadStatus=document.getElementById("mapUploadStatus").value;
		
	if(mapId=="null"){	
			if(mapUploadStatus=="File Upload Successful"){
				var uploadFile=confirm("You have already uploaded map image for new map point. Do you really want to upload new image?");
				if (uploadFile==true){
					//refillMapEntry();
					document.uploadMapForm.submit();
				}
			}
		
			else{
				//refillMapEntry();
				document.uploadMapForm.submit();		
			}
	}
	
	else{
		var uploadFile=confirm("Do you really want to upload new map image for this map point?");
		if (uploadFile==true){
			//refillMapEntry();
			document.uploadMapForm.submit();
		}
	}
}

//-----------------------------------------------------------------------------------------

function monitorUpload(){
	
	var monitorId=document.getElementById("monitorId").value;
	var monitorUploadStatus=document.getElementById("monitorUploadStatus").value;
		
	if(monitorId=="null"){	
			if(monitorUploadStatus=="File Upload Successful"){
				var uploadFile=confirm("You have already uploaded files for new monitor. Do you really want to upload new files?");
				if (uploadFile==true){
					//refillMonitorEntry();
					document.uploadMonitorForm.submit();
				}
			}
		
			else{
				//refillMonitorEntry();
				document.uploadMonitorForm.submit();		
			}
	}
	
	else{
		var uploadFile=confirm("Do you really want to upload new files for this monitor?");
		if (uploadFile==true){
			//refillMonitorEntry();
			document.uploadMonitorForm.submit();
		}
	}
}

//-----------------------------------------------------------------------------------------

function locationUpload(){
	var locationId=document.getElementById("locationId").value;
	var locationUploadStatus=document.getElementById("locationUploadStatus").value;
		
	if(locationId=="null"){	
			if(locationUploadStatus=="File Upload Successful"){
				var uploadFile=confirm("You have already uploaded files for new monitor location. Do you really want to upload new files?");
				if (uploadFile==true){
					//refillLocationEntry();
					document.uploadForm.submit();
				}
			}
		
			else{
				//refillLocationEntry();
				document.uploadForm.submit();		
			}
	}
	
	else{
		var uploadFile=confirm("Do you really want to upload new files for this monitor location?");
		if (uploadFile==true){
			//refillLocationEntry();
			document.uploadForm.submit();
		}
	}
}

//-----------------------------------------------------------------------------------------

function submitMap(){
		var mapId=document.getElementById("mapId").value;
		
		if(mapId=="null"){	
			var mapUploadStatus=document.getElementById("mapUploadStatus").value;
				if(mapUploadStatus=="File Upload Successful"){
					document.mapPointForm.submit();
				}
				else{
					alert("Upload the images/maps to enable new map point entry.");
				}
		}
		
		else{
			var temp=confirm("Do you really want to save the changes for this map point?");
			if (temp==true){
				document.mapPointForm.submit();
			}
			
		}
}

//-------------------------------------------------------------------------------

function submitMonitor(){
		
		var monitorId=document.getElementById("monitorId").value;
		
		if(monitorId=="null"){	
			var monitorUploadStatus=document.getElementById("monitorUploadStatus").value;
				if(monitorUploadStatus=="File Upload Successful"){
					document.monitorForm.submit();
				}
				else{
					alert("Upload the images/maps to enable new monitor entry.");
				}
		}
		
		else{
			var temp=confirm("Do you really want to save the changes for this monitor?");
			if (temp==true){
				document.monitorForm.submit();
			}
			
		}
}

//-------------------------------------------------------------------------------

function submitLocation(){
		
		var locationId=document.getElementById("locationId").value;
		
		if(locationId=="null"){
		
			var locationUploadStatus=document.getElementById("locationUploadStatus").value;
				if(locationUploadStatus=="File Upload Successful"){
					document.monitorLocationForm.submit();
				}
				else{
					alert("Upload the images/maps to enable new monitor location entry.");
				}
		}
		
		else{
			var temp=confirm("Do you really want to save the changes for this monitor location?");
			if (temp==true){
				document.monitorLocationForm.submit();
			}
			
		}
}

//-------------------------------------------------------------------------------

function setMonitorId(){
	document.getElementById("monitorId").value=document.getElementById("serialNo").value;
}

//-------------------------------------------------------------------------------

function setSerialNo(){
	if(document.getElementById("serialNo")!=null){
		document.getElementById("serialNo").value=document.getElementById("monitorId").value;;
	}
}

//-------------------------------------------------------------------------------

function setDownloadPeriod(){
	document.getElementById("downloadPeriod").value=document.getElementById("downloadPeriodId").value;
}

//-------------------------------------------------------------------------------

function setDownloadPeriodId(){
	document.getElementById("downloadPeriodId").value=document.getElementById("downloadPeriod").value;
}
//-------------------------------------------------------------------------------

function setMonitorType(){
	document.getElementById("type").value=document.getElementById("typeId").value;
}

//-------------------------------------------------------------------------------

function setMonitorTypeId(){
	document.getElementById("typeId").value=document.getElementById("type").value;
}


//-------------------------------------------------------------------------------

function verifyDate(field){
	var dt=document.getElementById(field).value;

	var dd=dt.substring(0,2);
	var mm=dt.substring(3,5);
	var yyyy=dt.substring(6,10);
	var separator1=dt.substring(2,3);
	var separator2=dt.substring(5,6);
	
	var today = new Date();
	var dt0 = new Date();
	dt0.setFullYear(yyyy, mm-1, dd);
		
	var isDateFormat=/^(\d{2})["\/"](\d{2})["\/"](\d{4})$/.exec(dt);
	
	if( (dt.length>0) && ( (!isDateFormat) || (!isValidDate(dd, mm, yyyy)) ) ){
		alert("Invalid Date. Enter Date in the format 'dd/mm/yyyy'.");
		document.getElementById(field).value="";
	}
	else if(today<dt0){
		alert("Invalid Date. Date cannot be latter than today.");
		document.getElementById(field).value="";
	}
	else if(!datesCompared()){
		alert("'Removed Date' cannot be earlier than 'Installed Date'.")
		document.getElementById("dateRemoved").value="";
	}
}

//-------------------------------------------------------------------------------

function isValidDate(day, month, year) {
		
		if (month < 1 || month > 12) {
            return false;
        }
        if (day < 1 || day > 31) {
            return false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) &&
            (day == 31)) {
            return false;
        }
        if (month == 2) {
            var leap = (year % 4 == 0 &&
               (year % 100 != 0 || year % 400 == 0));
            if (day>29 || (day == 29 && !leap)) {
                return false;
            }
        }
        return true;
    }
	
//-------------------------------------------------------------------------------

function datesCompared(){
	date1=document.getElementById("dateInstalled").value;
	date2=document.getElementById("dateRemoved").value;

	var dt1=new Date();
	var dt2=new Date();
	
	dt1.setFullYear(date1.substring(6,10), date1.substring(3,5)-1, date1.substring(0,2));
	dt2.setFullYear(date2.substring(6,10), date2.substring(3,5)-1, date2.substring(0,2));
	
	if(date2.length==0){
		return true;
	}
	else if (dt1 <= dt2){
		return true;
	}
	else{
		return false;
	}
}

//-------------------------------------------------------------------------------

