/**
 * @author   Sven
 * --------------------
*/


//Onfocus: Reset the style and the checked of a line back to init
/*
Used in:
includes/checkTet.php
includes/ordertypelines.php
*/
function resetLine(lineNum, fieldName) 
{
	document.getElementById(fieldName+lineNum).style.color = '#000000';
	if(fieldName == 'ref') 
	{
		document.getElementById('checked'+lineNum).value = 0;
		document.getElementById('dispspan'+lineNum).innerHTML = '<input id="minQty'+lineNum+'" type="hidden" value="" />';
	}
}



//Onfocus: Clear the input fields of the example text, if present, and set the exmpl field to 1
/*
function clearExample(refExample, qteExample) 
{
 if(document.getElementById('ref1').value == refExample && document.getElementById('qte1').value == qteExample) 
 {
  document.getElementById('ref1').value = '';
  document.getElementById('qte1').value = '';
  document.getElementById('exmpl').value = 1;
 }
}
*/


//Onfocus: Ajust the Style of the Reference for further use, do an initial check, if not conform regexp's checked = 0 else = 1
/*
Used in:
includes/checkTet.php
includes/ordertypelines.php
*/
function styleRef(lineNum) 
{
 var aref = document.getElementById('ref'+lineNum).value;
 //remove all spaces, '.' ,'-', '_'
 var reg = /((\s{1,})|(\.)|(-)|(_))/;
 var i = 1;
 while(i == 1) 
 {
  var match = reg.test(aref);
  if(match) aref = aref.replace(reg, '');
  else i=0;
 }
 //Check for starting with Prefix (BG1, BV9, ...) followed by 4 or 5 chars or only 4 or 5 chars, add a 0 for starters when only 4
 var prefix = '';
 reg1 = /(^([BNbn]{1}[A-Za-z]{1}[\d-A-Za-z]{1})((\d{4})|(\d{5}))$)/;
 match1 = reg1.exec(aref);
 reg2 = /(^((\d{4})|(\d{5}))$)/;
 match2 = reg2.exec(aref);
 if(match1) 
 {
  reg = /(^([A-Za-z]{2}[\d-A-Za-z]{1}))/;
  match = reg.exec(aref);
  prefix = match[0];
  prefix = prefix+' ';
  aref = aref.substr(3);
  aref = aref.toString();
  if(aref.length == 4) aref = '0'+aref; 
 } 
 else if(match2) 
 {
  if(aref.length == 4) aref = '0'+aref; 
 }
 if(match1 || match2) 
 {
  document.getElementById('ref'+lineNum).value = prefix+aref.substr(0,3)+aref.substr(3,2);
  document.getElementById('ref'+lineNum).style.color = '#000000';
  document.getElementById('checked'+lineNum).value = 1;
 } 
 else 
 {
  document.getElementById('ref'+lineNum).value = aref;
  document.getElementById('ref'+lineNum).style.color = '#FF0000';
  document.getElementById('checked'+lineNum).value = 0;
  document.getElementById('dispspan'+lineNum).innerHTML = '<input type="hidden" id="minQty'+lineNum+'" value="" />';
 }
}



//Check if the reference is a Tete-option
/*
Used in:
includes/checkTet.php
includes/ordertypelines.php
*/
function checkTet(lineNum) 
{
 //Is the reference conform the regexp
 if(document.getElementById('checked'+lineNum).value && (document.getElementById('checked'+lineNum).value == 1) && (document.getElementById('tete'+lineNum).value == 0)) 
 {
  loadFile('post', '/includes/checkTet.php', 'lineNum='+lineNum+'&lineVal='+document.getElementById('ref'+lineNum).value, 'json', postCheckTet);
 }
}

