/*
 * write embed tag for mac plugin
 */
function writeMacPlugin(){
    if( navigator.userAgent.match(/Mac|PPC/)){
        document.write( "<embed name='PrintCreationsPlugin' width='0' height='0' type='application/x-PrintWebkit'></embed>" );
		if(navigator.userAgent.indexOf("Firefox") >= 1)
		{
			var printcreationsplugin = navigator.mimeTypes["application/x-printWebkit"];
			if(printcreationsplugin)
			{
				document.write('<embed name="PrintCreationsXPlugin" id="myctrl" type="application/x-printWebkit" width=0 height=0"> </embed>');
			}
		}
		else
		{
			document.write('<embed name="PrintCreationsXPlugin" id="myctrl" type="application/x-printWebkit" width=0 height=0"> </embed>');
		}
    }
}

/*
 * write object tag for windows plugin
 */
function writeIEPlugin(){
    var userAgent = navigator.userAgent;
	if(userAgent.indexOf("MSIE")>=1 ){
    	document.write('<OBJECT codeBase="' + document.getElementById('win_plugin_url1').href +'#version=2,2,255,293" ');
    	document.write('id="heatax" height="600" width="800" data="data:application/x-oleobject;base64,xA3DyyjPm06QNm4IaiUTmgADAACvUgAAAz4AAA==" classid="clsid:CBC30DC4-CF28-4E9B-9036-6E086A25139A" name="heatax" VIEWASTEXT>');
    	document.write('<div class="noactivex">');
    	document.write('<SPAN STYLE="color:red">You need to install/upgrade the latest ActiveX Control to use the Brother CreativeCenter. Please close this window and install/upgrade ActiveX Control from previous screen.<\/SPAN>');
    	document.write('<br/><br/><a href="#" class="en-us-close" onClick="window.close(); return false;"></a>');
    	document.write('</div>');
    	document.write('<\/OBJECT>');
	}
	else if(userAgent.indexOf("Firefox") >= 1){
		writeFireFoxPlugin();
	}
}
/*
 * write embed tag for Firefox plugin
 */
function writeFireFoxPlugin(){
	var	plugin = navigator.mimeTypes["application/x-printWebkit"];
	if(plugin)
	{
    	document.write('<div id="closeMsg" class="noactivex" style="display:none">');
    	document.write('<SPAN STYLE="color:red">You need to install/upgrade the latest ActiveX Control to use the Brother CreativeCenter. Please close this window and install/upgrade ActiveX Control from previous screen.<\/SPAN>');
    	document.write('<br/><br/><a href="#" class="en-us-close" onClick="window.close(); return false;"></a>');
    	document.write('</div>');
    	document.write('<div id="emb" style="display:block">');
		document.write('<embed name="PrintCreationsXPlugin" id="myctrl" type="application/x-printWebkit" width="800" height="600">');
	    document.write('</embed>');
    	document.write('</div>');
	}
}
/*
 * edit operation for mac
 */
function download(url,prjid,prjName)
{
// 2007/10/30 Ins Y.Yamada ---------------------------------
    var plugin  = document.embeds["PrintCreationsPlugin"];
    var version = plugin.getVersion();
    var lastVersion = "2.2.255.181";
    if(!chkVersion(lastVersion,version))
    {
        alert("There is a new version available.");
    }else{
        plugin._imageURL = url;
        plugin._prjID = prjid;
        plugin._prjName = prjName;
        plugin.startTemplateDownload();
    }
/* 2007/10/30 Del Y.Yamada ---------------------------------
    var plugin  = document.embeds["PrintCreationsPlugin"];

    plugin._imageURL = url;
    plugin._prjID = prjid;
    plugin._prjName = prjName;
    plugin.startTemplateDownload();
-- 2007/10/30 End Y.Yamada -------------------------------*/
    //alert(plugin.getPrintPaperNum());
}

/*
 * For FireFox Plug-in setup on MAC
 */
function downloadex(url,prjid,prjName)
{
	var printcreationsplugin = navigator.mimeTypes["application/x-printWebkit"];
    var lastVersion = "2.2.255.181";

	if(printcreationsplugin)
	{
		try
		{
			var plugin  = document.getElementById("myctrl");
			var version = plugin.printVersion;
			if(!chkVersion(lastVersion,version)){
		        alert("There is a new version available.");
			}
			else{
				plugin.Startup(url,prjid);
			}
		}
		catch(e)
		{
		}
	}
	else
	{
		alert("Please install the plugin first! mac");
	}
}

/*
 * 
 */
function ShowPage()
{
    var userAgent = navigator.userAgent;

    if( navigator.platform == "Win32" )
    {
    }
    //else if( document.getElementById("idIsMac").value =="1" )
    else if( userAgent.match(/Mac|PPC/))
    {
            var plugin  = document.embeds["PrintCreationsPlugin"];
        alert("The printed page(s) number is " + plugin.getPrintPaperNum());
    }
}

var winprint;
/*
 * display edit screen
 */
