// constants
var g_SystemID=100000000;
var g_PageItem=0, g_PointItem=1, g_LineItem=2;

var AppType_Pedia=1;
var AppType_TextBook=2;

function degreeFromDegree(degree)
{
	return parseInt(degree);
}

function minuteFromDegree(degree)
{
	var minute;
	
	minute=degree-parseInt(degree);
	
	return parseInt(minute*60);
}

function secondFromDegree(degree)
{
	var deg,minute;
	var second;
	
	deg=parseInt(degree);
	minute=parseInt((degree-deg)*60);
	
	second=(degree-deg)*60-minute;
	return parseInt(second*60);
}

function msecondFromDegree(degree)
{
	var msecond;
	msecond=degree-parseInt(degree);
	msecond=msecond*60-parseInt(msecond*60);
	msecond=msecond*60-parseInt(msecond*60);
	
	return parseInt(msecond*60);
}

function degreeFromDMS(degree, minute, second)
{
//	return parseFloat(degree)+parseFloat(parseFloat(minute)/60.0)+parseFloat(parseFloat(second)/3600.0);
	return degree+minute/60.0+second/3600.0;
}

function degreeFromDMSHigh(degree, minute, second, msecond)
{
//	return parseFloat(degree)+parseFloat(parseFloat(minute)/60.0)+parseFloat(parseFloat(second)/3600.0)
//	+parseFloat(parseFloat(msecond)/216000.0);
	return degree+minute/60.0+second/3600.0+msecond/216000.0;
}

function g_isNull(object)
{
	if( object==null || typeof(object)=="undefined" )
		return true;
	return false;
}

function g_leftTrim(str) 
{ 
	return str.replace(/^[ \t\n\r]+/g, "");
}

function g_rightTrim(str) 
{
	return str.replace(/[ \t\n\r]+$/g, "");
}

function g_trim(str) 
{
    return g_rightTrim(g_leftTrim(str));
} 

// assume the format is #rrggbb
function g_getRed(color)
{
	if(color=="" || g_isNull(color))
		return 0;
	var red=color.substr(1,2);
	return parseInt("0x"+red);
}

function g_getGreen(color)
{
	if(color=="" || g_isNull(color))
		return 0;
	var red=color.substr(3,2);
	return parseInt("0x"+red);
}

function g_getBlue(color)
{
	if(color=="" || g_isNull(color))
		return 0;
	var red=color.substr(5,2);
	return parseInt("0x"+red);
}


/////////////////////////////////////////////////////////////
// ui helpers
function g_addOneItem(oSelect, text , index)
{
	var oOption = document.createElement("OPTION");
	oSelect.options.add(oOption);
	oOption.innerText = text;
	oOption.value = index;
}

function g_clearItems(oSelect)
{
	var len=oSelect.length;
	for(var i=0;i<=len;i++)
		oSelect.remove(0);
}

function g_setFocusByValue(oSelection,value)
{
	if(oSelection.length<=0)
		return;
	if(g_isNull(value) || value=="")
		oSelection.selectedIndex=0;
		
	var isFound=false;
	for(var i=0;i<oSelection.length;i++)
	{
		var opt=oSelection.options(i);
		if(!g_isNull(opt))
		{
			if(opt.innerText==value)
			{
				oSelection.selectedIndex=i;
				isFound=true;
				break;
			}
		}
	}
	if(!isFound)
		oSelection.selectedIndex=0;
}



/////////////////////////////////////////////////////////////

function g_addDynamicEntity(map, x,y,text,color, radius,legendName)
{
	var r, g, b;
	
	r=g_getRed(color)/255.0;
	g=g_getGreen(color)/255.0;
	b=g_getBlue(color)/255.0;

	if(g_isNull(legendName) || legendName=="")
		legendName="±κΗ©";
	map.AddPointEntity(x,y,text,legendName, r,g,b, 1);	
}

function g_createPointArray(map)
{
	return map.CreateObject(0);
}

