function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;

function checkElement(elm,checkElementValue){
	if(elm != null && elm != '' && elm != 'undefined') {
		if(checkElementValue == true) {
			if(checkElmValue(elm) == true){
				return true;	
			}
			return false;
		}
		return true;
	} else {
		return false;
	}	
}

function checkElmValue(elm){
	if(!elm) return;	
	if(elm.value != '' && elm.value != 'undefined' && elm.value != null){
		return true;
	}
	alert(elm.id + ' cannot be blank!');
	return false;
}

function toggle(link,parent,divToOpen,lastVisible,action) {
	if (action != null) { 
		if(action() == false) {
			return;
		}
	}
	var divToOpen = document.getElementById(divToOpen);
	var lastVisible = document.getElementById(lastVisible);
	var parent = document.getElementById(parent);
	var lastBlockId;

		for(var i =0; i<parent.childNodes.length; i++) {
			if(parent.childNodes[i].nodeName == 'DIV') {
				if(lastVisible == null) {
					if(parent.childNodes[i].style.display == 'block') {
						lastBlockId = parent.childNodes[i].id;
					}
				}
				parent.childNodes[i].style.display = 'none';
			}
		}
		divToOpen.style.display = 'block';
	
	if(link != null) {
		link.onclick = function(){toggle(link,parent.id,divToOpen.id,lastBlockId,action)}
	}
	return;
}

function checkForm(f) {
	if(f.elements['name'].value == null || f.elements['name'].value == '' || f.elements['name'].value == 'undefined'){
		alert('Pleae fill in your name!');
		return false;
	}
	if(f.elements['email'].value == null || f.elements['email'].value == '' || f.elements['email'].value == 'undefined') {
		alert('Please fill in your email address!');
		return false;
	}
	if(f.elements['message'].value == null || f.elements['message'].value == '' || f.elements['message'].value == 'undefined') {
		alert('Please type in your message box!');
		return false;
	}	
	if( checkEmail(f.elements['email']) != false) {
			if(jcap() == true) {
				f.submit();
				return true;
			}
		}
	
	return false;
}

function isEmailAddress (string) {
  var addressPattern =
    /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
  return addressPattern.test(string);
}

function checkEmail (field) {
  if (!isEmailAddress(field.value)) {
    alert('Please enter correct email address!');
    field.focus();
    field.select();
 	return (false);
  }
}

function setClassName(elm,className){	
	if(elm != undefined){
		elm.className = className;
	}
}

function displayGalleryImageInPopupDiv(imgId,imgSrc,width, height) {
	
	var html = '<img alt="" src="'+imgSrc+'" border="0" />';
	html += '<div style="padding:5px; background:#FFFFFF;font-weight:bold;text-align:right;">'+
	'<span style="cursor:pointer;background:red;color:white" onClick="closePopupDiv('+imgId+')">&nbsp;close&nbsp;</span></div>';
	var div = drawCenteredPopup(imgId,width,height+20,html);
	div.style.border = 2+'px solid #5288B5';
	div.style.background = 'white';
	div.style.padding = '0.9em';
	div.style.zIndex = 99999999999999999999;	
}
function closePopupDiv(id){
	var elm = document.getElementById(id);
	elm.style.display = 'none';
	elm.style.visibility = 'hidden';
}

function drawCenteredPopup(id,width,height,html) {  
	if (!document.getElementById(id)) {
    	var newNode = document.createElement("div");
    	newNode.setAttribute("id", id);
    	newNode.setAttribute("class", "Div");
    	newNode.setAttribute("style", "visibility: hidden");
    	newNode.setAttribute("style", "display: none");
    	document.body.appendChild(newNode);
	}
	
  	var popupDiv = document.getElementById(id);

	popupWidth = (width =='auto') ? popupDiv.offsetWidth  : width ;
	popupHeight = (height == 'auto') ? popupDiv.offsetHeight : height;
	var innerW = window.innerWidth;
	var innerH = window.innerHeight;
	
	if (document.all) {
		innerW = document.body.clientWidth;
		innerH = document.body.clientHeight;
	}

  	popupDiv.style.position = "absolute";
  	popupDiv.style.width = popupWidth;
  	popupDiv.style.height = popupHeight+'px';
  	var x = (innerW / 2) - (popupWidth / 2);
  	var y = (innerH / 2) - (popupHeight / 2); 
	  	           
  	popupDiv.style.left = x + "px";
  	popupDiv.style.top = y + "px";
  	popupDiv.style.visibility = (popupDiv.style.visibility == "visible" ? "hidden" : "visible"); //hid /vis
  	popupDiv.style.display = (popupDiv.style.display == "block" ? "none" : "block"); //none /block
  	//popupDiv.style.border = 1+'px solid black';
   	//popupDiv.style.opacity=  '.9'; //mozilla
	//popupDiv.style.filter = 'alpha(opacity=90)'; //IE
  	popupDiv.style.zIndex = 1000;
  	popupDiv.innerHTML = html;
	
 	return popupDiv;
}

function emptyTable(table,keepHeaderRow) {
	var firstRow = (keepHeaderRow == true) ? 1 : 0;
	if(table.rows.length > 0) {
		for(var j=table.rows.length; j != firstRow; j--) { //!= 1
		table.deleteRow(-1);
		}
	}	
	return table.rows.length;
}

function hide(elmId){
	var elm =document.getElementById(elmId);
	if(!elm) return;
	elm.style.display = 'none';
}
function show(elmId,displayType){
	var elm = document.getElementById(elmId);
	if(!elm) return;
	if(!displayType) displayType = 'block';
	elm.style.display = displayType;
}

function getUrl(url){
	if(url)window.location.href = url;
}
function expandLeftSubMenu(subId){
	var sub = document.getElementById(subId);
	if(sub){
		sub.style.display = 'block';
	}
}


function clearField(fld) {
	if(fld.value == 'Your email address') {
		fld.value="";
	}
	return true;
}

function doSignUp(f) {
	var email = f.elements['email'];
	if(email.value == "" || email.value== null || email.value == 'undefined') {
		return false;
	}	
	else if(checkEmail(email) == false) {
		return false;
	}else{
		f.submit();
		return true;
	}
}

function getArgs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring(0,pos);
		var value = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

function showThankYou() {
	var args = getArgs();
	var a = args.showThankYou;

	if(args.showThankYou == 'Y') {	
		if (args.type == "contact") {
			document.getElementById('showThankYou').innerHTML = "Thank you for your interest! <br />Your message has been successfully sent!";
		}
	}
	if(args.resp =="Y"){
		if(args.type == "confirmRequired") {
			document.getElementById('showThankYou2').innerHTML = "You have successfully subscribed to Mailing List. You will receive an email with confirmation link shortly.";
		}
		else if(args.type == "emailNotFound") {
			document.getElementById('showThankYou2').innerHTML = "Your email address is not found.";
		}
		else if(args.type == "confirmSuccess") {
			document.getElementById('showThankYou2').innerHTML = "Thank you! Your subscribtion to Mailing List has been confirmed!";
		}
		else if(args.type == "alreadyConfirmed") {
			document.getElementById('showThankYou2').innerHTML = "You have already confirmed your subscription.";
		}
		else if(args.type == "alreadySubscribed") {
			document.getElementById('showThankYou2').innerHTML = "You have already subscribed to our Mailing list.";
		} else {
			document.getElementById('showThankYou2').innerHTML = "";
		}
	}
}
