// JavaScript Document - smoothscroll function ********************

function ss_fixAllLinks() {
 var allLinks = document.getElementsByTagName('a');
 // Walk through the list
 for (var i=0;i<allLinks.length;i++) {
   var lnk = allLinks[i];
   if ((lnk.href && lnk.href.indexOf('#') != -1) &&  
       ( (lnk.pathname == location.pathname) ||
   ('/'+lnk.pathname == location.pathname) ) &&  
       (lnk.search == location.search)) {
     ss_addEvent(lnk,'click',smoothScroll);
   }
 }
}

function smoothScroll(e) {
 if (window.event) {
   target = window.event.srcElement;
 } else if (e) {
   target = e.target;
 } else return;
 
 if (target.nodeType == 3) {
   target = target.parentNode;
 }
 
 if (target.nodeName.toLowerCase() != 'a') return;
 
 anchor = target.hash.substr(1);
 var allLinks = document.getElementsByTagName('a');
 var destinationLink = null;
 for (var i=0;i<allLinks.length;i++) {
   var lnk = allLinks[i];
   if (lnk.name && (lnk.name == anchor)) {
     destinationLink = lnk;
     break;
   }
 }
 
 if (!destinationLink) return true;
 
 var destx = destinationLink.offsetLeft;  
 var desty = destinationLink.offsetTop;
 var thisNode = destinationLink;
 while (thisNode.offsetParent &&  
       (thisNode.offsetParent != document.body)) {
   thisNode = thisNode.offsetParent;
   destx += thisNode.offsetLeft;
   desty += thisNode.offsetTop;
 }
 
 clearInterval(ss_INTERVAL);
 
 cypos = ss_getCurrentYPos();
 
 ss_stepsize = parseInt((desty-cypos)/ss_STEPS);
 ss_INTERVAL = setInterval('ss_scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',10);
 
 if (window.event) {
   window.event.cancelBubble = true;
   window.event.returnValue = false;
 }
 if (e && e.preventDefault && e.stopPropagation) {
   e.preventDefault();
   e.stopPropagation();
 }
}

function ss_scrollWindow(scramount,dest,anchor) {
 wascypos = ss_getCurrentYPos();
 isAbove = (wascypos < dest);
 window.scrollTo(0,wascypos + scramount);
 iscypos = ss_getCurrentYPos();
 isAboveNow = (iscypos < dest);
 if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
   window.scrollTo(0,dest);
   clearInterval(ss_INTERVAL);
   location.hash = anchor;
 }
}

function ss_getCurrentYPos() {
 if (document.body && document.body.scrollTop)
   return document.body.scrollTop;
 if (document.documentElement && document.documentElement.scrollTop)
   return document.documentElement.scrollTop;
 if (window.pageYOffset)
   return window.pageYOffset;
 return 0;
}

function ss_addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+,  NS6 and Mozilla
// By Scott Andrew
{
 if (elm.addEventListener){
   elm.addEventListener(evType, fn, useCapture);
   return true;
 } else if (elm.attachEvent){
   var r = elm.attachEvent("on"+evType, fn);
   return r;
 }
}  

var ss_INTERVAL;
var ss_STEPS = 50;

ss_addEvent(window,"load",ss_fixAllLinks);
// JavaScript Smoothscroll Function End ****************************



// JavaScript Placeholder Image-Swap Function **********************
function showpic (whichpic) {
 if (document.getElementById) {
  document.getElementById('placeholder')
  .src = whichpic.href;
  if (whichpic.title) {
   document.getElementById('desc')
  .childNodes[0].nodeValue = whichpic.title;
  } else {
   document.getElementById('desc')
  .childNodes[0].nodeValue = whichpic.childNodes[0].nodeValue;
  }
  return false;
 } else {
  return true;
 }
}
// JavaScript Placeholder Image-Swap Function End ******************




// Contact Form Verification Function ******************************
function Verify()
{
  var themessage = "";
  
  if (isEmpty(document.getElementById("myMessage")))
  {
    themessage = "\n  message" + themessage ;
  }
  if (isEmpty(document.getElementById("mySubject")))
  {
    themessage = "\n  subject" + themessage ;
  }
  if (isEmpty(document.getElementById("myEmail")))
  {
    themessage = "\n  email" + themessage ;
  }
  else
  {
    var item = document.getElementById("myEmail").value;
    if (RightEmail(item))
    {
      themessage = "\n email in a correct format" + themessage ;
    }
  }
  if (isEmpty(document.getElementById("myName")))
  {
    themessage = "\n  name" + themessage ;
  }


//alert if fields are empty and cancel form submit
  if (themessage == "")
  {
    return true;
  }
  else
  {
    var themessage = "You are required to complete the following fields:\n" + themessage ;
    alert(themessage);
    return false;
  }
}


//Functionality: Check out introduced email address.
function RightEmail(email)
{
	if (email.indexOf(" ")!=-1) // Email address doesn't have spaces on right, left or between.
	{
	  return true;
	}
	if (email.indexOf("@")==-1) // Email address contains character @
	{
		return true;
	}
	else
	{
		var posat=email.indexOf("@");
		var name=email.substring(0, posat);
		var domain=email.substring(posat+1);
		// Name contains at least one character
		if (name.length==0)
			return true;
		// Name is not just a dot
		if (name==".")
			return true;
		// Domain contains at least a character
		if (domain.length==0)
			return true;
		// Domain contains a dot
		if (domain.indexOf(".")==-1)
			return true;
			// Domain's dot is not in the first or last position
			var len_dom=domain.length - 1;
		if ((domain.charAt(0)==".")||(domain.charAt(len_dom)=="."))
			return true;
			// Name and domain contains right characters
			if (RightCharIn(name)) return true;
			if (RightCharIn(domain)) return true;
	}
  return false;
}

//Functionality:String must contain valid characters and not have two consecutive dots.
function RightCharIn(string)
{
var rightChars =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
for (i=0;i<string.length;i++)
	{
	if (rightChars.indexOf(string.charAt(i))==-1) return true;
	if (string.charAt(i)==".")
	if ((i!=string.length-1) && (string.charAt(i+1)=="."))
	return true;
	}
	return false;
}

//Functionality:checks out if item is empty.
function isEmpty(objItem)
{
	var strValue = objItem.value.replace(/^\s*|\s*$/g,"");
	if(strValue=="")
	{
		objItem.focus();
  	var Empty = true;
	}
	else
	{
  	var Empty = false;
	}
	return Empty;
}
// Contact Form Verification Function End **************************