function g_addDynamicLineEntity(map,points,text, color, width)
{
	var r, g, b;
	
	r=g_getRed(color)/255.0;
	g=g_getGreen(color)/255.0;
	b=g_getBlue(color)/255.0;
	
	map.AddLineEntity(points,text,r,g,b,width);
}


var keyStr = "ABCDEFGHIJKLMNOP" +
        "QRSTUVWXYZabcdef" +
        "ghijklmnopqrstuv" +
        "wxyz0123456789+/" +
        "=";

 function encode64(input) 
 {
   input = escape(input);
   var output = "";
   var chr1, chr2, chr3 = "";
   var enc1, enc2, enc3, enc4 = "";
   var i = 0;

   do {
    chr1 = input.charCodeAt(i++);
    chr2 = input.charCodeAt(i++);
    chr3 = input.charCodeAt(i++);

    enc1 = chr1 >> 2;
    enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    enc4 = chr3 & 63;

    if (isNaN(chr2)) {
      enc3 = enc4 = 64;
    } else if (isNaN(chr3)) {
      enc4 = 64;
    }

    output = output + 
      keyStr.charAt(enc1) + 
      keyStr.charAt(enc2) + 
      keyStr.charAt(enc3) + 
      keyStr.charAt(enc4);
    chr1 = chr2 = chr3 = "";
    enc1 = enc2 = enc3 = enc4 = "";
   } while (i < input.length);

   return output;
 }

 function decode64(input) 
 {
   var output = "";
   var chr1, chr2, chr3 = "";
   var enc1, enc2, enc3, enc4 = "";
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   var base64test = /[^A-Za-z0-9\+\/\=]/g;
   if (base64test.exec(input)) 
   {
		return input;
/*		alert("There were invalid base64 characters in the input text.\n" +
			"Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
			"Expect errors in decoding.");
*/			
   }
   
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do 
   {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));

		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;

/*
		output = output + String.fromCharCode(chr1);

		if (enc3 != 64) 
		{
			output = output + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			output = output + String.fromCharCode(chr3);
		}
*/		
		var str="";
		
		str = str+ String.fromCharCode(chr1);

		if (enc3 != 64) 
		{
			str = str + String.fromCharCode(chr2);
		}
		if (enc4 != 64) {
			str = str + String.fromCharCode(chr3);
		}
		
		output=output+str;
		
		chr1 = chr2 = chr3 = "";
		enc1 = enc2 = enc3 = enc4 = "";

   } while (i < input.length);

	return unescape(output);
 }

function g_HTMLEncode(text)
{
	text = text.replace(/&/g, "&amp;") ;
	text = text.replace(/"/g, "&quot;") ;
	text = text.replace(/</g, "&lt;") ;
	text = text.replace(/>/g, "&gt;") ;
	text = text.replace(/'/g, "&#146;") ;

	return text ;
}			

function g_formatText(text)
{
	text=text.replace(/</g,"&lt;");
	text=text.replace(/>/g,"&gt;");
	//text=text.replace(/ /g,"&nbsp;");
	return text.replace(/\"/g,"&quot;");
}

///////////////////////////////////////////
// window functon
function g_openWindowCenter(opener,url,title,width,height)
{
	window.opener=opener;
	var x,y;
	
	x=(screen.width-width)/2;
	y=(screen.height-height)/2;
	
	var pos="left="+x+","+"top="+y+","+"width="+width+","+"height="+height+",";
	window.open (url, title, pos+"titlebar=no, toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, status=no")‘‘
}

function g_openWindowCenterStatus(opener,url,title,width,height)
{
	window.opener=opener;
	var x,y;
	
	x=(screen.width-width)/2;
	y=(screen.height-height)/2;
	
	var pos="left="+x+","+"top="+y+","+"width="+width+","+"height="+height+",";
	window.open (url, title, pos+"titlebar=no, toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, status=yes")‘‘
}