//Universal scripts

function autoresize(txtbox) {
    var cols = txtbox.cols ;
    var content = txtbox.value ;
    var lineCount = 0 ;

    var lastEOL = -1 ;
    do {
        var begin = lastEOL+1 ;
        lastEOL = content.indexOf("\n",lastEOL+1) ;
        var line = "" ;
        if(lastEOL != -1) {
            line = content.substring(begin,lastEOL) ;
        } else {
            line = content.substring(begin,content.length) ;
        }
        var rows_in_line = Math.floor(line.length/cols)+1 ;
        lineCount += rows_in_line
    } while (lastEOL != -1) ;
    txtbox.rows = lineCount ;
}

//scripts for stage 2
//need to be consolidated - a lot of duplication here

//Adds fields for adding a manager to the set of fieldsets for an asset
function addManager(num_assets) {
	//Find div for managers for this asset
	var asset_managers_id = "asset_managers["+num_assets+"]";
	var ni = document.getElementById("asset_managers["+num_assets+"]");
	//Find out how many managers there are currently
	var numi = document.getElementById("asset_manager_count["+num_assets+"]");
	//Update the number of managers
	var num = new Number(document.getElementById("asset_manager_count["+num_assets+"]").value) ;
	//Remove note from current last manager
	var newManLink = document.getElementById('note_asset_manager['+num_assets+']');
	var newManLinkPar = newManLink.parentNode;
	newManLinkPar.removeChild(newManLink);
	//Create and add form elements
	var newdiv = document.createElement('div');
	newdiv.setAttribute("class",'form_row');
	newdiv.innerHTML = "<label for='asset_manager["+num_assets+"]["+num+"']>Asset Manager "+(num + 1)+"</label><input value='_new' id='asset_manager_id["+num_assets+"]["+num+"]' name='asset_manager_id["+num_assets+"]["+num+"]' type='hidden' /><input id='asset_manager_name["+num_assets+"]["+num+"]' class='audit_input' size='20' name='asset_manager_name["+num_assets+"]["+num+"]' type='text' />";
	//Add note to new last manager
	newdiv.appendChild(newManLink);
	ni.appendChild(newdiv);
	numi.value = num + 1;
}

//Adds fields for adding a reference to the set of fieldsets for an asset
function addReference(num_assets) {
	//Find div for references for this asset
	var asset_references_id = "asset_references["+num_assets+"]";
	var ni = document.getElementById("asset_references["+num_assets+"]");
	//Find out how many references there are currently
	var numi = document.getElementById("asset_reference_count["+num_assets+"]");
	//Update the number of references
	var num = new Number(document.getElementById("asset_reference_count["+num_assets+"]").value) ;
	//Remove note from current last reference
	var newRefLink = document.getElementById('note_asset_reference['+num_assets+']');
	var newRefLinkPar = newRefLink.parentNode;
	newRefLinkPar.removeChild(newRefLink);
	//Create and add form elements
	var newdiv = document.createElement('div');
	newdiv.setAttribute("class",'form_row');
	newdiv.innerHTML = "<label for='asset_reference["+num_assets+"]["+num+"']>Reference "+(num + 1)+"</label><input value='_new' id='asset_reference_id["+num_assets+"]["+num+"]' name='asset_reference_id["+num_assets+"]["+num+"]' type='hidden' /><input id='asset_reference_title["+num_assets+"]["+num+"]' class='audit_input' size='20' name='asset_reference_title["+num_assets+"]["+num+"]' type='text' value='Title'/><input id='asset_reference_uri["+num_assets+"]["+num+"]' class='audit_input' size='20' name='asset_reference_uri["+num_assets+"]["+num+"]' type='text' value='URI'/>";
	//Add note to new last reference
	newdiv.appendChild(newRefLink);
	ni.appendChild(newdiv);
	numi.value = num + 1;
}

//Adds fields for adding a new asset
function addAsset(asset_fields) {
	var ni = document.getElementById('newAssets');
	var numi = document.getElementById('assetValue');
	var num = new Number(document.getElementById("assetValue").value);
	var divIdName = "asset["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = updateIDs(asset_fields, num);
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	numi.value = num + 1;
}

//Replaces "next" in string with num
function updateIDs(str, num) {
	return str.replace(/nextAssetNum/g, num);
}

//scripts for stage 3

