function checkDistrict()
{   var input = document.all.tags("input");
    var district = document.all.tags("select");
    var dist = "";
    var start = 0;
	for (var i=0;i<district.length;i++)
  {   if(district[i].getAttribute("type") == "select-one")
       {   
           
           if( district[i].name.indexOf("dist")>=0)
           {
                   dist = district[i].options[district[i].selectedIndex].text;
                   dist = dist.substring(0,2);
                        for (var j=start;j<input.length;j++)
                        {   if(input[j].getAttribute("type") == "radio")
                            {
                                if( input[j].name.indexOf("area")>=0 )
                                {   if (input[j].value == "&#x9999;&#x6e2f;" || input[j].value == "HK")
                                     {start = j;}
									
                                    if (input[j].checked)  
                                    {   start = start + 4;
                                        if (input[j].value.substring(0,2) != dist)
                                        {alert("The sub_district and area should be matched!");
                                        return false;}  
                                        j = input.length;
                                    }  
                                    else if (j >= start + 3 )
                                    {   start = j;
		         						j = input.length;
                                        if (district[i].options[district[i].selectedIndex].text != "-")
                                        {alert("The sub_district and area should be matched!");
                                        return false;}     
                                    }    
                                   
                                    
                                }    
                            }
                        }
                            
                }
                
        }
        
    }
    
    return true;
}
function checkAllBox()
{   var input = document.all.tags("input");
                    for (var j=0;j<input.length;j++)
                        {   if(input[j].getAttribute("type") == "checkbox")
                            {
                                    if (!input[j].checked)  
                                    {  input[j].value = "N";
		        input[j].checked = true;	
                                    }  
                            }    
                        }
                            
}

function ArrowKey(index)
{	var start=0;
			for(var i = 0;i<document.all.length;i++)
				{
					if (document.all[i].getAttribute("type") == "text")
					{	start = i;
					}
				}
var direction =event.keyCode;


switch(direction){
case 38:
document.all[index-12].focus();
break;
case 40:
document.all[index+12].focus();
break;
case 37:
document.all[index-2].focus();
break;
case 39:
document.all[index+2].focus();
break;
default:
break;
}
}
function TickDistrict( index)
{	
	
			
			
			
			         	for(var i =index;i<document.all.length;i++)
				{
					if (document.all[i].getAttribute("type") == "radio")
					{	if (document.all[index].selectedIndex == 0)
						{document.all[i].checked = false;
						 document.all[i+1].checked = false;
                         document.all[i+2].checked = false;}
				       	else if (document.all[index].selectedIndex > 0 && document.all[index].selectedIndex < 35 )
						document.all[i].checked = true;
						else if (document.all[index].selectedIndex > 34 && document.all[index].selectedIndex < 73 )
			               			{ document.all[i+1].checked = true;}
						else if (document.all[index].selectedIndex > 72 && document.all[index].selectedIndex < 112)
				                	{ document.all[i+2].checked = true ;}	
						i = document.all.length;

					}
				}
		                
			
		
	

}

function chkLicDistrict(){
		var district = document.all.tags("select");
		var dist = "";
		var sIdx = 0;
		var name = "";
		for (var i=0;i<district.length;i++){
			if(district[i].getAttribute("type")=="select-one"){
				name = district[i].getAttribute("name");
				if( (name.indexOf("subdist")>=0 ) || (name.indexOf("subdistChn")>=0 ) ){			
					dist = district[i].options[district[i].selectedIndex].text;
					dist = dist.substring(0,2);
					if( dist=="HK" ){
						sIdx = 1;
					}else if( dist == "KL" ){
						sIdx = 2;
					}else if( dist == "NT" ){
						sIdx = 3;
					}else{
						sIdx = 0;
					}
				}else if( (name.indexOf("areacode")>=0 ) || (name.indexOf("areacodeChn")>=0 ) ){
					district[i].selectedIndex = sIdx;
				}
			}
		}
}
function new_view_window(URL) {

var option = "toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=600,height=600,left=200,top=200";
var win = window.open(URL, "new", option);  

//win = window.open(URL,"new","toolbar=1,menubar=1,srollbars=1,resizable=1,height=600,width=600,left=200,top=200") ;
	win.creator = self;
}

function upload(URL) {
	var option = "toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=600,height=600,left=200,top=200";
	var win = window.open(URL, "new", option);  

	win.creator = self;
}
function leftRightTrim(str){
    while(''+ str.charAt(0) == ' ')
    str = str.substring(1, str.length);

    while(''+ str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length -1);
    
    return str;
}
function prPopUp(urlPath){
  window.open(urlPath,'','toolbar=no,titlebar=no,scrollbars=yes,resizable=yes');
}


