/* JaveScript code for dynamically changing the display label
	for Dantech Ltd
 Copyright Cornish WebServices Ltd - http://www.cornishwebsrevices.co.uk 
 ------------------------------------------------------------------------ */

function changeimage(picture) {
	document.roundtopleft.src = picture;
}
function checkquantity(qty) {
	var PNum = new String(qty);	
	var regex = /[^0-9]/;	
	if (regex.test(PNum)) 	alert('Please enter a valid quantity (in units of 100)');
}


function changecolour() { // change the colour of text and background
	// first check if label type A or B then change the background
	if ((document.form1.fstyle1.checked)||(document.form1.fstyle2.checked)) {
	if (document.form1.fcolour0.checked) {changeimage('../images/red-tl3.jpg'); change('roundtop','roundtopred left'); change('showlabeltop','backred'); change('showlabelbottom','backwhiteblack'); change('line3','testclassred');}
	if (document.form1.fcolour1.checked) {changeimage('../images/blue-tl3.jpg'); change('roundtop','roundtopblue left'); change('showlabeltop','backblue'); change('showlabelbottom','backwhiteblack'); change('line3','testclassblue');}
	if (document.form1.fcolour2.checked) {changeimage('../images/green-tl3.jpg'); change('roundtop','roundtopgreen left'); change('showlabeltop','backgreen'); change('showlabelbottom','backwhiteblack'); change('line3','testclassgreen');}
	if (document.form1.fcolour3.checked) {changeimage('../images/black-tl3.jpg'); change('roundtop','roundtopblack left'); change('showlabeltop','backblack'); change('showlabelbottom','backwhiteblack'); change('line3','testclassblack');}
	}
	else { // label is C/D/E so change the text and make background white
	if (document.form1.fcolour0.checked) {changeimage('../images/white-tl3.jpg'); change('roundtop','roundtopwhite left'); change('showlabeltop','backwhitered'); change('showlabelbottom','backwhitered'); change('line3','testclasswhite');}
	if (document.form1.fcolour1.checked) {changeimage('../images/white-tl3.jpg'); change('roundtop','roundtopwhite left'); change('showlabeltop','backwhiteblue'); change('showlabelbottom','backwhiteblue'); change('line3','testclasswhite');}
	if (document.form1.fcolour2.checked) {changeimage('../images/white-tl3.jpg'); change('roundtop','roundtopwhite left'); change('showlabeltop','backwhitegreen'); change('showlabelbottom','backwhitegreen'); change('line3','testclassehite');}
	if (document.form1.fcolour3.checked) {changeimage('../images/white-tl3.jpg'); change('roundtop','roundtopwhite left'); change('showlabeltop','backwhiteblack'); change('showlabelbottom','backwhiteblack');change('line3','testclasswhite');}	
	checkline5();
		}
	
}
function changesize() {
	if (document.form1.fsize0.checked) { // gettting smaller - need to check displeay
		change('showlabelx','widthsmall');
		changemaxlength2('ftext1','fline1',20,'doIt1'); doIt1();
		changemaxlength2('ftext2','fline2',20,'doIt2'); doIt2();
		changemaxlength2('ftext3','fline3',20,'doIt3'); doIt3();
		changemaxlength2('ftext4','fline4',20,'doIt4'); doIt4();
		changemaxlength2('ftext5','fline5',20,'doIt5'); doIt5();
		changemaxlength3('fnumberingstart','num',6,'doIt6'); doIt6();
		}
	if (document.form1.fsize1.checked) {
		change('showlabelx','widthlarge');
		changemaxlength2('ftext1','fline1',25,'doIt1');
		changemaxlength2('ftext2','fline2',25,'doIt2');
		changemaxlength2('ftext3','fline3',25,'doIt3');
		changemaxlength2('ftext4','fline4',25,'doIt4');
		changemaxlength2('ftext5','fline5',25,'doIt5');
		changemaxlength3('fnumberingstart','num',8,'doIt6');
		}
}
function changelines() { // this changes the number of lines to enter text depending on style AND size
	// Rewritten to chamge 1 line at a time
	// line1 always shows fline1
	// line2 always shows fline2
	 checknumbering();
	checkline3(); // Show fline3 for styleA || styleC || styleE || Large
	checkline4(); // Show for Style E, show for Large && (style A or style C)
	checkline4b();
	checkline5(); // if E show Text, 
}

function checkline3() { // Show fline3 for styleA || styleC || styleE || Large
	if ((document.form1.fsize1.checked)||
		 	(document.form1.fstyle1.checked)||
				(document.form1.fstyle3.checked)||
					(document.form1.fstyle5.checked)) { // show fline3
		change('fline3','show');
		doIt3();
		}
	else {
		change('fline3','noshow');	
		writit('','line3');
	}
}
function checkline4() { // Show for Style E, show for Large && (style A or style C)
	if (document.form1.fstyle5.checked){// must be style E so must display TEXT
		if (document.form1.fsize1.checked) { // large size
			change('fline4','show');		
			doIt4();
		}
		else {
			change('fline4','show');	// need to show this	
			writit('','line4');	
			//doIt4E();
		}
	}	
	else if ((document.form1.fsize1.checked)&&((document.form1.fstyle1.checked)||(document.form1.fstyle3.checked))){
		change('fline4','show');		
		doIt4();
	}
	else {
		change('fline4','noshow');	
		writit('','line4');	
	}
}
function checkline4b() { // check for barcode - display if style B or D AND numbering selected
	if ((document.form1.fstyle2.checked)||(document.form1.fstyle4.checked)){// style B or D
		if (document.form1.fnumbering0.checked) { // numbering
			var bar = "&nbsp;<img src=\"../images/barcode0.jpg\">";
			writit(bar,'line4b');  // write barcode to label
		}
		else {
			writit('&nbsp;','line4b');  // write space to label
		}
	}
	else { // no barcode
		writit('','line4b');  // write NO barcode to label
	}
}