function addCreator() {
	var ni = document.getElementById('creators_block');
	var numi = document.getElementById('creator_count');
	var num = new Number(document.getElementById("creator_count").value);
	var divIdName = "af3_data_creators["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_data_creator["+num+"]\">Data Creator "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_data_creators["+num+"]\" name=\"af3_data_creators["+num+"]\" size=\"30\" type=\"text\" /><div class=\"form_note\" id=\"note_af3_data_creators["+num+"]\"><a class=\"help\" onhover=\"\">?<span>Person, group or organisation responsible for the intellectual content of the data asset</span></a> <a href=\"javascript:;\" onclick=\"addCreator();\">Add a Creator</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_data_creators['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>Person, group or organisation responsible for the intellectual content of the data asset</span></a>";
	numi.value = num + 1;
}

function addTitle() {
	var ni = document.getElementById('titles_block');
	var numi = document.getElementById('title_count');
	var num = new Number(document.getElementById("title_count").value);
	var divIdName = "af3_data_titles["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_data_title["+num+"]\">Data Title "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_data_titles["+num+"]\" name=\"af3_data_titles["+num+"]\" size=\"30\" type=\"text\" /><div class=\"form_note\" id=\"note_af3_data_titles["+num+"]\"><a class=\"help\" onhover=\"\">?<span>Official name of the data asset, with additional or alternative titles or acronyms if they exist</span></a> <a href=\"javascript:;\" onclick=\"addTitle();\">Add a Title</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_data_titles['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>Official name of the data asset, with additional or alternative titles or acronyms if they exist</span></a>";
	numi.value = num + 1;
}

function addFormat(extended) {
	var commentStr;
	if (extended) {
		commentStr = "File format(s) and their version(s) the data asset is using";
	}
	else {
		commentStr = "Physical formats of data asset, including file format information";
	}
	var ni = document.getElementById('formats_block');
	var numi = document.getElementById('format_count');
	var num = new Number(document.getElementById("format_count").value);
	var divIdName = "af3_data_formats["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_data_format["+num+"]\">Data Format "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_data_formats["+num+"]\" name=\"af3_data_formats["+num+"]\" size=\"30\" type=\"text\" /><div class=\"form_note\" id=\"note_af3_data_formats["+num+"]\"><a class=\"help\" onhover=\"\">?<span>"+commentStr+"</span></a> <a href=\"javascript:;\" onclick=\"addFormat();\">Add a Format</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_data_formats['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>"+commentStr+"</span></a>";
	numi.value = num + 1;
}

function addType() {
	var ni = document.getElementById('types_block');
	var numi = document.getElementById('type_count');
	var num = new Number(document.getElementById("type_count").value);
	var divIdName = "af3_data_types["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_data_type["+num+"]\">Data Type "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_data_types["+num+"]\" name=\"af3_data_types["+num+"]\" size=\"30\" type=\"text\" /><div class=\"form_note\" id=\"note_af3_data_types["+num+"]\"><a class=\"help\" onhover=\"\">?<span>Description of the technical type of the data asset (e.g. database, photo collection, text corpus, etc.)</span></a> <a href=\"javascript:;\" onclick=\"addType();\">Add a Type</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_data_types['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>Description of the technical type of the data asset (e.g. database, photo collection, text corpus, etc.)</span></a>";
	numi.value = num + 1;
}

function addDate() {
	var ni = document.getElementById('dates_block');
	var numi = document.getElementById('date_count');
	var num = new Number(document.getElementById("date_count").value);
	var divIdName = "af3_dates["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_data_date["+num+"]\">Creation Date "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_dates["+num+"][date]\" name=\"af3_dates["+num+"][date]\" size=\"20\" date=\"text\" value=\"YYYY-MM-DD\" onclick='scwShow(this,event);' /><input class=\"audit_input\" id=\"af3_dates["+num+"][comment]\" name=\"af3_dates["+num+"][comment]\" size=\"40\" value=\"Comment\" type=\"text\" /><div class=\"form_note\" id=\"note_af3_dates["+num+"]\"><a class=\"help\" onhover=\"\">?<span>The date(s) on which the data asset was created or published</span></a> <a href=\"javascript:;\" onclick=\"addDate();\">Add a Date</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_dates['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>The date(s) on which the data asset was created or published</span></a>";
	numi.value = num + 1;
}

