function insertAdjacentElement(obj,where,parsedNode){
		switch (where){
		case 'beforeBegin':
			obj.parentNode.insertBefore(parsedNode,obj)
			break;
		case 'afterBegin':
			obj.insertBefore(parsedNode,obj.firstChild);
			break;
		case 'beforeEnd':
			obj.appendChild(parsedNode);
			break;
		case 'afterEnd':
			if (obj.nextSibling) 
			obj.parentNode.insertBefore(parsedNode,obj.nextSibling);
			else obj.parentNode.appendChild(parsedNode);
			break;
		}
}

function FindParentNode(obj, nodeToFind){
	
	 var node = obj.parentNode;
    while (node && node.nodeName.toUpper != "BODY")
    {
				if (node.nodeName == nodeToFind)
        {
            return node;
        }
        node = node.parentNode;
    }
    return null;
}

function ReapplyStyles(obj){

	var tbody;
	var trclass;
	

	tbody = obj.parentNode;
	for (var i=0;i<tbody.childNodes.length;i++){
			trclass = tbody.childNodes(i).className;
			if (trclass == "even" || trclass == "odd"){
					if (i % 2 == 1){
						tbody.childNodes(i).className = "odd";
					}
					else {
						tbody.childNodes(i).className = "even";
					}
			}
			else
				break;
	}	

}

function InsertRow(obj, nodeToFind, whereToInsert){

	var entry = FindParentNode(obj, nodeToFind);
	var newRow;
	var pkg;
	var hiddenpkg='';
	var nonhidden = 0;
	var inputid;
	var imgid;
	var tmp;
	var tmp2;
	var rand=Math.floor(Math.random()*100001)
	var bFirstInput;
  var newpkg='';

	bFirstInput = true;

//alert(entry.innerHTML);
	insertAdjacentElement(entry, whereToInsert, newRow = entry.cloneNode(true));

	//var spans = newRow.getElementsByTagName('SPAN');
	var inputs = newRow.getElementsByTagName('INPUT');
	var imgs = newRow.getElementsByTagName('IMG');
	
 for (var i =0; i < imgs.length; i++){
		try {
			// this is needed for select divs
			if (imgs[i].getAttribute('InputID').indexOf('INPUT') > -1){
					imgid = imgs[i].getAttribute('InputID');
					imgs[i].setAttribute("InputID",imgid+'_'+rand)
			}
		}
		catch (error) {
		}
	}

	for (var j=0; j < inputs.length; j++){ 
		var new_id = inputs[j].getAttribute('RecID') + rand;

			// this condition is needed for select divs
			if (inputs[j].getAttribute('ID').indexOf('INPUT') > -1){
					inputid = inputs[j].getAttribute('ID');
					if (inputid.indexOf('B') > -1){
						tmp = inputid.replace("B", '_' + rand + 'B');
						inputs[j].id = 'n_e_w' + tmp;
						inputs[j].setAttribute('RecID', 'n_e_w' + new_id);
					}
					else{
						inputs[j].id = 'n_e_w' + inputid+'_'+rand
					}
			} else {
				
				inputs[j].id = 'n_e_w' + new_id;
				inputs[j].setAttribute('RecID', 'n_e_w' + new_id);
			}

			// end of select div condition

			if (inputs[j].getAttribute('type') == "hidden"){
					try{
						// this is needed for popups
						if (inputs[j].getAttribute('popup') == "yes"){
								inputid = inputs[j].getAttribute('ID');
								inputs[j].setAttribute('Changed',0);
								inputs[j].id = 'n_e_w' + inputs[j].getAttribute('ID') + rand
								inputs[j].name = inputs[j].name + rand
								inputs[j].parentNode.childNodes(2).innerHTML = 'None ';
								inputs[j].parentNode.childNodes(2).id = inputs[j].parentNode.childNodes(2).getAttribute('ID') + rand
								inputs[j].parentNode.childNodes(2).setAttribute('onclick', "");
								tmp = inputid + rand
								inputs[j].parentNode.childNodes(3).setAttribute('pid',tmp);
						}
						else{
								inputs[j].setAttribute("Changed",1);
						}
					}
					catch (error) {}
				
			}
			else{
				// if it isn't a hidden input, we need to clear it's value
								
					inputs[j].value = '';
					
			}	

			if (inputs[j].getAttribute('pkg')){
				if (inputs[j].getAttribute('pkg').indexOf('Job') > 0) inputs[j].value = '';
					newpkg = inputs[j].getAttribute('pkg');
					inputs[j].setAttribute('new', 'yes');
					FlagChangeV2(inputs[j]);
				//	/*if (bFirstInput == true && inputs[j].getAttribute("Changed") == "1")	{ 
				//			alert('first');
				//			inputs[j].setAttribute('first', 'yes');
				//			bFirstInput = false;
				//			alert(inputs[j].parentNode.innerHTML);
				//	
				//	}*/
			}


	
/*
			else{
					nonhidden++;
					inputs[j].value = '';
					pkg = inputs[j].getAttribute('pkg');
					if (pkg != null){
						if (j == 1) { 
							inputs[j].setAttribute('pkg','@@a=i' + pkg);
						}
						else{
							inputs[j].setAttribute('pkg','@@a=new' + pkg);
						}
					}
			}
*/
	 
	}
	
	for (var x=0; x < inputs.length; x++){ 
			if (inputs[x].getAttribute('first') == "yes"){
					//alert('first ' + newpkg);
					inputs[x].setAttribute("Changed",1);
					inputs[x].setAttribute('new', 'yes');
					inputs[x].setAttribute('pkg',newpkg);
					
			}
	}
	
	//alert(newRow.innerHTML);
	ReapplyStyles(newRow);
}