function showDialog(templates, classcode, fileDirMac)
{
    //alert(navigator.platform);
    var userAgent = navigator.userAgent;
    if( navigator.platform == "Win32" || navigator.platform == "Win64")
    {
        if( userAgent.indexOf("MSIE")>=1 )
        {
// 2007/10/31 Ins Y.Yamada ---------------------------------
                var hei = 825;
                var wid = 830;

                if(hei > screen.avilHeight){hei = screen.avilHeight;}
                if(wid > screen.avilWidth){wid = screen.avilWidth;}
/* 2007/10/30 Del Y.Yamada ---------------------------------
                var hei = 780;
                var wid = 830;

                if(hei > screen.height){hei = screen.height;}
                if(wid > screen.width){wid = screen.width;}
-- 2007/10/30 End Y.Yamada -------------------------------*/
                var showURL = "";
                showURL = showURL + "../../../compose/index.htm";

                var dialogPram = "";
                dialogPram = dialogPram + "dialogHeight:" + hei + "px;";
                dialogPram = dialogPram + "dialogWidth:" + wid + "px;";
                dialogPram = dialogPram + "center:yes;";
                dialogPram = dialogPram + "scroll:yes;";
                dialogPram = dialogPram + "status:no;";
                dialogPram = dialogPram + "help:no;";
                dialogPram = dialogPram + "resizable:yes;";
                dialogPram = dialogPram + "dependent:yes;";
                
                window.showModalDialog(showURL, self, dialogPram);
                
        }
		else if(userAgent.indexOf("Firefox") >= 1)
		{
			var printcreationsplugin = navigator.mimeTypes["application/x-printWebkit"];
			if(printcreationsplugin)
			{
                var hei = 825;
                var wid = 830;

                if(hei > screen.avilHeight){hei = screen.avilHeight;}
                if(wid > screen.avilWidth){wid = screen.avilWidth;}
                var showURL = "";
                showURL = showURL + "../../../compose/index.htm";

				if(winprint == null || winprint.closed)
				{
					document.cookie="printcreationwindowisopen=yes";
					winprint = window.open(showURL+"?temClass="+classcode,
					"",
					"width="+hei+"px,height="+wid+"px,top=0,left=0,modal=yes,resizable=yes,scrollbars=no,help=no");
				}
				else
				{
					alert("The window is opened!");
				}
			}
			else
			{
				alert("Please install the plugin first!");
			}
		}
        else
        {
            alert("This site requires IE6 or higher, or Firefox 1.5 or higher.");
        }
    }
//    else if( (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh"))
    else if( userAgent.match(/Mac|PPC/))
    {
        if( userAgent.indexOf("Safari")>=1 )
        {
            download(templates,classcode,fileDirMac);
        }
		else if(userAgent.indexOf("Firefox") >= 1)
		{
			downloadex(templates,classcode,fileDirMac);
		}
        else
        {
            alert("This site requires Safari 1.4 or higher");
        }
    }
    else
    {
        alert("This site need windows or mac OS");
    }

}


/*
 * initialize ActiveX object
 */
function init()
 {

    // firefox
    var userAgent = navigator.userAgent;
    if(userAgent.indexOf("Firefox") >= 1){
		try{

			var pram=location.search;

			if (pram){
				pram=pram.substring(1);
				var pair=pram.split("&");
				var i=temp="";
				var key=new Array();
				for (i=0; i < pair.length; i++) {
					temp=pair[i].split("=");
					keyName=temp[0];
					keyValue=temp[1];
					key[keyName]=keyValue;
				}
				strurl = opener.document.getElementById('cc_compose_file').href;
				if(key["temClass"]!="") {
					classcode = key["temClass"]
				}
				if(key["temClassid"]!="") {
					classcodeid = key["temClassid"]
				}
				var embed = document.getElementById("myctrl");
				embed.Startup(strurl,classcode,classcodeid);
				var lastVersion = "2.2.255.293";
				
				if(!chkVersion(lastVersion,embed.printVersion)){
					alert("There is a new version available.");
					var msg = document.getElementById("closeMsg");
					var emb = document.getElementById("emb");
					if(msg.style.display=="none"){
						msg.style.display="block";
						emb.style.display="none";
					}
					else{
						msg.style.display="none";
						emb.style.display="block";
					}
				}
			}
		}
		catch(e){
		}
	}
	else{
     	try
     	{
        	var obj = document.all("heatax");
        	if( window.dialogArguments != undefined && window.dialogArguments.file != undefined && window.dialogArguments.pattern != undefined ){
            	//alert( window.dialogArguments.file );
            	obj.IStartup(window.dialogArguments.file, window.dialogArguments.pattern, "");
        	}
    	}
    	catch(e)
    	{
	//        window.setTimeout("AutoClose()",5000);
    	}
    }
}

// Install Plugion Version Check
function chkVersion(lastversion,currentVersion){
	var nowversion=currentVersion.split(".");
	var arrversion=lastversion.split(".");
	var allcurrversion="";
	var allversion="";

	// last Version
	for (var i=0;i<arrversion.length;i++)
	{
		allversion=allversion+arrversion[i];
	}

	// current Version
	for (var j=0;j<nowversion.length;j++)
	{
		allcurrversion=allcurrversion+nowversion[j];
	}

	if(parseInt(allcurrversion) < parseInt(allversion) ){
		return false;
	}
	else{
		return true;
	}
}


/*
 * 
 */
function ShowPrintedPages()
{
        var obj = document.all("heatax");
        if( null != obj )
        {
            alert( "The printed page(s) number is " + obj.nPrintedPages );
            //alert( obj.OutPutPages("") );
        }
}


/*
 * auto window close function 
 */
function AutoClose()
{
    window.close();
}