function addOwner() {
	var ni = document.getElementById('owners_block');
	var numi = document.getElementById('owner_count');
	var num = new Number(document.getElementById("owner_count").value);
	var divIdName = "af3_data_owners["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_data_owners["+num+"]\">Owner "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_data_owners["+num+"]\" name=\"af3_data_owners["+num+"]\" size=\"30\" date=\"text\" /><div class=\"form_note\" id=\"note_af3_data_owners["+num+"]\"><a class=\"help\" onhover=\"\">?<span>Formal owner(s) of the data in terms of intellectual rights</span></a> <a href=\"javascript:;\" onclick=\"addOwner();\">Add an Owner</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_data_owners['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>Formal owner(s) of the data in terms of intellectual rights</span></a>";
	numi.value = num + 1;
}

function addVariantName() {
	var ni = document.getElementById('variant_names_block');
	var numi = document.getElementById('variant_name_count');
	var num = new Number(document.getElementById("variant_name_count").value);
	var divIdName = "af3_variant_names["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_variant_names["+num+"]\">Variant name "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_variant_names["+num+"]\" name=\"af3_variant_names["+num+"]\" size=\"30\" date=\"text\" /><div class=\"form_note\" id=\"note_af3_variant_names["+num+"]\"><a class=\"help\" onhover=\"\">?<span>Alternative or commonly used name, if available</span></a> <a href=\"javascript:;\" onclick=\"addVariantName();\">Add a Variant Name</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_variant_names['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>Alternative or commonly used name, if available</span></a>";
	numi.value = num + 1;
}

function addExtendedManager() {
	var ni = document.getElementById('managers_block');
	var numi = document.getElementById('manager_count');
	var num = new Number(document.getElementById("manager_count").value);
	var divIdName = "af3_data_managers["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_data_managers["+num+"]\">Asset Manager "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_data_managers["+num+"]\" name=\"af3_data_managers["+num+"]\" size=\"30\" date=\"text\" /><div class=\"form_note\" id=\"note_af3_data_managers["+num+"]\"><a class=\"help\" onhover=\"\">?<span>Name and contact details of the person responsible for the management of the data asset</span></a> <a href=\"javascript:;\" onclick=\"addExtendedManager();\">Add a Manager</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_data_managers['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>Name and contact details of the person responsible for the management of the data asset</span></a>";
	numi.value = num + 1;
}

function addFormerManager() {
	var ni = document.getElementById('former_managers_block');
	var numi = document.getElementById('former_manager_count');
	var num = new Number(document.getElementById("former_manager_count").value);
	var divIdName = "af3_former_managers["+num+"]";
	var newdiv = document.createElement('div');
	newdiv.setAttribute("id",divIdName);
	var form_fields = "<div class=\"form_row\"><label for=\"af3_former_managers["+num+"]\">Former Asset Manager "+(num + 1)+"</label><input class=\"audit_input\" id=\"af3_former_managers["+num+"]\" name=\"af3_former_managers["+num+"]\" size=\"30\" date=\"text\" /><div class=\"form_note\" id=\"note_af3_former_managers["+num+"]\"><a class=\"help\" onhover=\"\">?<span>Curation history / chain of custody for the dataset</span></a> <a href=\"javascript:;\" onclick=\"addFormerManager();\">Add a Former Manager</a></div></div>";
	newdiv.innerHTML = form_fields;
	ni.appendChild(newdiv);
	var prevNote = document.getElementById('note_af3_former_managers['+(num - 1)+']');
	prevNote.innerHTML = "<a class=\"help\" onhover=\"\">?<span>Curation history / chain of custody for the dataset</span></a>";
	numi.value = num + 1;
}

function getOrgDetails() {
	//get org ID
	var org_id = document.getElementById("department").value;
	if (org_id == "_new" || org_id == "") return;
	if (org_id == "_other") {
		document.getElementById("organisation_id").value == "";
		document.getElementById("organisation_address").value = "";
		document.getElementById("organisation_strategies").value = "";
		document.getElementById("organisation_description").value = "";
		document.getElementById("organisation_responsibilities").value = "";
		document.getElementById("organisation_budget").value = "";
		document.getElementById("primary_contact_name").value = "";
		document.getElementById("primary_contact_email").value = "";
		document.getElementById("primary_contact_phone").value = "";
		document.getElementById("primary_contact_other").value = "";
		document.getElementById("primary_contact_role").value = "";
		document.form1.organisation_type.selectedIndex = 0;
		var sec_cons = document.getElementById("anotherContact");
		while (sec_cons.hasChildNodes()) {
    		sec_cons.removeChild(sec_cons.firstChild);
		}
	}
	else {
		var url = "../incs/getOrgDetails.php?org=" + org_id;
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = updatePage;
		xmlHttp.send(null);
	}
}