function popWin(urlPath) {
  window.open(urlPath,"PS","width="+screen.width+",height="+screen.height*0.88+",location=no,directories=no,menubar=no,toolbar=no,scrollbars=yes,status=yes,resizable=yes,left='0',top='0'");
}

function popHelp1(urlPath) {
  window.open(urlPath,"PS","width=800,height=600,location=no,directories=no,menubar=no,toolbar=no,scrollbars=yes,status=yes,resizable=yes,left='0',top='0'");
}

function leftTrim(str){
    while(''+ str.charAt(0) == ' ')
    str = str.substring(1, str.length);
    return str;
}

function rightTrim(str){
    while(''+ str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length -1);
    return str;
}
function setSampleValue(){
		var objEle = document.all.tags("input");
		var rn=Math.round(Math.random()*1000);
		for (var i=0;i<objEle.length;i++){ 
			if(objEle[i].getAttribute("type")=="text"){
				if( objEle[i].name.indexOf("Bus")>=0 )
					objEle[i].value="bus-" + rn;
				else if( objEle[i].name.indexOf("brcBrCode")>=0 )
					objEle[i].value=rn;
				else if( objEle[i].name.indexOf("Page")>=0 )
					objEle[i].value=rn;
				else if( objEle[i].name.indexOf("Dir1")>=0 )
					objEle[i].value="dir1-" + rn;
				else if( objEle[i].name.indexOf("Dir2")>=0 )
					objEle[i].value="dir2-" + rn;
				else if( objEle[i].name.indexOf("Dir3")>=0 )
					objEle[i].value="dir3-" + rn;
				else if( objEle[i].name.indexOf("Dir4")>=0 )
					objEle[i].value="dir4-" + rn;
				else if( objEle[i].name.indexOf("App")>=0 )
					objEle[i].value="app-"+ rn;
				else if( objEle[i].name.indexOf("Pic1A")>=0 )
					objEle[i].value="Pic1A-" + rn;
				else if( objEle[i].name.indexOf("Pic2A")>=0 )
					objEle[i].value="Pic2A-" + rn;
				else if( objEle[i].name.indexOf("Pic5A")>=0 )
					objEle[i].value="Pic5A-" + rn;
				else if( objEle[i].name.indexOf("Dpic")>=0 )
					objEle[i].value="dpic2-" + rn;
				else if( objEle[i].name.indexOf("Pha4A")>=0 )
					objEle[i].value="Pha4A-" + rn;
				else if( objEle[i].name.indexOf("Pha6A")>=0 )
					objEle[i].value="Pha6A-" + rn;
				else if( objEle[i].name.indexOf("Pha8A")>=0 )
					objEle[i].value="Pha8A-" + rn;
				else if( objEle[i].name.indexOf("Pha9A")>=0 )
					objEle[i].value="Pha9A-" + rn;
				else if( objEle[i].name.indexOf("PhaML")>=0 )
					objEle[i].value="PhaML-" + rn;
				else if( objEle[i].name.indexOf("Awh1")>=0 )
					objEle[i].value="Awh1-" + rn;
				else if( objEle[i].name.indexOf("Awh2")>=0 )
					objEle[i].value="Awh2-" + rn;
				else if( objEle[i].name.indexOf("Awh3")>=0 )
					objEle[i].value="Awh3-" + rn;
				else if( objEle[i].name.indexOf("Awh4")>=0 )
					objEle[i].value="Awh4-" + rn;
				else if( objEle[i].name.indexOf("sig")>=0 )
					objEle[i].value="john-" + rn;
				else if( objEle[i].name.indexOf("SPay3A")>=0 )
					objEle[i].value="sm3A-" + rn;
				else if( objEle[i].name.indexOf("chk")>=0 )
					objEle[i].value="hello world-" + rn;
				else
					objEle[i].value="text" + rn;
			}else if(objEle[i].getAttribute("type")=="radio"){
				objEle[i].checked = true;
			}
		}
		var objEle1 = document.all.tags("select");
		var rn1=Math.round(Math.random()*5);
		for (var i=0;i<objEle1.length;i++){
			if(objEle1[i].getAttribute("type")=="select-one"){
				if( objEle1[i].length <= 1 )
					objEle1[i].selectedIndex=0;
				else{
					if( objEle1[i].length > rn1 )
						objEle1[i].selectedIndex=rn1;
					else
						objEle1[i].selectedIndex=1;
				}
			}
		}
		var objEle2 = document.all.tags("textarea");
		var rn2=Math.round(Math.random()*5);
		for (var i=0;i<objEle2.length;i++){
			objEle2[i].value="text description " + rn;
		}
}


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_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_findObj(n, d) { //v3.0
  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); return x;
}

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 goSelectURL(item) {
var site=item.selectedIndex;

  if ( (item.options[site].value!="") && (item.options[site].value!="0") )
     location = item.options[site].value + ".html";
}

