// SVTAddRows.js

function addSicRows(url,cmd,perform,cdTyp,isPriCd,imgPath){
   var eleIndex = parseInt(document.forms[0].hSicEleIndex.value,10);
   var sicLen=parseInt(document.forms[0].asicLen.value,10);
   var element = null;
   
   aElement = eval ("document.forms[0].txtPSIC");
   aElementVal = trim(aElement.value);
   if(aElementVal.length > 0) {
   		if (!(isValidWholeNumber(aElementVal))) {
   			alert('Invalid Data In Primary SIC. Primary SIC can have Numbers only.');
			aElement.focus();
   			return false; 
   		}
	}
 
	   for(var count = 0;count <= eleIndex;count++) {
           element = eval("document.forms[0].txtASIC"+count);
		   var strData = "";
		   strData = trim(element.value);
			if(strData.length > 0) {
	   			if (!(isValidWholeNumber(strData))) {
	   				alert('Invalid Data In Additional SIC. Additional SIC can have Numbers only.');
					element.focus();
	   				return false; 
	   			}
		   	} else {
	   			alert('One or More Rows are Empty');
	   			element.focus();
	   			return false;
			}
		}
	
       element = eval("document.forms[0].txtASIC"+eleIndex);
	   var oRow1=sicTable.insertRow(sicLen);
	   var aRows=sicTable.rows;
	   var aCells=oRow1.cells;
  	   eleIndex = eleIndex + 1;
	   var oCell1_1=aRows(oRow1.rowIndex).insertCell(aCells.length); 
	   var oCell1_2=aRows(oRow1.rowIndex).insertCell(aCells.length);
	   var xcol="&nbsp;";
	   var ycol="<input type='text' name='txtASIC"+eleIndex+"' class='textSize' value=''id = 'Additional SIC' size='10' maxlength='10'";
	   ycol = ycol + "<INPUT type ='hidden' name='hASIC"+eleIndex+"'>";	

	   
	   //oCell1_1="<td class="darkGrayedTD"> &nbsp;</td>";
	   oCell1_1.innerHTML=xcol;
	   oCell1_2.innerHTML=ycol;
	   document.forms[0].asicLen.value=sicLen+1;
	   document.forms[0].hSicEleIndex.value = eleIndex;

}

 function addNaicsRows(url,cmd,perform,cdTyp,isPriCd,imgPath){
 	var eleIndex = parseInt(document.forms[0].hNaicsEleIndex.value,10);
    var naicsLen=parseInt(document.forms[0].aNaicsLen.value,10);
    var element = null;
    
   aElement = eval ("document.forms[0].txtPNAICS");
   aElementVal = trim(aElement.value);
   if(aElementVal.length > 0) {
   		if (!(isValidWholeNumber(aElementVal))) {
   			alert('Invalid Data In Primary NAICS. Primary NAICS can have Numbers only.');
			aElement.focus();
   			return false; 
   		}
	}

    for(var count = 0;count <= eleIndex;count++) {
	   element = eval("document.forms[0].txtANAICS"+count);
	   var strData = "";
  	   strData = trim(element.value);
	   if(strData.length > 0) {
			if (!(isValidWholeNumber(strData))) {
				alert('Invalid Data In Additional NAICS. Additional NAICS can have Numbers only.');
				element.focus();
				return false;
			}
		} else {
			alert('One or More Rows are Empty');
			element.focus();
			return false;
	   	}
	}
	
	   element = eval("document.forms[0].txtANAICS"+eleIndex);
	   var oRow1=naicsTable.insertRow(naicsLen);
	   var aRows=naicsTable.rows;
	   var aCells=oRow1.cells;
	   eleIndex = eleIndex + 1;
	   var oCell1_1=aRows(oRow1.rowIndex).insertCell(aCells.length);
	   var oCell1_2=aRows(oRow1.rowIndex).insertCell(aCells.length);
	   var xcol="&nbsp;";
	   var ycol="<input type='text' name='txtANAICS"+eleIndex+"' class='textSize' value='' size='10' maxlength='10'";
	   ycol = ycol + "<INPUT type ='hidden' name='hANAICS"+eleIndex+"'>";
	   
	   
	   //oCell1_1="<td class="darkGrayedTD"> &nbsp;</td>";
	   oCell1_1.innerHTML=xcol;
	   oCell1_2.innerHTML=ycol;
   	   document.forms[0].aNaicsLen.value=naicsLen+1;
       document.forms[0].hNaicsEleIndex.value = eleIndex;
}

