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 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=200,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('ÃËÑÊ¼èÒ¹µéÍ§ÁÕµÑÇÍÑ¡ÉÃ¾ÔàÈÉ');
		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('ÃËÑÊ¼èÒ¹µéÍ§ÁÕµÑÇ¾ÔÁ¾ìãË­è');
		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('ÃËÑÊ¼èÒ¹µéÍ§ÁÕµÑÇ¾ÔÁ¾ìàÅç¡');
		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('ÃËÑÊ¼èÒ¹µéÍ§ÁÕµÑÇàÅ¢');
		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){
	var windowName = getCookie("xid");
  	if (windowName){
		windowName = "_" + windowName;
  	}
  	var time = getCurrentUTC();
  	var concat = (fileName.indexOf('?') > -1) ? "&" : "?";
  	var tmp = window.open(fileName + concat + time,"Daytrade" + windowName,"toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=780,height=550");
  	if (tmp.focus) {
  		tmp.focus();
  	}
}

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 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


// Streaming
/*var streamingWin;
function openOnlineTradeZone(){		
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open('/scbs/realtime/OnlineTradeZone.jsp?resolution='+screen.width,"Daytrade","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=1004,height=670");
	//alert(streamingWin);
	//moveWindow(streamingWin, 1004, 670);
}
function openStreaming(){
	alert("00000");
	if (streamingWin && streamingWin.open && !streamingWin.closed) streamingWin.close();
	streamingWin = window.open('/daytradeflex/DaytradeFlexScreen.jsp?width=780&height=550',"Daytrade","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=780,height=550");
	alert(streamingWin);
}
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?width='+width+'&height='+height,'Daytrade','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width='+width+',height='+height);
}*/

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 getCookie(c_name) {
	if (document.cookie.length>0) {
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1) { 
		    c_start=c_start + c_name.length+1 
		    c_end=document.cookie.indexOf(";",c_start)
	    	if (c_end==-1) c_end=document.cookie.length
	    	return unescape(document.cookie.substring(c_start,c_end))
		} 
	}
	return ""
}
function getCurrentUTC() {
    var currentTime = new Date();

    return Date.UTC(
      currentTime.getUTCFullYear(),
      currentTime.getUTCMonth(),
      currentTime.getUTCDate(),
      currentTime.getUTCHours(),
      currentTime.getUTCMinutes(),
      currentTime.getUTCSeconds(),
      currentTime.getUTCMilliseconds());
}