function goSelectURL(item) {
  var site=item.selectedIndex;
  location = item.options[site].value;
}


function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
}

function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}


function format_with_comma(value) {
    	var Count = -1
     	var returnvalue = ""
     	var location = value.indexOf(".", 0)
     	     	
	for (var i = location; i > 0; i--) {
		Count++;
    		if ((Count % 3) == 0 && (Count != 0))
	        	returnvalue = "," + returnvalue;
	        	returnvalue = value.substring(i-1, i) + returnvalue;
    	}
	returnvalue += value.substring(location, value.length)
    	return returnvalue
}

function convertTo2Chars(value) {
  if (value.length >=2)
  return value;
  else if (value.length <= 1)
  return "0" + value;
 }

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 popup(link, name) 
{
if (! window.focus)
  return true;
var href;
if (typeof(link) == 'string')
  href=link;
else
  href=link.href;
window.open(href, name, 'width=800,height=600, top=0,left=0, scrollbars=yes,resizable=yes');
return false;
}

function popHelp(linkStr) 
{ var win = window.open(linkStr,"Help","width=380,height=240,status=no,resizable=no,top=300,left=460");
}
function popView(linkStr)
{
  var win = window.open(linkStr,"Help","width=450,height=500,status=no,resizable=yes,scrollbars=yes,top=50,left=460");
}
function getattachIDName(value) {
	var idname1= "Please attach BR Certificate!";
	var idname2= "Please attach BR Certificate of additional storage / warehouse!";
	var idname3= "Please attach Copy of Certificate of Incorporation (Ltd. Only)!";
	var idname4= "Please attach Copy of Director List (Ltd Only)!";
	var idname5= "Please attach Copy of Form 1A (Sole Proprietor Only)!";
	var idname6= "Please attach Copy of Form 1C (Partnership)!";
	var idname7= "Please attach Staff List!";
	var idname8= "Please attach Sale & Purchase Agreement (antibiotics/poisons/dangerous) - For I/E Only!";
	var idname9= "Please attach Copy of Product Registration / Agency Appointment Proof!";
	var idname10= "Please attach Floor Plan of Premises!";
	var idname11= "Please attach Floor Plan of additonal storage / warehouse!";
	var idname12= "Please attach Pharmacist Certificate!";
		if (value == "1") {
			return idname1;
		}
		else if (value == "2") {
			return idname2;
		}
		else if (value == "3") {
			return idname3;
		}
		else if (value == "4") {
			return idname4;
		}
		else if (value == "5") {
			return idname5;
		}
		else if (value == "6") {
			return idname6;
		}
		else if (value == "7") {
			return idname7;
		}
		else if (value == "8") {
			return idname8;
		}
		else if (value == "9") {
			return idname9;
		}
		else if (value == "10") {
			return idname10;
		}
		else if (value == "11") {
			return idname11;
		}
		else if (value == "12") {
			return idname12;
		}
		else {
			return "";
		}
}
	
	
function checkAddressDist(){
		var district = document.all.tags("select");
		var dist = "";
		var sIdx = 0;
		var name = "";
		for (var i=0;i<district.length;i++){
			if(district[i].getAttribute("type")=="select-one"){
				name = district[i].getAttribute("name");
				if( (name.indexOf("SUB_DISTRICT")>=0 )  ){			
					dist = district[i].options[district[i].selectedIndex].text;
					dist = dist.substring(0,2);
					if( dist=="HK" ){
						sIdx = 1;
					}else if( dist == "KL" ){
						sIdx = 2;
					}else if( dist == "NT" ){
						sIdx = 3;
					}else{
						sIdx = 0;
					}
				}else if( (name.indexOf("AREA")>=0 ) ){
					district[i].selectedIndex = sIdx;
				}
			}
		}
}

function popLink(urlPath) {
  window.open(urlPath,"PS","width="+screen.width+",height="+screen.height*0.88+",location=yes,directories=yes,menubar=yes,toolbar=yes,scrollbars=yes,status=yes,resizable=yes,left='0',top='0'");
}