/*
function postCheckTet(data)
{
	if(isSet(data.catalog))
	{
		document.getElementById('div'+data.lineNum).innerHTML=data.refBox;
		if(data.product == 1)
		{
			document.getElementById('tete'+data.lineNum).value=1;
		}
		checkDispVars(data.lineNum);
	}
	else
	{
		startAlert(16);
		document.getElementById('ref'+data.lineNum).value='';
	  document.getElementById('qte'+data.lineNum).value='';
	}
}
*/


//Handles the return option of the select
/*
Used in:
includes/checkTet.php
*/
function handleTet(lineNum) 
{
 if(document.getElementById('checked'+lineNum).value != 1)
 {
	 document.getElementById('checked'+lineNum).value = 1;
 }
 if(document.getElementById('tete'+lineNum).value == 1 && document.getElementById('ref'+lineNum).options[document.getElementById('ref'+lineNum).selectedIndex].value == 'clear') 
 {
  var inputHTML = '<input type="hidden" name="tete'+lineNum+'" id="tete'+lineNum+'" value="0" autocomplete="off" /><input type="text" name="ref'+lineNum+'" id="ref'+lineNum+'" value="" tabindex="'+((lineNum*2)-1)+'" class="input-large" onfocus="resetLine('+lineNum+',\'ref\');" onblur="styleRef('+lineNum+');checkTet('+lineNum+');checkDispVars('+lineNum+');" />';
  document.getElementById('div'+lineNum).innerHTML = inputHTML;
  document.getElementById('dispspan'+lineNum).innerHTML = '';
 } 
 else if(document.getElementById('tete'+lineNum).value == 1 && document.getElementById('ref'+lineNum).options[document.getElementById('ref'+lineNum).selectedIndex].value == 'select') 
 {
  document.getElementById('checked'+lineNum).value = '0';
  document.getElementById('dispspan'+lineNum).innerHTML = '<img alt="" src="/images/general/layout/pictostockrouge.gif" /><input type="hidden" value="0" name="disp'+lineNum+'" id="disp'+lineNum+'" />';
 } 
 else if(document.getElementById('tete'+lineNum).value == 1) 
 {
  aref = document.getElementById('ref'+lineNum).options[document.getElementById('ref'+lineNum).selectedIndex].value;
  loadFile('post', '/includes/checkDisp.php', 'quickRef='+aref+'&quickLine='+lineNum, 'text', 'dispspan'+lineNum);
 }
}


/*
function checkDispVars(lineNum) 
{
	if(document.getElementById('checked'+lineNum).value && (document.getElementById('checked'+lineNum).value == 1)) 
	{
		var aref, unique_article;
		if(document.getElementById('tete'+lineNum).value == 0) 
		{
			aref = document.getElementById('ref'+lineNum).value;
			unique_article = '';
		} 
		else 
		{
			aref = document.getElementById('ref'+lineNum).options[document.getElementById('ref'+lineNum).selectedIndex].value;
			if(aref!="select")
			{
				unique_article = document.getElementById(aref).value;
			}
		}
			
		if(aref!="select")
		{
//remove all spaces, '.' ,'-', '_'
			var reg = /((\s{1,})|(\.)|(-)|(_))/;
			var i = 1;
			while(i == 1)
			{
				var match = reg.test(aref);
				if(match) aref = aref.replace(reg, '');
				else i = 0;
			}
		}
		if(aref!="select")
		{
			loadFile('post', '/includes/checkDisp.php', 'quickRef='+aref+'&quickLine='+lineNum, 'text', 'dispspan'+lineNum);
		}
	}
}
*/


//Checks if the ammount good for the min.ammount 
/*
function checkAmount(lineNum, div,minAmount,totAmount) 
{
 if(document.getElementById('checked'+lineNum).value == 1) 
 {
  var times = Math.floor(totAmount/minAmount);
  var newAmount = times * minAmount;
  if(totAmount != newAmount) {
   startAlert(7, { "minAmount": minAmount, "newAmount":  newAmount });
   document.getElementById(div).value = newAmount;
  }
 }
}
*/