// JavaScript Document
//
// IN-PAGE DROP-DOWN NIB CODE FUNCTIONALITY
//
function makeNibPanel(calltype,name,snapURL) {
	if (calltype=="faculty" || calltype=="noheader" || calltype== "noprofile") {
		if (fetcharg('nib')==""){
			if (calltype!=="noheader" && calltype!== "noprofile"){
				//just put up the banner
				document.write('<h2><a href="',snapURL,'"><img src="http://www.phil.ufl.edu/std-images/snap4-20.jpg" width="18" height="18" border="0" /></a> &nbsp;',name,'</h2>')
			}
		} else {
			var returnURL = location.href.split("?")[0]
			//alert("ready to noprofile")
			if (calltype == "noprofile") {
				//alert("noprofile")
				document.write('<div style="height: 178px"><br>')
			} else {
				// document.write('<h3>To Send Email to ',denotatum(),'</h3>')
				document.write('<div style="height: 178px"><h3><a href="',snapURL,'"><img src="http://www.phil.ufl.edu/std-images/snap4-20.jpg" width="18" height="18" border="0" /></a>&nbsp;Email address for ',denotatum(),':</h3><br>')
			}
			// using a link with javascript-mailto via the invoke() call
			document.write('<div id="dashedlinks" align="center"><a style="cursor:pointer" onClick="invoke()">')
			document.write('<font color="maroon">',paircode(fetcharg('nib'),fetcharg('dom')),'</font>','</a></div>')
			if (fetcharg('dom')=='ufl.edu') {
				document.write('<br><p>Click the above address to compose a message. ')
				document.write('Find more UF people in the <a href="http://phonebook.ufl.edu/">UF directory</a>.')
			} else {
				document.write('<br><p>Click the above address to compose a message')
				document.write(' in your email program. ')
			}
			document.write('</p><br><p align="right"><a href="',returnURL,'" style="background:black;color:white;"><em>&nbsp;CLOSE&nbsp;</em></a></p></div><hr>')
		}
	} else {
	//no other types defined just now
	}
}

//pull the designated arg value from the url by param TAG ('?tag=val&tag=val&etc')
function fetcharg(tag) {
	// s will pull anything starting with first occurance of '?' in current URL
	var s = self.location.search
	//set blanks
	var tink = ''
	var phrase = ''
	var param = ''
	var arg = ''
	
	// nip off the '?'
	if (s.length > 1) {s = s.substring(1,s.length)}
	
	if (s.length > 0) {
		// split arguments into an array
		var phrases = s.split("&")
		//grab tag-specified param
		for (i=0;i<phrases.length;i++) {
			var phrase = phrases[i]
			var pharray = phrase.split("=")
			param = pharray[0]
			arg = pharray[1]
			if (param==tag) {
				break
			} else {
				param = ''
			}
		}
	}
	
	//parse that param
	if (param==""){
		tink = ''
		if (tag=='dom'){tink = finisher()}
	}else{
		if (tag=="url") {
			// In this case we do not want to unescape the param
			tink = arg;
		} else {
			tink = unescape(arg);
		}
	}
	return tink
}

function denotatum() {
	if (fetcharg('name')==''){
		return '&lt;' + fetcharg('nib') +'&gt;';
	} else {
		return fetcharg('name');
	}
}

function finisher() {
	return 'ufl.edu'
}

function invoke() {
	if (fetcharg('name')=='') {
		var dest = 'mail'+'to:' + fetcharg('nib') + '@'+ fetcharg('dom');
	} else {
		var dest = 'mail'+'to:'+'"'+fetcharg('name')+'" <'+fetcharg('nib')+'@'+fetcharg('dom')+'>';
	}
	if (fetcharg('subj')!==''){
		var dest = dest + '?subject=' + fetcharg('subj')
	}
	location.href=dest;
}

function encode(e) {
	var coded = ""
	if (e == "") {}else{
        for (i = 0; i < e.length; i++) {
                var charNum = "000"
                var curChar = e.charAt(i)
				k = e.charCodeAt(i)
				
				if (k==0) {
					c = "000"
				} else if (k<100) {
					c = "&#0" + k + ";"
				} else {
					c = "&#" + k + ";"
				}

                coded += c
        }
	}
	return coded
}
function paircode(nb,dm) {
	return encode(nb) + '&#064' + encode(dm)
}

//END NIB CODE