/*
	domEl() function - painless DOM manipulation
	written by Pawel Knapik  //  pawel.saikko.com
	example: domEl('a','go home',[['href','index.html']],document.getElementById('content'));
	usage: domEl( e , c , a , p , x );
	params: 
			e: type of element you want to create.
			If you want to create a <div>, you need to pass a string ‘div’ as the first argument, but it 
			doesn’t have to be a string: you can use document.getElementById(’someid’) and you will get 
			empty copy of referenced node (with all attributes, but without child elements).

			c: content of the new element.
			Now again you can use string, node reference or… an array! So if you want to just write ‘hello 
			world’ you pass it as a string, but if you want to go crazy and collect all links from the
			document in one place your second argument will be simply 
			document.getElementsByTagName(’a').

			a: attributes of the new element.
			An array of arrays, for example [['class','myclass'] , ['id','newId'] , ['foo','bar']]

			p: node(s) where new element will be placed.
			You can pass a node reference (document.body, document.getElementById etc)
			or an array of nodes (for example document.getElementsByTagName) - the new node
			will be added to all of these elements.

			x: remove current content before adding new element?
			if x=1, the new element will replace current content of ‘p’ element(s),
			if x=0 or is not defined, the new node will be added to ‘p’ child nodes.
*/

var domEl = function(e,c,a,p,x) {
if(e||c) {
	c=(typeof c=='string'||(typeof c=='object'&&!c.length))?[c]:c;	
	e=(!e&&c.length==1)?document.createTextNode(c[0]):e;	
	var n = (typeof e=='string')?document.createElement(e) : !(e&&e===c[0])?e.cloneNode(false):e.cloneNode(true);	
	if(e.nodeType!=3) {
		c[0]===e?c[0]='':'';
		for(var i=0,j=c.length;i<j;i++) typeof c[i]=='string'?n.appendChild(document.createTextNode(c[i])):n.appendChild(c[i].cloneNode(true));
		if(a) {for(var i=(a.length-1);i>=0;i--) a[i][0]=='class'?n.className=a[i][1]:n.setAttribute(a[i][0],a[i][1]);}
	}
}
	if(!p)return n;
	p=(typeof p=='object'&&!p.length)?[p]:p;
	for(var i=(p.length-1);i>=0;i--) {
		if(x){while(p[i].firstChild)p[i].removeChild(p[i].firstChild);
			if(!e&&!c&&p[i].parentNode)p[i].parentNode.removeChild(p[i]);}
		if(n) p[i].appendChild(n.cloneNode(true));
	}	
}

// used to show sub menus on drop down menus
function showSubMenu() {
	var hover = arguments[0];
	hover.className = 'hover';
	var element = document.getElementById(arguments[1]);
	element.style.display = 'block';
}

// used to hide sub menus on drop down menus
function hideSubMenu() {
	//alert(arguments.length);
	var hover = arguments[0];
	hover.className = '';
	var element = document.getElementById(arguments[1]);
	element.style.display = 'none';
}

// hides emails from spam bots
function safemail(name, domain, display) {
	var displayed;
	displayed=(typeof(display)=="undefined") ? name+"@"+domain : display
	document.write('<a href=mailto:' + name + '@' + domain + '>' + displayed + '</a>');
}

// used to open a window to mapquest and get directions
function getDirectionsFromMap() {
  var strLocOrigin = document.ddFromMap.mapLocationOrigin.value;
  var strLocDest = document.ddFromMap.mapLocationDest.value;
  var strUrl = "http://www.mapquest.com/directions/main.adp?go=1&cat=&"


    var strAddr  = document.getElementById("a1").value;
    var strCity  = document.getElementById("c1").value;
    var strState = document.getElementById("s1").value;
    var strZip   = document.getElementById("z1").value;
    var strFFI   = document.getElementById("ffi1").value;
    var strLat   = document.getElementById("l1").value;
    var strLng   = document.getElementById("g1").value;
    var strLvl   = document.getElementById("v1").value;

    strUrl = strUrl + "&1a=" + strAddr + "&1c=" + strCity + "&1s=" + strState + "&1z=" + strZip + "&1y=" + "&1ffi=" + strFFI + "&1l=" + strLat + "&1g=" + strLng + "&1v=" + strLvl + "&" + strLocDest;
  window.open(strUrl);
  //return false;
}

