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 openWindow4(url, name, nWidth, nHeight){
	screenWidth = screen.width; 
	screenHeight = screen.height;
	winTop = 0;
	winLeft = 0;
	winWidth = nWidth;
	winHeight = nHeight;
	nProperty = "scrollbars=no,menubar=no,status=yes,toolbar=no,resizable=yes,titlebar=no,alwaysRaised=yes";

	if ((screen.availHeight-28) <= nHeight) {
		winHeight = screen.availHeight-28;
	}
	if ((screen.availWidth-10) <= nWidth) {
		winWidth = screen.availWidth-10;
	}
	
	winTop = (screen.availHeight-28 - winHeight) / 2; 
	winLeft = (screen.availWidth-10 - winWidth) / 2;
	nProperty = nProperty+
				",left="+(winLeft)+
				",top="+(winTop)+
				",width="+(winWidth)+
				",height="+(winHeight);		

    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 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 checkfield(field1, type1, nlen, chknull, fname) {
    var   isvalid =true
    if (chknull) {
        if (field1.value == null || field1.value == "") {
             alert(fname + " จะต้องมีค่า")
             isvalid=false
         }
    }
    strVal=field1.value;
    if (nlen != 0 && isvalid==true) {          
         if (strVal.length < nlen) {
             alert(fname + " จะต้องมีความยาวอย่างน้อย " + nlen + " ตัวอักษร")
             isvalid=false
         }
    }
    if (type1 == "n-" && isvalid==true) {
         if (!isNumber(field1.value, true, true)) {
             alert(fname +" จะต้องประกอบด้วยตัวเลขเท่านั้น")
             isvalid=false
         }
    }
    if (type1 == "n" && isvalid==true) {
         if (!isNumber(field1.value, false, true)) {
             alert(fname +" จะต้องประกอบด้วยตัวเลขเท่านั้น")
             isvalid=false
         }
    }
	if (type1 == "i-" && isvalid==true) {
         if (!isNumber(field1.value, true, false)) {
             alert(fname +" จะต้องเป็นเลขจำนวนเต็มเท่านั้น")
             isvalid=false
         }
    }
	if (type1 == "i" && isvalid==true) {
         if (!isNumber(field1.value, false, false)) {
             alert(fname +" จะต้องเป็นเลขจำนวนเต็มเท่านั้น")
             isvalid=false
         }
    }

	if (isvalid==false) {
		field1.focus()
    }
    return isvalid
/*
	if (isvalid==true)
	{
		if (field1.type == "text") 
			field1.focus()
		return true
    }
    else 
        return false
*/
}

function validpswd(password1, password2){
    if (password1.value != password2.value) {
        alert ("รหัสผ่าน และ รหัสผ่านอีกครั้งหนึ่ง จะต้องมีค่าเดียวกัน")
        password1.focus()
        return false
	}
	return true        
}

function checkpassword(field1, type1, chknull, fname) {
	var   isvalid = true
    if (chknull) {
		if (field1.value == null || field1.value == "") {
			alert(fname + " จะต้องมีค่า")
            isvalid=false
        }
    }
    strVal = field1.value
    if (isvalid==true)   {
    	var offset = 0
		offset = strVal.indexOf(' ')
		if (offset != -1)   {
			alert(fname + " ไม่สามารถมีช่องว่าง")
	    	isvalid = false
		}
	}
	strVal = field1.value

    if (isvalid==true) {          
		if( (strVal.length < 6) || (strVal.length > 10))
        {
            alert(fname + " จะต้องมีความยาวอยู่ระหว่าง 6-10 ตัวอักษร")
            isvalid=false
        }
    }
    
	if (isvalid==false) {
		field1.focus()
    }
    return isvalid
/*
    if (isvalid==false) {
        if (field1.type == "text") {
            field1.focus()
        }
        return false
    }
    else 
        return true
*/
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function load() {
  MM_preloadImages(
    '/brokerpage/IPO/images/top_panel/mainlink1_on.gif',
    '/brokerpage/IPO/images/top_panel/mainlink2_on.gif',
    '/brokerpage/IPO/images/top_panel/mainlink3_on.gif',
    '/brokerpage/IPO/images/top_panel/mainlink4_on.gif',
    '/brokerpage/IPO/images/top_panel/mainlink5_on.gif',
    '/brokerpage/IPO/images/top_panel/mainlink6_on.gif',
    '/brokerpage/IPO/images/left_panel/arrow_orange.gif',
    '/brokerpage/IPO/images/left_panel/bullet_blue.gif');
}
function validate2() 
{
	if (checkfield(document.loginForm.loginName,'s',0,true,'Login')==false)
	{
		return false;
	}
	if (checkpassword(document.loginForm.passwd,'s',true,'Password')==false)
	{
		return false;
	}
	return true;
}
function encodeQuery(query) 
{
	var enQ = query;
	var indexQ = enQ.indexOf("&");
	if (indexQ != -1) {
 		enQ = enQ.substring(0, indexQ) + "%26" + enQ.substring(indexQ+1);	
 	}
	return enQ;
}
function stockSelectPage() 
{
	if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="1")
	{
		document.quoteForm.action = "/S13_FastQuote.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&txtSymbol="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="2")
	{
		document.quoteForm.action = "/sims_profile.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&securityName="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="3")
	{
		document.quoteForm.action = "/sims_boardOfDirector.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&securityName="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="4")
	{
		document.quoteForm.action = "/sims_majorShares.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&securityName="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="5")
	{
		document.quoteForm.action = "/sims_financialStatement.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&securityName="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="6")
	{
		document.quoteForm.action = "/sims_competitor.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&securityName="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="7")
	{
		document.quoteForm.action = "/sims_rights.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&securityName="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="8")
	{
		document.quoteForm.action = "/sims_moreNews.jsp?txtBrokerId="+document.quoteForm.txtBrokerId.value+"&securityName="+encodeQuery(document.quoteForm.txtSymbol.value);
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="9")
	{
		window.open("http://www.set.or.th/static/company/f56_thai_sec.html");
		return false;
	}
	else if (document.quoteForm.selectPage.options[document.quoteForm.selectPage.selectedIndex].value=="10")
	{
		window.open("http://www.set.or.th/static/company/annual_thai_sec.html");
		return false;
	}
	document.quoteForm.securityName.value = document.quoteForm.txtSymbol.value;
	return true;
}
function setCookie(NameOfCookie, value, expiredays)
{
	
	var ExpireDate = new Date ();
	/*
	var indexS = window.location.href.indexOf("#");
	var oldlocation = window.location.href;
	if (indexS != -1) {
 		oldlocation = window.location.href.substring(0, indexS);	
 	}*/
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + '=' + escape(value) + ((expiredays == null) ? "" : "; path=/; expires=" + ExpireDate.toGMTString());
	
	//window.location.href = oldlocation;
}
function getCookie(NameOfCookie)
{
	if (document.cookie.length > 0)
	{ 
		begin = document.cookie.indexOf(NameOfCookie+'=');
		if (begin != -1)
			begin += NameOfCookie.length+1;
		else
			return "";
		end = document.cookie.indexOf(';', begin);
		if (end == -1)
			end = document.cookie.length;
		return unescape(document.cookie.substring(begin, end));
	}
	return "";
}

