var newWindow;
function MakeArray(n) {
   	this.length=n; 
   	for(var i=1; i<=n; i++) {
		this[i]=0
   	}
   	return this
}
function openWindow(url, windName, nWidth, nHeight){
	screenWidth = screen.availWidth; 
	screenHeight = screen.availHeight; 
	winTop = (screen.availHeight - nHeight) / 2; 
	winLeft = (screen.availWidth - nWidth) / 2; 
	var nProperty = "scrollbars=no,menubar=no,status=yes,toolbar=no,resizable=no,titlebar=no,alwaysRaised=yes,width="+(nWidth)+",height="+(nHeight)+",top="+(winTop)+",left="+(winLeft);
	OLTdgWindow = window.open(url, windName, nProperty)
	OLTdgWindow.focus();
}
function openWindow2(url, name){
	newWindow = window.open(url,name,"scrollbars=yes,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes,width=780,height=460");
	newWindow.focus();
}

function openWin(url){
	newWindow = window.open(url);
	newWindow.focus();
}

function openWindow4(url, name, nWidth, nHeigth){
	screenWidth = screen.width; 
	screenHeight = screen.height; 
	
	if (screenHeight <= 600) {
		winTop = 0;
		winLeft = 0;
	  	var nProperty = "scrollbars=no,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes,left="+(winLeft)+",top="+(winTop)+",width="+(screen.availWidth-10)+",height="+(screen.availHeight-28);		
	} else {
		winTop = (screen.availHeight-28 - nHeigth) / 2; 
		winLeft = (screen.availWidth-10 - nWidth) / 2; 	
		var nProperty = "scrollbars=no,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes,left="+(winLeft)+",top="+(winTop)+",width="+(nWidth)+",height="+(nHeigth);
	}
					 
	newWindow = window.open(url,name,nProperty);
	if(document.layers){ 
		if (screen.height == 600)
			newWindow.resizeTo(screen.availWidth-(newWindow.outerWidth-newWindow.innerWidth),screen.availHeight-(newWindow.outerHeight-newWindow.innerHeight)); 
	}		   
	newWindow.focus();
}

function openWindow3(url){
	cWindow = window.open(url,"thewindow3","scrollbars=yes,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes,width=700,height=460");
	cWindow.focus();
}

function openWindow5(url, windName, nWidth, nHeight){
	screenWidth = screen.availWidth; 
	screenHeight = screen.availHeight; 
	winTop = (screen.availHeight - nHeight) / 2; 
	winLeft = (screen.availWidth - nWidth) / 2; 
	var nProperty = "scrollbars=yes,menubar=no,status=yes,toolbar=no,resizable=no,titlebar=no,alwaysRaised=yes,width="+(nWidth)+",height="+(nHeight)+",top="+(winTop)+",left="+(winLeft);
	OLTdgWindow = window.open(url, windName, nProperty)
	OLTdgWindow.focus();
}

function openWindow6(url, windName, nWidth, nHeight){
        screenWidth = screen.availWidth; 
        screenHeight = screen.availHeight;
        winTop = (screen.availHeight - nHeight) / 2;
        winLeft = (screen.availWidth - nWidth) / 2;
        var nProperty = "scrollbars=yes,menubar=no,status=no,toolbar=no,resizable=no,titlebar=no,alwaysRaised=yes,width="+(nWidth)+",height="+(nHeight)+",top="+(winTop)+",left="+(winLeft);
        OLTdgWindow = window.open(url, windName, nProperty)
        OLTdgWindow.focus();
}

function helpWindow(url){
	hWindow = window.open(url,"thewindow4", 'scrollbars=1,menubar=no,status=no,toolbar=noresizable=no,width=355,height=430,titlebar=no,alwaysRaised=yes, left=330,top=2,screenX=330,screenY=2');
	hWindow.focus();
}
function helpWindow2(url, name){
	hWindow = window.open(url,name,'scrollbars=1,menubar=no,status=no,toolbar=noresizable=no,width=355,height=430,titlebar=no,alwaysRaised=yes, left=330,top=2,screenX=330,screenY=2');
	hWindow.focus();
}
function symbolWindow(url){
	aWindow = window.open(url,'symbolWindow', 'scrollbars=1,menubar=no,width=380,height=140,titlebar=no,alwaysRaised=yes, left=0,top=0,screenX=0,screenY=0');
	aWindow.focus();
}
function dealWindow(url){
		dWindow = window.open(url,"DealData", 'location=0,width=550,height=600,scrollbars=yes');
		dWindow.focus();
}
function closeWindow(url, name){
		openWindow2(url,name);
		newWindow.close();
}
function closeAllWindows(){
	closeWindow("","RTQWindow");
	closeWindow("","TradeWindow");
}
function isNumber(inputVal, allownegative, allowdecimal) {
	oneDecimal = false
	inputStr = "" + inputVal

	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			if (allownegative==true) 
				continue
			else
				return false
		}

		if (oneChar == "." && !oneDecimal) {
			if (allowdecimal==true) {
				oneDecimal = true
				continue
			}
			else
				return false
		}

		if (oneChar < "0" || oneChar > "9") {
			return false
		}
   }
   return true
}