function updatePage() {
  if (xmlHttp.readyState == 4) {
    var response = xmlHttp.responseXML;
    document.getElementById("organisation_id").value == "";
    document.getElementById("organisation_address").value = "";
    document.getElementById("organisation_strategies").value = "";
    document.getElementById("organisation_description").value = "";
    document.getElementById("organisation_responsibilities").value = "";
    document.getElementById("organisation_budget").value = "";
    document.getElementById("primary_contact_name").value = "";
    document.getElementById("primary_contact_email").value = "";
    document.getElementById("primary_contact_phone").value = "";
    document.getElementById("primary_contact_other").value = "";
    document.getElementById("primary_contact_role").value = "";
    document.form1.organisation_type.selectedIndex = 0;
    var sec_cons = document.getElementById("anotherContact");
    while (sec_cons.hasChildNodes()) {
    	sec_cons.removeChild(sec_cons.firstChild);
	}
    if (response.getElementsByTagName("id")[0].firstChild.data != "_other") {
		if (response.getElementsByTagName("id")[0].firstChild) {
			document.getElementById("organisation_id").value = response.getElementsByTagName("id")[0].firstChild.data;
		}
		if (response.getElementsByTagName("address")[0].firstChild) {
			document.getElementById("organisation_address").value = response.getElementsByTagName("address")[0].firstChild.data;
		}
		if (response.getElementsByTagName("strats")[0].firstChild) {
			document.getElementById("organisation_strategies").value = response.getElementsByTagName("strats")[0].firstChild.data;
		}
		if (response.getElementsByTagName("desc")[0].firstChild) {
			document.getElementById("organisation_description").value = response.getElementsByTagName("desc")[0].firstChild.data;
		}
		if (response.getElementsByTagName("resps")[0].firstChild) {
			document.getElementById("organisation_responsibilities").value = response.getElementsByTagName("resps")[0].firstChild.data;
		}
		if (response.getElementsByTagName("budget")[0].firstChild) {
			document.getElementById("organisation_budget").value = response.getElementsByTagName("budget")[0].firstChild.data;
		}
		if (response.getElementsByTagName("type")[0].firstChild) {
			document.form1.organisation_type.selectedIndex = parseInt(response.getElementsByTagName("type")[0].firstChild.data) - 2;
		}
		if (response.getElementsByTagName("pc_name")[0].firstChild) {
			document.getElementById("primary_contact_name").value = response.getElementsByTagName("pc_name")[0].firstChild.data;
		}
		if (response.getElementsByTagName("pc_email")[0].firstChild) {
			document.getElementById("primary_contact_email").value = response.getElementsByTagName("pc_email")[0].firstChild.data;
		}
		if (response.getElementsByTagName("pc_phone")[0].firstChild) {
			document.getElementById("primary_contact_phone").value = response.getElementsByTagName("pc_phone")[0].firstChild.data;
		}
		if (response.getElementsByTagName("pc_other")[0].firstChild) {
			document.getElementById("primary_contact_other").value = response.getElementsByTagName("pc_other")[0].firstChild.data;
		}
		if (response.getElementsByTagName("pc_role")[0].firstChild) {
			document.getElementById("primary_contact_role").value = response.getElementsByTagName("pc_role")[0].firstChild.data;
		}
		if (response.getElementsByTagName("sec_con").length > 0) {
			var numi = document.getElementById("contactValue");
			var num = new Number(numi).value;
			for (i=0;i<response.getElementsByTagName("sec_con").length;i++) {
				var ni = document.getElementById('anotherContact');
				
				var nameDiv = document.createElement('div');
				nameDiv.setAttribute("class","form_row");
				if (response.getElementsByTagName("sc_name")[i].firstChild)
					nameDiv.innerHTML = "<label for=\"secondary_contact_name["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_name["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_name["+(i+num)+"]\" value=\""+response.getElementsByTagName("sc_name")[i].firstChild.data+"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_name["+(i+num)+"]\">&nbsp;</div>";
				else
					nameDiv.innerHTML = "<label for=\"secondary_contact_name["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_name["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_name["+(i+num)+"]\" value=\"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_name["+(i+num)+"]\">&nbsp;</div>";
				ni.appendChild(nameDiv);
				
				var emailDiv = document.createElement('div');
				emailDiv.setAttribute("class","form_row");
				if (response.getElementsByTagName("sc_email")[i].firstChild)
					emailDiv.innerHTML = "<label for=\"secondary_contact_email["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_email["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_email["+(i+num)+"]\" value=\""+response.getElementsByTagName("sc_email")[i].firstChild.data+"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_email["+(i+num)+"]\">&nbsp;</div>";
				else
					emailDiv.innerHTML = "<label for=\"secondary_contact_email["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_email["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_email["+(i+num)+"]\" value=\"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_email["+(i+num)+"]\">&nbsp;</div>";
				ni.appendChild(emailDiv);
				
				var phoneDiv = document.createElement('div');
				phoneDiv.setAttribute("class","form_row");
				if (response.getElementsByTagName("sc_phone")[i].firstChild)
					phoneDiv.innerHTML = "<label for=\"secondary_contact_phone["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_phone["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_phone["+(i+num)+"]\" value=\""+response.getElementsByTagName("sc_phone")[i].firstChild.data+"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_phone["+(i+num)+"]\">&nbsp;</div>";
				else
					phoneDiv.innerHTML = "<label for=\"secondary_contact_phone["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_phone["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_phone["+(i+num)+"]\" value=\"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_phone["+(i+num)+"]\">&nbsp;</div>";
				ni.appendChild(phoneDiv);
				
				var otherDiv = document.createElement('div');
				otherDiv.setAttribute("class","form_row");
				if (response.getElementsByTagName("sc_other")[i].firstChild)
					otherDiv.innerHTML = "<label for=\"secondary_contact_other["+(i+num)+"]\">* Contact Name</label><textarea id=\"secondary_contact_other["+(i+num)+"]\" class=\"audit_input\" rows=\"3\"cols=\"30\" name=\"secondary_contact_other["+(i+num)+"]\">"+response.getElementsByTagName("sc_other")[i].firstChild.data+"</textarea><div class=\"form_note\" id=\"note_secondary_contact_other["+(i+num)+"]\">&nbsp;</div>";
				else
					otherDiv.innerHTML = "<label for=\"secondary_contact_other["+(i+num)+"]\">* Contact Name</label><textarea id=\"secondary_contact_other["+(i+num)+"]\" class=\"audit_input\" rows=\"3\"cols=\"30\" name=\"secondary_contact_other["+(i+num)+"]\"></textarea><div class=\"form_note\" id=\"note_secondary_contact_other["+(i+num)+"]\">&nbsp;</div>";
				ni.appendChild(otherDiv);
				
				var roleDiv = document.createElement('div');
				roleDiv.setAttribute("class","form_row");
				if (response.getElementsByTagName("sc_role")[i].firstChild)
					roleDiv.innerHTML = "<label for=\"secondary_contact_role["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_role["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_role["+(i+num)+"]\" value=\""+response.getElementsByTagName("sc_role")[i].firstChild.data+"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_role["+(i+num)+"]\">&nbsp;</div>";
				else
					roleDiv.innerHTML = "<label for=\"secondary_contact_role["+(i+num)+"]\">* Contact Name</label><input id=\"secondary_contact_role["+(i+num)+"]\" class=\"audit_input\" size=\"30\" name=\"secondary_contact_role["+(i+num)+"]\" value=\"\" type=\"text\" /><div class=\"form_note\" id=\"note_secondary_contact_role["+(i+num)+"]\">&nbsp;</div>";
				ni.appendChild(roleDiv);
				
				var idInput = document.createElement('input');
				idInput.setAttribute("id","secondary_contact_id["+(i+num)+"]");
				idInput.setAttribute("name","secondary_contact_id["+(i+num)+"]");
				if (response.getElementsByTagName("sc_id")[i].firstChild)
					idInput.setAttribute("value",response.getElementsByTagName("sc_id")[i].firstChild.data);
				else
					idInput.setAttribute("value","");
				idInput.setAttribute("type","hidden");
				ni.appendChild(idInput);
				
				var br1 = document.createElement('br');
				ni.appendChild(br1);
				var br2 = document.createElement('br');
				ni.appendChild(br2);

				
				numi.value = num +1;
			}
		}
	}
  }
}

