// declare some globals from php

function openEmailPanel(j) {
				// opens an email composition form in the hidden row beneath the clicked icon
				var r = false;
				if(document.getElementById("shareEmailComposition")) { // a form is already open
					var s = document.getElementById("shareEmailComposition");
					if(document.getElementById("formRowNumber" + j)) var r = true; // we clicked on the form that's already open
					Slide.closed(s);

					// wait until slider has closed before removing it
					function waitForClose() {
                         this._closer = setTimeout(function()
                              {
								var s = document.getElementById("shareEmailComposition");
								if(!Core.hasClass(s, 'noShow')) waitForClose();
								else {
									Core.addClass(document.getElementById('row' + j), 'noShow');
									s.parentNode.removeChild(s); // -> remove it																		
								}										
                         	}, 100);							
					}
					waitForClose(r);
					
				}	
				if(r) return;  // if we clicked on the form that's already open, just remove it and return
				var row = document.getElementById("row" + j);
				var job = "job" + j; 
				var jobNameTD = "jobName" + j;

				var formForEmail = document.getElementById(job);
				var jobName = document.getElementById(jobNameTD);

				var e = "<div class='shareEmailComposition' id='shareEmailComposition'>"
					+ "<p id='sharedJob'>" 
					+ jobName.innerHTML 
					+ "</p>"
					+ "<p><img src='images/thumbsUp.gif' alt='thumbs up icon'>&nbsp;&nbsp;Tell a Friend about this Job&hellip;&nbsp;email them here!</p>"
					+ "<form id='sendEmailForm' class='sendEmail' name='sendEmail' method = 'POST' action = '" 
					+ phpSelf 
					+ "'>"
					+ "<div id='formRowNumber" + j + "' class='noShow'></div>"
					+ "<p><input class = 'short' type='text' width='35' name='sender'>&nbsp; From <span class='small'>[Your own email address]</span></p>" 
					+ "<p><input class = 'short'  type='text' width='35' name='recipient'>&nbsp;  To <span class='small'>[Your friend's email address]</span></p>"
					+ "<div class='small red' >[Send to more than one person&hellip; add extra email addresses separated with a comma]</div>" 
					+ "<p><input type='text' width='35' name='subject' value = 'Check out this Job Opportunity' >&nbsp; Subject</p>"
					+ "<p><textarea id = 'emailMsg' rows='5' cols='50' name='msg'  onkeypress='countChars()' onblur='countChars()'>"
					+ customerName
					+ " - "
					+  trim(jobName.innerHTML.replace(/<[\/\!]*?[^<>]*?>/gi, "")) + "\n\n"
					+ "</textarea>&nbsp; Your Message <span class='small'><a href='javascript:void(0)' onclick = 'expandTextArea(); return false' title='Click to Expand Text Area'>[Expand Space]</a></span></p>" 
					+ "<div class='medium red' >[A Link to see this Job will automagically be inserted here in your email when it is sent]</div>" 
					+ "<p><textarea rows='1' cols='50' name='signature' onkeypress='countChars()' onblur='countChars()'>"
					+ "</textarea>&nbsp; Signature</p>"
					+ "<div class='small red' >[Maximum Length <div id='charCount' class= 'inLine'>?</div> / 300 characters with signature]</div>" 					
					+ "<br><button value = 'send' onClick = 'if(validate(this.form)) this.form.submit(); return false'>Send Email</button>"
					+ "&nbsp;&nbsp;<button value = 'cancel' onClick = 'openEmailPanel(\"" + j + "\"); return false;'>Cancel</button>"  
					+ "<span class='small ghost'>&nbsp;&nbsp;&raquo;&raquo;&nbsp;In using this service you are agreeing to our <a href='legal.php' target='_blank' >Terms and Conditions</a></span>"
					+ "</form>"
					+ "</div>"
				;
				formForEmail.innerHTML = e; // insert the form etc into the row TD
				var b = document.getElementById('shareEmailComposition');// set background
				if(Math.random() >= 0.5 ) Core.addClass(b, " shareEmailCompositionBackgroundImageMale");
				else Core.addClass(b, " shareEmailCompositionBackgroundImageFemale");
				Core.removeClass(row, 'noShow');
				Slide.open(document.getElementById('shareEmailComposition'));
				//setTimeout('countChars()', 3000);
			}
			
		function countChars() {
			//if(typeof(document.sendEmail.emailMsg == 'undefined')) alert(1);
			var msg = document.sendEmail.emailMsg.value
			var sig = document.sendEmail.signature.value;
			var l = msg + sig;
			var c = document .getElementById('charCount');
			c.innerHTML = l.length;
			if(l.length >= 300) document.sendEmail.emailMsg.value = document.sendEmail.emailMsg.value.substr(0, 300 - sig.length); // trim message 
		}
     function expandTextArea() {
     	var t = document.getElementById('emailMsg');
     	var r = Math.round(t.getAttribute("rows")) 
     	t.setAttribute("rows", Math.min((r+3), 50));
     
	}				
		
		function trim(s) {
			while (s.charCodeAt(0) <= '32') {
				s = s.substring(1, s.length);
			}
			while (s.charCodeAt(s.length-1) <= '32') {
				s = s.substring(0,s.length-1);
			}
			return s;
		}

		function validEmail(e) {
			if(e.match(/^[A-Za-z0-9_-]+([.][A-Za-z0-9_-]+){0,4}[@][A-Za-z0-9_-]+([.][A-Za-z0-9_-]+){1,3}$/)) return true; else return false;
		}
		
		function validate(form) {

			var submitform = true;
			var errortext = "Sorry!\n\n";

			if (form.sender.value == '') {
				submitform = false;
				errortext += "Please enter your email address [Sender].\n\n";
			} else if (!validEmail(form.sender.value)) {
				submitform = false;
				errortext += form.sender.value
				errortext += " is not a valid email address\n";
			}
			if(!validEmail(form.recipient.value)) {
		
			}
			// iterate list of send to emails if present
			if(form.recipient.value !="") {
				var to = trim(form.recipient.value); 
				var tos = to.split(",") 
				for(i=0; i < tos.length; i++) {
                    if (!validEmail(trim(tos[i]))) {
                         submitform = false;
                         errortext += "To Email Address: " +  tos[i]
                         errortext += " \n\n ...seems to be invalid.\n\nRemember to separate multiple addresses with a comma.\n\n";			
                    }
				}
			}
			else {
				submitform = false;
				errortext += "Please enter an email address to send the email to [Your friend's address].\n\n";				
			}
						
			if (submitform == false) alert(errortext);

			return submitform;

		}


		function expandConcertina(d) {
			if(!document.getElementById('concertina' + d)) return;
			var concertina = document.getElementById('concertina' + d);
			var action = document.getElementById('action' + d);
			var openingdate = document.getElementById('date' + d);
			var sharehead = document.getElementById('sharehead' + d);
			var leftHeaderText = document.getElementById('leftHeaderText' + d);
			var rightHeaderText = document.getElementById('rightHeaderText' + d);
			var downArrow = document.getElementById('downArrow' + d);
			var upArrow = document.getElementById('upArrow' + d);
			Core.removeClass(concertina, 'noShow');
			Core.removeClass(action, 'noShow');
			Core.removeClass(openingdate, 'noShow');
			Core.removeClass(sharehead, 'noShow');
			Core.removeClass(leftHeaderText, 'noShow');
			Core.removeClass(upArrow, 'noShow');
			Core.addClass(rightHeaderText, 'noShow');
			Core.addClass(downArrow, 'noShow');
		}
		
		function collapseConcertina(d) {
			if(!document.getElementById('concertina' + d)) return;;
			var concertina = document.getElementById('concertina' + d);
			var action = document.getElementById('action' + d);
			var openingdate = document.getElementById('date' + d);
			var sharehead = document.getElementById('sharehead' + d);
			var leftHeaderText = document.getElementById('leftHeaderText' + d);
			var rightHeaderText = document.getElementById('rightHeaderText' + d);
			var downArrow = document.getElementById('downArrow' + d);
			var upArrow = document.getElementById('upArrow' + d);
			Core.addClass(concertina, 'noShow');
			Core.addClass(action, 'noShow');
			Core.addClass(openingdate, 'noShow');
			Core.addClass(sharehead, 'noShow');
			Core.addClass(leftHeaderText, 'noShow');
			Core.addClass(upArrow, 'noShow');
			Core.removeClass(rightHeaderText, 'noShow');
			Core.removeClass(downArrow, 'noShow'); 
		}		

		function expandAll(s) {

			var panels = Core.getElementsByClassName('panel', 'tbody');
			var expand = document.getElementById('expandPanels');
			var collapse = document.getElementById('collapsePanels');
			var action = Core.getElementsByClassName('action', 'th');
			var openingdate = Core.getElementsByClassName('date', 'th');
			var sharehead = Core.getElementsByClassName('sharehead', 'th');
			var leftHeader = Core.getElementsByClassName('leftHeader', 'th');
			var rightHeader = Core.getElementsByClassName('rightHeader', 'th');
			var upArrow = Core.getElementsByClassName('upArrow', 'th');
			var downArrow = Core.getElementsByClassName('downArrow', 'th');
			
          if(s) { // expanding the panels - adding the headers
               
               Core.addClass(expand,'noShow');
				Core.removeClassGlobal(panels, 'noShow');
               Core.removeClass(collapse, 'noShow');
				Core.removeClassGlobal(action, 'noShow');
				Core.removeClassGlobal(openingdate, 'noShow');
				Core.removeClassGlobal(sharehead, 'noShow');
				Core.removeClassGlobal(leftHeader, 'noShow');
				Core.addClassGlobal(rightHeader, 'noShow');
				Core.addClassGlobal(downArrow, 'noShow');
				Core.removeClassGlobal(upArrow, 'noShow');
          }
          else {
               Core.addClassGlobal(panels, 'noShow');
               Core.removeClass(expand,'noShow');
               Core.addClass(collapse, 'noShow');
				Core.addClassGlobal(action, 'noShow');
				Core.addClassGlobal(openingdate, 'noShow');
				Core.addClassGlobal(sharehead, 'noShow');
				Core.addClassGlobal(leftHeader, 'noShow');
				Core.removeClassGlobal(rightHeader, 'noShow');
				Core.removeClassGlobal(downArrow, 'noShow');
				Core.addClassGlobal(upArrow, 'noShow');
          }
		}


if(document.getElementById('sourceOverlayMessage'))	 Slide.open(document.getElementById('sourceOverlayMessage'));