function addVndrNbrRows(url,cmd,perform,imgPath){
   var eleIndex = parseInt(document.forms[0].hVndrEleIndex.value,10);
   var vndrLen=parseInt(document.forms[0].vndrLen.value,10);
   var element = null;

	   for(var count = 0;count <= eleIndex;count++) {
           element = eval("document.forms[0].txtVndrNbr"+count);
		   var strData = "";
		   strData = trim(element.value);
			if(strData.length > 0) {
	   			if (!(isValidWholeNumber(strData))) {
	   				alert('Invalid Data In Vendor Number. Vendr Number can have Numbers only.');
					element.focus();
	   				return false; 
	   			}
		   	} else {
	   			alert('One or More Rows are Empty');
	   			element.focus();
	   			return false;
			}
		}
	
       element = eval("document.forms[0].txtVndrNbr"+eleIndex);
	    var oRow1=vndrNbrTable.insertRow(vndrLen);
	   var aRows=vndrNbrTable.rows;
	   var aCells=oRow1.cells;
	   var oCell1_1=aRows(oRow1.rowIndex).insertCell(aCells.length);
	   var oCell1_2=aRows(oRow1.rowIndex).insertCell(aCells.length);
  	   eleIndex = eleIndex + 1;
	   var xcol="&nbsp;";
	   var ycol="<input type='text' name='txtVndrNbr"+eleIndex+"' class='textSize' value=''id = 'Vendor Number' size='10' maxlength='10'";
	   ycol = ycol + "<INPUT type ='hidden' name='hVndrNbr"+eleIndex+"'>";	

	   
	   //oCell1_1="<td class="darkGrayedTD"> &nbsp;</td>";
	   oCell1_1.innerHTML=xcol;
	   oCell1_2.innerHTML=ycol;
	   document.forms[0].vndrLen.value=vndrLen+1;
	   document.forms[0].hVndrEleIndex.value = eleIndex;

}

/*
*	Method Name: Function addReferenceRow()
*	The function will be called on click of Add more button
*	It will dynamically creates the rows with components
*/
var iNoOfActvAddedRow = 0;
function addReferenceRow(screenName){
	var frm = document.forms[0];
	var tbl = document.getElementById('refTable');
  	var lastRow = tbl.rows.length;
  	var refCmpnyNme = "";
	var refCtctNme = "";
  	for(var i = 0; i < lastRow - 1; i++){
		refCmpnyNme = document.getElementById('Reference Company Name'+i).value;
		refCtctNme = document.getElementById('Reference Name'+i).value;

		if((trim(refCmpnyNme) == "") && (trim(refCmpnyNme) == "") &&
			((frm.elements["txtRefPh"+ i][0].value == "") && (frm.elements["txtRefPh"+ i][1].value == "") && 
			(frm.elements["txtRefPh"+ i][2].value == ""))){
			alert("One or more rows are empty.");
			document.forms[0].elements["txtRefCmpny"+ i].focus();
			return;
		}
	}
  	if(lastRow > 3){
  		alert("Maximum only 3 References can be added.");
  		return;
  	}
  	
  	var iteration = lastRow - 1;
  	
  
  	   var oRow1=refTable.insertRow(lastRow);
	   var aRows=refTable.rows;
	   var aCells=oRow1.cells;
	   if("Modify" == screenName){
	   oRow1.bgColor='#DBDBDB';
	   }
	   var oCell1_1=aRows(oRow1.rowIndex).insertCell(aCells.length);
	   var oCell1_2=aRows(oRow1.rowIndex).insertCell(aCells.length);
	   var oCell1_3=aRows(oRow1.rowIndex).insertCell(aCells.length);
	   
	   var xcol="<input type='text' name='txtRefCmpny"+iteration+"' class='textSize' value='' id = 'Reference Company Name"+iteration+"' size='31' maxlength='100'>"; 
	   var ycol="<input type='text' name='txtRefNme"+iteration+"' class='textSize' value='' id = 'Reference Name"+iteration+"' size='31' maxlength='31'>"; 
	   var zcol="( <input type='text' name='txtRefPh"+iteration+"' class='textSize' value='' id = 'Reference phone number' size='3' maxlength='3' " +
	   			"onkeyup = javascript:tabNext(this,'3',document.forms[0].txtRefPh"+iteration+"[1])> ) " +
	   			" - <input type='text' name='txtRefPh"+iteration+"' class='textSize' value='' id = 'Reference phone number' size='3' maxlength='3' " +
	   			"onkeyup = javascript:tabNext(this,'3',document.forms[0].txtRefPh"+iteration+"[2])> - " +
	   			"<input type='text' name='txtRefPh"+iteration+"' class='textSize' value='' id = 'Reference phone number' size='4' maxlength='4'> "; 
	   	   
	   oCell1_1.innerHTML=xcol;
	   oCell1_2.innerHTML=ycol;
	   oCell1_3.innerHTML=zcol;
}