// used to validate some common elements
function validate() {
	var form = document.forms[0];
	var phoneRegExp = /(\s)*(\(\d{3}\))?-?\d{3}-\d{4}/;
	var emailRegExp = /^[^\s()<>@,;:\"\/\[\]?=]+@\w[\w-]*(\.\w[\w-]*)*\.[a-z]{2,}$/;
	for(var i = 0; i < form.length; i++) {
		/*if(form[i].name == 'Company_Name' && form[i].value == '') {
			alert('Please enter a Company Name to proceed');
			return false;
		}*/
		if(form[i].name == 'Name' && form[i].value == '') {
			alert('Please enter a Contact Name to proceed');
			return false;
		}
		if(form[i].name == 'Phone' && (form[i].value == '' || !phoneRegExp.test(form[i].value))) {
			alert('Please enter a valid Phone Number to proceed');
			return false;
		}
		if(form[i].name == 'Email' && (form[i].value == '' || !emailRegExp.test(form[i].value))) {
			alert('Please enter a valid E-mail address to proceed');
			return false;
		}
	}
	return true;
}

// used to add a form element to a page dynamically
// may need some site form specific changes to implement
function addEntry(strTableId,  arrayNames, strClass) {
	//alert(strTableId);
	var myTable;
	var myTableBody;
	var myTableRows;
	var myTableRow;
	var myTableCell;
	var myInputBox;
	var mySelectBox;
	var myNBS;
	
	myTable = document.getElementById(strTableId);
	myTableRows = myTable.getElementsByTagName('tr');
	myTableBody = document.createElement('tbody');
	myTableRow = document.createElement('tr');

	for(var i = 0; i < arrayNames.length; i++) {
		myTableCell = document.createElement('td');
		myTableCell.className = "center";
		if(arrayNames[i] != "Width") {
			myInputBox = document.createElement('input');
			myInputBox.className = strClass;
			myInputBox.name = arrayNames[i] + "_" + myTableRows.length;
			myInputBox.id = arrayNames[i] + "_" + myTableRows.length;
			myTableCell.appendChild(myInputBox);
		} else if(arrayNames[i] == "Width") {
			mySelectBox = document.createElement('select');
			mySelectBox.name = arrayNames[i] + "_" + myTableRows.length;
			mySelectBox.id = arrayNames[i] + "_" + myTableRows.length;
			var myOption;
			var arrayOptions = new Array(5);
			arrayOptions[0] = "Select Width";
			arrayOptions[1] = "834'";
			arrayOptions[2] = "1250'";
			arrayOptions[3] = "1668'";
			arrayOptions[4] = "2500'";			
			for(var j = 0; j < arrayOptions.length; j++) {
				mySelectBox.options[j] = new Option(arrayOptions[j], arrayOptions[j]);
			}
			myTableCell.appendChild(mySelectBox);
		}else {
			myNBS = document.createTextNode("\u00A0");
			myTableCell.appendChild(myNBS);
		}
		myTableRow.appendChild(myTableCell);
	}
	
	myTableBody.appendChild(myTableRow);
	myTable.appendChild(myTableBody);
}

// used to send form info to google maps and opens a new window showing the directions 
function getDirections() {
	var url = 'http://maps.google.com/maps';
	var zip = document.directions.saddr.value;
	var destination = '57 Ash Circle, 18974';
	var lang = 'en';
	url += '?saddr=' + zip + '&daddr=' + destination + '&hl=' + lang;
    	
	var params = "width=800,height=600,toolbar=1,menubar=1,status=1,location=1,directories=0,resizable=1,scrollbars=1";
	var newWin = window.open(url, null, params);
	newWin.document.onload = newWin.focus();    	
	return false;
 }
 
 // used to draw a google map. you need coordinates and API key per site. add 
 // '<div id="map" style="width: 400px; height: 300px; border:1px solid #000;">' 
 // on the page where you want the map to display. then add an onload event to the 
 // window
 function drawMap() {
	var map = new GMap(document.getElementById("map"));
	var point = new GPoint(-75.091593, 40.189002);
	var marker = new GMarker(point);
	
	var infoWindow = '<div style="width:200px;"><p><ul style="list-style:none; font-size: 11px; font-family: arial, sans-serif; margin:0; padding:0;">';
	infoWindow += '<li>Polykote Corp.<\/li>';
	infoWindow += '<li>57 Ash Circle<\/li>';
	infoWindow += '<li>Warminster, PA 18974<\/li>';
	infoWindow += '<li>800-298-KOTE (5683)<\/li>';
	infoWindow += '<\/ul><\/p>';
	infoWindow += '<form name="directions" onsubmit="return getDirections();">';
	infoWindow += '<p style="font-size: 11px; font-family: arial, sans-serif"><label for="saddr">Enter Your ZIP code for directions<\/label>';
	infoWindow += '<input type="text" name="saddr" id="saddr" value="" size="15" />';
	infoWindow += '<input type="submit" value="Go" />';
	infoWindow += '<\/p><\/form><\/div>';

    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.centerAndZoom(point, 8);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(infoWindow);
}

// used to open a seperate page and print info to it. will need customize per site.
function printReceipt() {	
	var top = '<html><head><title>AccelRx</title></head><body>';
	var receipt = '<table><tr><th colspan="2">AccelRx Clinical Trials</th></tr>';
	var bottom = '</body></html>';
	for(var i = 0; i < document.contactform.length; i++) {
		if(document.contactform[i].name != 'Submit' && document.contactform[i].name != 'Print') {
			receipt += '<tr><td>';
			receipt += document.contactform[i].name.replace('_', ' ', 'g');
			receipt += ': </td><td>';
			receipt += document.contactform[i].value;
			receipt += '</td></tr>';
		}
	}
	receipt += '</table>';
	var wind = window.open('',  'Receipt', 'width=360,height=330');
	var doc = wind.document.open();
	doc.write(top + receipt + bottom);
	wind.print();
	doc.close();
}