function checkline5() { // if E and large show Text
	
	if (document.form1.fstyle5.checked) {  // LABEL E so show TEXT
		if(document.form1.fsize1.checked) { // large size
			change('fline5','show');
			doIt5();
		}
		else { // small size - get text from fline4
			change('fline5','noshow');
			doIt4E();
		}
		change('fnumberingall','noshow');
	
		if (document.form1.fcolour0.checked) change('line5','showred'); // change text colour of line 5
		if (document.form1.fcolour1.checked) change('line5','showblue');
		if (document.form1.fcolour2.checked) change('line5','showgreen');
		if (document.form1.fcolour3.checked) change('line5','showblack');
	}
	else  { // if not E show numbers
		change('fline5','noshow'); // do not show entry text for line 5
		change('line5','showbold');
		if (document.form1.fnumbering0.checked) { // show numbering
			change('fnumberingstartshow','show');
			change('line5','show');
			doIt6();		
		}
		else { // do not show numbering
			change('fnumberingstartshow','noshow'); 
			writit('&nbsp;','line5');	
		}
	}	
}
function checknumbering() { // if Style E do not show
	if (document.form1.fstyle5.checked) { // style E
		change('fnumberingall','noshow');
		change('fnumberingstartshow','noshow'); 
	}
	else {
		change('fnumberingall','show');
		change('fnumberingstartshow','show');
	}
}
function changenumberingstart() {
	if (document.form1.fnumbering0.checked) { // show numbering
		change('fnumberingstartshow','show');
			doIt6();
	}
	else { // do not show numbering
		change('fnumberingstartshow','noshow'); 
		change('line5','noshow');
	}
}


function doIt1()
{ // update line 1
	var txt = '&nbsp;';
	txt += document.forms[0].ftext1.value;
	writit(txt,'line1');
}
function doIt2()
{ // update line 2
	var txt = '&nbsp;';
	txt += document.forms[0].ftext2.value;
	writit(txt,'line2');
}
function doIt3()
{ // update line 3 with text
	var txt = '&nbsp;';
	txt += document.forms[0].ftext3.value;
	writit(txt,'line3');
	//changelines();
}
function doIt4()
{ // update line 4
	var txt = '&nbsp;';
	txt += document.forms[0].ftext4.value;
	writit(txt,'line4');
	//changelines();
}
function doIt4E()
{ // update line 4
	var txt = '&nbsp;';
	txt += document.forms[0].ftext4.value;
	writit(txt,'line5');
	//changelines();
}
function doIt5()
{	// update line 5 with text
	var txt = '&nbsp;';
	txt += document.forms[0].ftext5.value;
	writit(txt,'line5');
	//changelines();
}
function doIt6()
{  // update line 5 with number information
	var txt = '&nbsp;';
	txt += document.forms[0].fnumberingstart.value;
	writit(txt,'line5');
	
	if (document.form1.fcolour0.checked) {change('line5','showbold');} // change text colour of line 5
	if (document.form1.fcolour1.checked) {change('line5','showbold');}
	if (document.form1.fcolour2.checked) {change('line5','showbold');}
	if (document.form1.fcolour3.checked) {change('line5','showbold');}
}

function writit(text,id)
{
	if (document.getElementById)
	{
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	}
	else if (document.all)
	{
		x = document.all[id];
		x.innerHTML = text;
	}
	else if (document.layers)
	{
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>;';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}
function changemaxlength2(id,id2,length,fn) { // this changes the attributes of the input tag
		identity2 = document.getElementById(id);
		
		var Size = length;
	    var Value = identity2.value; // need to grab value
		if (Value.length > Size) { 
			var newValue = Value.substring(0,Size); 
		}
		else {
			var newValue = Value;
		}
		var num = id2.substring(5,6);
		text = '<span name="';
		text = text + id2;
		text = text + '" id="';
		text = text + id2;
		text = text + '"><label>Line ';
		text = text + num ;
		text = text + ':</label> <input name="';
		text = text + id ;
		text = text + '" id="';
		text = text + id;
		text = text + '" size ="'; 
		text = text + length ;
		text = text + '" maxlength = "'; 
		text = text + length;
		text = text + '" value = "';
		text = text + newValue; 
		text = text + '" onchange="javascript:'; 
		text = text + fn ;
		text = text + '();" onblur="javascript:'; 
		text = text + fn ;
		text = text + '();"></span>';
		writit(text,id2);
}
function changemaxlength3(id,id2,length,fn) { // this changes the attributes of the input tag
		identity2 = document.getElementById(id);
		
		var Size = length;
	    var Value = identity2.value; // need to grab value
		if (Value.length > Size) { 
			var newValue = Value.substring(0,Size); 
		}
		else {
			var newValue = Value;
		}
		text = '<span name="num" id="num"><label for="fnumberingstart">Start No:</label> <input name= "';
		text = text + id ;
		text = text + '" id= "';
		text = text + id;
		text = text + '" size ="'; 
		text = text + length ;
		text = text + '" maxlength = "';
		text = text + length;
		text = text + '" value = "';
		text = text + newValue ;
		text = text + '" onchange="javascript:'; 
		text = text + fn ;
		text = text	+ '();" onblur="javascript:';
		text = text + fn ;
		text = text + '();"></span>';
		writit(text,id2);
}