/*
*	Method Name: Function addCertficationRow()
*	The function will be called on click of Add more button
*	It will dynamically creates the rows with components
*/

var arryCert0 = new Array();
var arryCert1 = new Array();
var arryCert2 = new Array();
var arryCert3 = new Array();
var arryCert4 = new Array();
var arryCert5 = new Array();
var arryCert6 = new Array();


function addCertficationRow(screenName,certCount,calLink){
	var tbl = document.getElementById('certDetails'+certCount);
	var tableName='certDetails'+certCount;
  	var lastRow = tbl.rows.length;
  
  	var certNum = "";
	var certDate = "";
	var certAgy = "";
	var oldcertAgy="";
	var arrayName=eval("arryCert"+certCount);
	var iterator=0;
	var rowid=0;
		
	if(arrayName.length>0)
 		{
 			iterator=arrayName.length-1;
 			
 		}
 		else
 		{
 			iterator=lastRow-1;
 		}
	
  	for(var i = 0; i <= iterator; i++){
 		if(arrayName.length>0)
 		{
 	
 			rowid=arrayName[i];
 			
			
		}
		else
		{
			rowid=i;
			
			
		}
			certNum = document.getElementById('Certification Number'+certCount+rowid).value;
			certDate = document.getElementById('Certification Expiry Date'+certCount+rowid).value;
			certAgy = document.getElementById('Certification Agency'+certCount+rowid).value;
		if((trim(certNum) == "") && (trim(certDate) == "") && (trim(certAgy) == ""))
		{
			alert("One or more rows are empty.");
			document.forms[0].elements["txtCertNbr"+certCount+rowid].focus();
			return;
		}
		if (!(isNotEmptySpace(certNum))) { 
				alert("Please enter a valid Certfication Number. Certfication Number cannot be blank."); 
				document.forms[0].elements["txtCertNbr"+certCount+rowid].focus();
				return;
		}
		if (!(checkForAlphaNum(certNum))) { 
					
			alert("Please enter a valid Certfication Number. Certfication Number can only be alphanumeric."); 
			document.forms[0].elements["txtCertNbr"+certCount+rowid].focus();
			return;
		}
			
		if (!(isNotEmptySpace(certDate))) { 
				alert("Please enter a valid Certfication Date. Certfication Date cannot be blank."); 
				document.forms[0].elements["txtExDt"+certCount+rowid].focus();
				return;
		}
		if(!(isValidDate(certDate))) {
			alert('Invalid date in Certification Expiry Date field. Please enter a valid date in MM/DD/YYYY format');
			document.forms[0].elements["txtExDt"+certCount+rowid].focus();
			return;
		}
		if (!(isNotEmptySpace(certAgy))) { 
				alert("Please enter a valid Certfication Agency. Certfication Agency cannot be blank."); 
				document.forms[0].elements["txtCertAgy"+certCount+rowid].focus();
				return;
		}
		if ((isNotEmptySpace(oldcertAgy)))  
		{
			if(-1 != (oldcertAgy.indexOf(certAgy) ))
			{
				alert("Certification Agency Name already exists");
				document.forms[0].elements["txtCertAgy"+certCount+rowid].focus();
				return ;
			}
		}
		if (!(checkForAlphaNum(certAgy))) { 
			alert("Please enter a valid Certfication Agency. Certfication Agency can only be alphanumeric."); 
			document.forms[0].elements["txtCertAgy"+certCount+rowid].focus();
			return;
		}
		
		oldcertAgy=oldcertAgy+certAgy;
	}
   
   	var tbody = document.getElementById(tableName).getElementsByTagName("TBODY")[0];
    
    // create row
    var row = document.createElement("TR");
  
 
  if(arrayName.length>0)
  {
  	 rowid=arrayName[iterator]+1;
 	
  }
   else{ 
    rowid=lastRow;
   
	
	}
	
	row.setAttribute('id',rowid);
    // create table cell 1
    var td1 = document.createElement("TD");
	var xcol="<input type='text' name='txtCertNbr"+certCount+rowid+"' class='textSize' value='' id = 'Certification Number"+certCount+rowid+"' size='10' maxlength='10'>"; 
	td1.innerHTML=xcol;
		
	// create table cell 2
    var td2 = document.createElement("TD");
	var ycol="<input type='text' name='txtExDt"+certCount+rowid+"' class='textSize' value='' id = 'Certification Expiry Date"+certCount+rowid+"' size='10' maxlength='10'>" ; 
	td2.innerHTML=ycol;

	// create table cell 3
    var td3 = document.createElement("TD");
    var fieldName = 'document.frmSuppEntry.txtExDt'+certCount+rowid;
	var anc = document.createElement("a");
	anc.setAttribute("href","javascript:openCalendar("+fieldName+",'');");
	var img = document.createElement('img');
	img.src = calLink+'calender.gif';
	img.border='0';
	anc.appendChild(img);
	td3.appendChild(anc);
  
	// create table cell 4
    var td4 = document.createElement("TD");
	var zcol="<input type='text' name='txtCertAgy"+certCount+rowid+"' class='textSize' value='' id = 'Certification Agency"+certCount+rowid+"' size='10' maxlength='50'>" ;
	td4.innerHTML=zcol;
	
	// create table cell 5
    var td5 = document.createElement("TD");
 	var delCol="<A href='javascript:delRow("+rowid+","+certCount+");'><IMG src='"+calLink+"Remove.gif' border='0'></A>";
	td5.innerHTML=delCol;
	
	
	
	// append data to row
    row.appendChild(td1);
    row.appendChild(td2);
    row.appendChild(td3);
    row.appendChild(td4);
    row.appendChild(td5);
  	tbody.appendChild(row);
  	
    	if(arrayName.length==0)
    	{
   	arrayName[0]=0;
   	}
   	
   	if(arrayName.length>0)
  {
  	 rowid=arrayName[iterator]+1;
 	
 	arrayName[arrayName.length]=rowid;
  }
   else{ 
    rowid=lastRow;
   arrayName[rowid]=rowid;
	
	}
   	
   	
}
function delRow(rowNum,certCount)
{
	
	var tbl = document.getElementById('certDetails'+certCount); // table reference   
	var lastRow = tbl.rows.length;
	
	
	
	if(1==lastRow)
	{
		alert("Sorry! All rows cannot be deleted.");
		return;
	}
	
	var arrayName=eval("arryCert"+certCount);
	var pos= getPosition(arrayName,rowNum);
	
	tbl.deleteRow(pos);   

	arrayName.splice(pos,1);
	
	
 
  }

function getPosition(arrayName,arrayItem)
{

    for(var i=0;i<arrayName.length;i++){ 
       if(arrayName[i]==arrayItem)
            return i;
    }
}