function isDate(datestr,field1,fname) { 
		 var MinYear = 0, MaxYear = 99; 
		 var MinCent = 0, MaxCent = 99; 

		 var dateOk = false; 
		 var leap=false; 
		 var parts = datestr.split('/'); 
		 var months = new Array(31,28,31,30,31,30,31,31,30,31,30,31); 

		 var cc = 0; 
		 var yy = parseInt(parts[2]); 

	 var mm = parseInt(parts[1]); 
		 var dd = parseInt(parts[0]); 
		 var ccyy = yy; 
		
	 if(parts[1] == "08") { mm = 8 }
	 if(parts[1] == "09") { mm = 9 }
	 if(parts[0] == "08") { dd = 8 }
	 if(parts[0] == "09") { dd = 9 }

 
		 if (parts[2].length == 4) {
			cc = parts[2].substring(0,2); 
			yy = parts[2].substring(2,4); 
		 } 
		 else { 
			if (ccyy >= 20 ) ccyy +=1900; 
			else ccyy += 2000; 
		 } 

		 if ((cc >= MinCent && cc <= MaxCent) && (yy >= MinYear && yy <= MaxYear) &&  (mm >= 1 && mm
	  <= 12)) { 
			if (mm == 2)  { 
			   if ( (ccyy % 4 != 0) || (ccyy % 100 == 0 && ccyy % 400 != 0) ) leap = false; 
			   else leap = true; 

			   months[1] += leap; 
			} 
		  if (dd >= 1 && dd <= months[mm-1]) dateOk = true; 
		 } 
		 if (dateOk) CurrentDateObject = new Date(ccyy,parseInt(mm)-1,dd,0,0,0); 
	 if (!dateOk)
	 {
			 alert("Invalid date format for " + fname + ".")
			 field1.focus()
	 }
	 return dateOk; 
	  } 

function checkfield(field1, type1, nlen, chknull, fname) {
	var   isvalid =true
	
	if (chknull) {
		 if (field1.value == null || field1.value == "") {
			 alert(fname + " requires a value.")
			 isvalid=false
		 }
	}

	strVal=field1.value;
	if (nlen != 0 && isvalid==true) {		  
		 if (strVal.length < nlen) {
			 alert(fname + " minimum " + nlen + " of character.")
			 isvalid=false
		 }
	}


	if (type1 == "n-" && isvalid==true) {
		 if (!isNumber(field1.value, true, true)) {
			 alert("Invalid number for " + fname)
			 isvalid=false
		 }
	}

	if (type1 == "n" && isvalid==true) {
		 if (!isNumber(field1.value, false, true)) {
			 alert("Invalid positive number for " + fname)
			 isvalid=false
		 }
	}

	if (type1 == "i-" && isvalid==true) {
		 if (!isNumber(field1.value, true, false)) {
			 alert("Invalid integer for " + fname)
			 isvalid=false
		 }
	}

	if (type1 == "i" && isvalid==true) {
		 if (!isNumber(field1.value, false, false)) {
			 alert("Invalid positive integer for " + fname)
			 isvalid=false
		 }
	}

  			   
//------------------tested------------------------------------------//			   
			   
   
   if (isvalid==true)
   {
	  if (field1.type == "text") 
	  {
			field1.focus()
		}
		return true
	}
	else 
		return false
	}

//-----------------------------------------------------------------//
function validpswd(password1, password2){
	if (password1.value != password2.value) {
		alert ("Password validation failed please re-enter password.")
		password1.focus()
		return false
	}
	return true		
}

