//popup script
PositionX 		= 100;
PositionY 		= 100;
defaultWidth  	= 413;
defaultHeight 	= 413;
counterVar		= 1;
var AutoClose 	= true;
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=400,height=300,left='+PositionX+',top='+PositionY;

function popImage(imageURL,imageTitle){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Please wait while loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=100-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=BAC6CC scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=BAC6CC scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}

function appendContactInfoToList( location, table ) {
	
	
	var optionsElement 	= document.getElementById('ContactOptions');
	var input 			= document.getElementById('contactaddvalues');
	var id 	  			= optionsElement.value;
	var short 			= (optionsElement.options[optionsElement.selectedIndex].text).split(":")[0];
	var extra 			= (optionsElement.options[optionsElement.selectedIndex].text).split(":")[1];
	var phone 			= document.getElementById('Phone').value;


	var tblBody = table.tBodies[0];
	var newRow = tblBody.insertRow(-1);
	setCounterVar();
	newRow.setAttribute('id', 'dynamicRow:' + getCounterVar());
	
//	var cel0 = newRow.insertCell(0);
	var cel1 = newRow.insertCell(0);
	var cel2 = newRow.insertCell(1);
	var cel3 = newRow.insertCell(2);
	var cel4 = newRow.insertCell(3);
	
//	cel0.innerHTML=id.trim();
	cel1.innerHTML=short.trim();
	cel2.innerHTML=phone.trim();
	cel3.innerHTML=extra.trim();
	cel4.innerHTML='<a href="" onclick="return deleteCurrentRow(this);"><img alt="Delete phone number" class="icons" src="../../images/icons/delete.png"></a>';	

	//now add the contact info to the input for insertion
	var oldval	= input.getAttribute('value');
	input.setAttribute('value', getCounterVar() + ',' + id + ',' + phone + ':' + oldval );
	
	//to stop submit
	return false;
	

}


function CreateInputCellAndAppend( cel, value ) {
	
	var inp = document.createElement('input');
	cel.appendChild(inp);
	
	inp.setAttribute('type', 'text' );
	inp.setAttribute('width', '30px' );
	inp.setAttribute('value', value );
	inp.setAttribute('readonly', true );
	 
}


function deleteCurrentRow(obj)
{
	var dbDelValsInp 	= document.getElementById('contactdeletevalues');
	var dynAddValsInp 	= document.getElementById('contactaddvalues');

	//test to see if deleting the inserted row db
	if ( (obj.parentNode.parentNode.id).search(':') )
	{
		var id    	= (obj.parentNode.parentNode.id).split(":")[1];
		var type	= (obj.parentNode.parentNode.id).split(":")[0];
		var oldval	= dbDelValsInp.getAttribute('value');
	
		//if deleting real value then store id
		if (type == "dbRow") {
			dbDelValsInp.setAttribute('value', id + ':' + oldval );
		}

		if (type == "dynamicRow") {
			var temp = findAndRemoveSubstring(id, dynAddValsInp.getAttribute('value'));
			dynAddValsInp.setAttribute('value', temp );

		}
		
	}

	var delRow = obj.parentNode.parentNode;
	var tbl = delRow.parentNode.parentNode;
	var rIndex = delRow.sectionRowIndex;
	var rowArray = new Array(delRow);
	deleteRows(rowArray);

	return false;
}

function deleteRows(rowObjArray)
{
	for (var i=0; i<rowObjArray.length; i++) {
		var rIndex = rowObjArray[i].sectionRowIndex;
		rowObjArray[i].parentNode.deleteRow(rIndex);
	}
}

function findAndRemoveSubstring(id, string)
{
	//split the string into smaller chunks
	var insertArray = string.split(":");


	for (var i=0; i<insertArray.length; i++) {

		//now split again to find id and return it when stripped
		if ( insertArray[i].split(",")[0] == id ) {
			return string.replace(insertArray[i] + ':', '');
		}

	}

}

function getCounterVar(){
	return counterVar;	
}
function setCounterVar(){
	return counterVar++;	
}

function confirmation(message) {
	var answer = confirm(message)
	if (answer){
		
	}
	else{
		return false;
	}
}
