/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


function PrintLabel() {

		 var paperWidth=gTemplate[1];
		 var paperHeight=gTemplate[2];
		 var marginLeft=gTemplate[3];
		 var marginTop=gTemplate[4];

		 var labelWidth=gTemplate[5];
		 var labelHeight=gTemplate[6];
		 var numRows=gTemplate[7];

		 var numColumns=gTemplate[8];
		 var horizontalSpace=gTemplate[9];
		 var verticalSpace=gTemplate[10];

		 var pr1 = window.open( "", "PrintLabel");
	       var myhtml = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link rel="stylesheet" href="css/reset.css" type="text/css"><link rel="stylesheet" href="css/labelgrid.css" type="text/css"></head><body></body></html>';

		 pr1.document.open();
	       pr1.document.write(myhtml);
		 pr1.document.close();
		 var labelbody = $('body',pr1.document);

		 var i=0,j=0,ipages=1;
		 var posix=0,posiy=0;
		 var numrows=numRows, numcolumns=numColumns;

		 for (ipages=0;ipages<gNumberOfPages;ipages++)
		 {
		 var labelwrapperwidth=0,labelwrapperheight=0;
		 labelwrapperwidth=(numcolumns-1)*(labelWidth+horizontalSpace) + labelWidth;
		 labelwrapperheight=(numrows-1)*(labelHeight+verticalSpace) + labelHeight;

	       var labelwrapper=$('<div class="labelwrapper" id="lw'+ipages+'"></div>',pr1.document);
		 labelwrapper.css('margin-left',marginLeft+'in')
		             .css('margin-top',marginTop+'in')
		             .css('width',labelwrapperwidth+'in')
		             .css('height',labelwrapperheight+'in');

		 labelbody.append(labelwrapper);

		 for (i=0;i<numrows;i++)
		 {
			posix=0;
			posiy=i*(labelHeight+verticalSpace);
			for (j=0;j<numcolumns;j++)
			{
				posix=j*(labelWidth+horizontalSpace);
			      var label=$('<div class="label"></div>',pr1.document);
				label.css('width',labelWidth+'in')
				     .css('height',labelHeight+'in')
				     .css('left',posix+'in')
				     .css('top',posiy+'in');
				$('#lw'+ipages,pr1.document).append(label);
			}
		 }
		 labelbody.append('<p style="page-break-before: always;height:1px"></p>');

	       }

	 	 var oleft,otop;
		 oleft=$('.labelwrapper',pr1.document).css('margin-left');
		 otop=$('.labelwrapper',pr1.document).css('margin-top');
		 oleft=ConvertPxToInt(oleft)-21;
		 otop=ConvertPxToInt(otop)-14;
		 oleft=oleft+'px';
		 otop=otop+'px';
		 $('.labelwrapper',pr1.document).css('margin-left',oleft).css('margin-top',otop);

 		 $('.label').children().each(function(index){
			var textobj=$('<div class="label-text ui-widget-content">hello</div>',pr1.document);
			textobj.html($(this).html());

			textobj.css('left',$(this).css('left'));
			textobj.css('top',$(this).css('top'));
			textobj.appendTo($('.label',pr1.document));
		 });

		pr1.print();


  	};

      function ConvertPxToInt(pxVal) {
            var validch = "0123456789.";
            var retVal = 0;
            for (i = 0; i < pxVal.length; i++) {
                if (validch.indexOf(pxVal.charAt(i)) == -1) {
                    if (i > 0) {
                        retVal = parseInt(pxVal.substring(0, i));
                        return retVal;
                    }
                }
            }
            return retValue;
      }

	function ConvertInchToInt(pxVal) {
            var validch = "0123456789.";
            var retVal = 0;
            for (i = 0; i < pxVal.length; i++) {
                if (validch.indexOf(pxVal.charAt(i)) == -1) {
                    if (i > 0) {
                        retVal = parseFloat(pxVal.substring(0, i));
                        return retVal;
                    }
                }
            }
            return retValue;
      }


	function CreateTemplateView(paperWidth,paperHeight,marginLeft,marginTop,labelWidth,labelHeight,numRows,numColumns,horizontalSpace,verticalSpace)
	{
		var sRatio=1.35;
		var screenwidth=8.268/sRatio,screenheight=11.693/sRatio;
		var widthratio=screenwidth/paperWidth;
		var heightratio=screenheight/paperHeight;
		paperWidth=widthratio*paperWidth;
		paperHeight=heightratio*paperHeight;
		marginLeft=widthratio*marginLeft;
		marginTop=heightratio*marginTop;
		labelWidth=widthratio*labelWidth;
		labelHeight=heightratio*labelHeight;
		horizontalSpace=widthratio*horizontalSpace;
		verticalSpace=heightratio*verticalSpace;

		CreateTemplateInches(paperWidth,paperHeight,marginLeft,marginTop,labelWidth,labelHeight,numRows,numColumns,horizontalSpace,verticalSpace)

	}

	function CreateTemplateInches(paperWidth,paperHeight,marginLeft,marginTop,labelWidth,labelHeight,numRows,numColumns,horizontalSpace,verticalSpace)
	{

			var paper=$('<div class="paper"></div>');
			paper.css('width',paperWidth+'in');
			paper.css('height',paperHeight+'in');
			paper.appendTo('#container');

			var i=0,j=0;
			var posix=0,posiy=0;
			var numrows=numRows, numcolumns=numColumns;
			for (i=0;i<numrows;i++)
			{
				posix=0;
				posiy=i*(labelHeight+verticalSpace);

				for (j=0;j<numcolumns;j++)
				{
					posix=j*(labelWidth+horizontalSpace);
					var label=$('<div class="label"></div>');
					label.css('margin-left',marginLeft+'in')
					     .css('margin-top',marginTop+'in')
					     .css('width',labelWidth+'in')
					     .css('height',labelHeight+'in')
					     .css('left',posix+'in')
					     .css('top',posiy+'in');
					paper.append(label);
				}
			}

	};