function checkpasswordthai(field1){
	var	  isvalid = true;

	var psw = field1.value;

	var found = false;var i=0;var c = 0;
	//must have spacial character
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(!( (c>64 && c<91) || (c>96 && c<123) || (c>47 && c<58))){
			found = true;
			break;
		}
	}

	if(!found){
		alert('\u00c3\u00cb\u00d1\u00ca\u00bc\u00e8\u00d2\u00b9\u00b5\u00e9\u00cd\u00a7\u00c1\u00d5\u00b5\u00d1\u00c7\u00cd\u00d1\u00a1\u00c9\u00c3\u00be\u00d4\u00e0\u00c8\u00c9');
		return false;
	}

	//must have upper case character
	found = false;
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(c>64 && c<91){
			found = true;
			break;
		}
	}

	if(!found){
		alert('\u00c3\u00cb\u00d1\u00ca\u00bc\u00e8\u00d2\u00b9\u00b5\u00e9\u00cd\u00a7\u00c1\u00d5\u00b5\u00d1\u00c7\u00be\u00d4\u00c1\u00be\u00ec\u00e3\u00cb\u00ad\u00e8');
		return false;
	}
	//must have lower case character
	found = false;
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(c>96 && c<123){
			found = true;
			break;
		}
	}

	if(!found){
		alert('\u00c3\u00cb\u00d1\u00ca\u00bc\u00e8\u00d2\u00b9\u00b5\u00e9\u00cd\u00a7\u00c1\u00d5\u00b5\u00d1\u00c7\u00be\u00d4\u00c1\u00be\u00ec\u00e0\u00c5\u00e7\u00a1');
		return false;
	}

	//must have number
	found = false;
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(c>47 && c<58){
			found = true;
			break;
		}
	}

	if(!found){
		alert('\u00c3\u00cb\u00d1\u00ca\u00bc\u00e8\u00d2\u00b9\u00b5\u00e9\u00cd\u00a7\u00c1\u00d5\u00b5\u00d1\u00c7\u00e0\u00c5\u00a2');
		return false;
	}
}
function checkpassword2(field1){
	var	  isvalid = true;

	var psw = field1.value;

	var found = false;var i=0;var c = 0;
	//must have spacial character
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(!( (c>64 && c<91) || (c>96 && c<123) || (c>47 && c<58))){
			found = true;
			break;
		}
	}

	if(!found){
		alert('password must have special character');
		return false;
	}

	//must have upper case character
	found = false;
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(c>64 && c<91){
			found = true;
			break;
		}
	}

	if(!found){
		alert('password must have upper case character');
		return false;
	}
	//must have lower case character
	found = false;
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(c>96 && c<123){
			found = true;
			break;
		}
	}

	if(!found){
		alert('password must have lower case character');
		return false;
	}

	//must have number
	found = false;
	for(i=0;i<psw.length;i++){
		c = psw.charCodeAt(i);
		if(c>47 && c<58){
			found = true;
			break;
		}
	}

	if(!found){
		alert('password must have number');
		return false;
	}
}
function checkpassword1(field1, type1, chknull, fname) {	
	if(checkpassword(field1, type1, chknull, fname) == false)
		return false;
	else
		return true;

/*	
	var   isvalid = true
	
	if (chknull) {
		 if (field1.value == null || field1.value == "") {
			 alert(fname + " requires a value.")
			 isvalid=false
		 }
		 
	}
	
	strVal = field1.value
	
	if (isvalid==true)   {
		var offset = 0
			
			offset = strVal.indexOf(' ')
			if (offset != -1)   {
				alert(fname + " cannot be space.")
					 		
				isvalid = false
			}
	}
	
	strVal = field1.value
	if (isvalid==true) {		  
		 if( (strVal.length < 4) || (strVal.length > 10))
		 {
			 alert(fname + " must in 4-10 character.")
			 isvalid=false
		 }
	}
	
	if (isvalid==false) {
		if (field1.type == "text") {
			field1.focus()
		}
		return false
	}
	else 
		return true

*/
}
function checkpassword(field1, type1, chknull, fname) {
	var   isvalid = true
	if (chknull) {
		 if (field1.value == null || field1.value == "") {
			 alert(fname + " requires a value.")
			 isvalid=false
		 }
		 
	}
	
	strVal = field1.value
	
	if (isvalid==true)   {
		var offset = 0
			
			offset = strVal.indexOf(' ')
			if (offset != -1)   {
				alert(fname + " cannot be space.")
					 		
				isvalid = false
			}
	}
	
	strVal = field1.value
	if (isvalid==true) {		  
		 if( (strVal.length < 6) || (strVal.length > 10))
		 {
			 alert(fname + " must in 6-10 character.")
			 isvalid=false
		 }
	}
	
	if (isvalid==false) {
		if (field1.type == "text") {
			field1.focus()
		}
		return false
	}
	else 
		return true
}
function daytradeWindow(fileName){
  open(fileName,"Daytrade","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=780,height=550");
}

function technicalWindow(fileName){
  open(fileName,"TechnicalAnalysis","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=780,height=550");
}
function openTechnicalWindow(fileName){
  window.open(fileName,"TechnicalAnalysis","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=780,height=550");
}
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
  // if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
	  retValue = retValue.substring(1, retValue.length);
	  ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
	  retValue = retValue.substring(0, retValue.length-1);
	  ch = retValue.substring(retValue.length-1, retValue.length);
   }
   /*
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	  retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   */
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function windif() {
 var difx, dify, w = getwindow(),
  winx = w.w, winy = w.h;
 window.resizeTo( winx, winy );
 w = getwindow();
 difx = winx - w.w; dify = winy - w.h;
 window.resizeTo( winx + difx, winy + dify );
 return difx + 'px ? ' + dify + 'px';
}


// Streaming
var streamingWin;
function moveWindow(windowName, width, height) {
	screenWidth = screen.availWidth; 
	screenHeight = screen.availHeight; 
	winTop = (screen.availHeight - height) / 2; 
	winLeft = (screen.availWidth - width) / 2; 
	windowName.moveTo(winTop, winLeft);
	windowName.focus();
}
function openStreaming(){
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open('/daytradeflex/DaytradeFlexScreen.jsp?resolution='+screen.width,"Daytrade","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=780,height=550");
	moveWindow(streamingWin, 780, 550);
}
function openStreamingDGW(){
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open('/daytradeflex/DaytradeFlexScreenAsl.jsp?resolution='+screen.width,"Daytrade","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=780,height=550");
	moveWindow(streamingWin, 780, 550);
}
function openStreamingFullScreen(){
	var width = screen.width * 0.975;
	var height =  width * 0.70512820512820512820512820512821;
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open('/daytradeflex/DaytradeFlexScreen.jsp?resolution='+screen.width,'Daytrade','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+width+',height='+height);
	moveWindow(streamingWin, width, height);
}
function openStreamingFullScreenDGW(){
	var width = screen.width * 0.975;
	var height =  width * 0.70512820512820512820512820512821;
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open('/daytradeflex/DaytradeFlexScreenAsl.jsp?resolution='+screen.width,'Daytrade','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+width+',height='+height);
	moveWindow(streamingWin, width, height);
}
function openStreamingMktRep(){
	streamingMktRepWin = window.open('/daytradeflex/DaytradeFlexScreenMktRep.jsp?resolution='+screen.width,"DaytradeMktRep","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=780,height=550");
	moveWindow(streamingMktRepWin, 780, 550);
}
function openRealTimeScreen(url, width, height) {
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open(url,'Daytrade','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+width+',height='+height);
	moveWindow(streamingWin, width, height);
}

function openRealTimeFullScreen(url, width, height) {
	var newHeight = screen.availHeight * 0.95;
	var newWidth = newHeight * (width / height);
	
	if (newWidth > width || newHeight > height) {
		newWidth = width;
		newHeight = height;
	}
	
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open(url + '&width='+newWidth+'&height='+newHeight,
		'Daytrade','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+newWidth+',height='+newHeight);
	moveWindow(streamingWin, newWidth, newHeight);
}

var streamingWinD;
var realtimeWinD;
function openStreamingD(server){
	if (streamingWinD && streamingWinD.open && !streamingWinD.closed) streamingWinD.close();
	streamingWinD = window.open(server + '/StreamingDScreen.jsp?resolution='+screen.width,"StreamingD","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=780,height=550");
	//moveWindow(streamingWinD, 780, 550);
}
function openStreamingDFullScreen(server){
	var width = screen.width * 0.975;
	var height =  width * 0.70512820512820512820512820512821;
	if (streamingWinD && streamingWinD.open && !streamingWinD.closed) streamingWinD.close();
	streamingWinD = window.open(server + '/StreamingDScreen.jsp?resolution='+screen.width,'StreamingD','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+width+',height='+height);
	//moveWindow(streamingWinD, width, height);
}
function openRealTimeDScreen(url, width, height) {
	if (realtimeWinD && realtimeWinD.open && !realtimeWinD.closed) realtimeWinD.close();
	realtimeWinD = window.open(url,'RealTimeD','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+width+',height='+height);
	//moveWindow(realtimeWinD, width, height);
}
function openStreamingDScreen(url, width, height) {
	if (streamingWinD && streamingWinD.open && !streamingWinD.closed) streamingWinD.close();
	streamingWinD = window.open(url,'StreamingD','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width='+width+',height='+height);
	//moveWindow(streamingWinD, width, height);
}
function openPlaceOrderD(url, width, height) {
	if (streamingWinD && streamingWinD.open && !streamingWinD.closed) streamingWinD.close();
	streamingWinD = window.open(url,'StreamingD','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=0,width='+width+',height='+height);
	//moveWindow(streamingWinD, width, height);
}

function openDerivative(fileName) {
	var derivative = window.open(fileName, "DerivativesPopup",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=no,resizable=0,width=653,height=560');
	derivative.focus();
}

function openDerivativeMain(fileName) {
	var derivative = window.open(fileName, "Derivatives");
	derivative.focus();
}

function openDerivativeMainNoPopup(fileName) {
	parent.location = fileName;
}

//for left menu
function preload(){
	if(document.images){
		Open = new Image(16,13)    
		Closed = new Image(16,13)
		Open.src = "open.gif"
		Closed.src = "closed.gif"
	}
}
function showhide(what){
	if (what.style.display=='none'){
		what.style.display=''
	}else{
		what.style.display='none'
	}
}