if(location.href.indexOf('10.0.1.100') != -1){
	var rootdomain="http://"+window.location.hostname+"/saladin-security.com/";
}else{
	var rootdomain="http://"+window.location.hostname+"/";
}

function $(id){return document.getElementById(id);}

function GetXmlHttpObject(url){
var xmlHttp=null;
	try{  // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){// Internet Explorer
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
  }
return xmlHttp;
}


function sendMail(){
var form = document.thisForm;
document.getElementById('nameWarning').style.display = 'none';
document.getElementById('emailWarning').style.display = 'none';
document.getElementById('phoneWarning').style.display = 'none';
document.getElementById('commentsWarning').style.display = 'none';
document.getElementById('emailPanelWarning').style.display = 'none';

if(form.txtName.value == ''){form.txtName.focus();document.getElementById('nameWarning').style.display = '';return false;}
if(form.txtEmail.value == ''){form.txtEmail.focus();document.getElementById('emailWarning').style.display = '';document.getElementById('emailWarning').innerHTML = ' - Please insert your email';return false;}
if(validate(form.txtEmail.value,'email') == false){document.getElementById('emailWarning').style.display = '';document.getElementById('emailWarning').innerHTML = ' - Please insert a valid email';return false;}
//if(form.txtPhone.value == ''){ document.getElementById('phoneWarning').style.display = '';return false;}
//if(form.txtComments.value == ''){	document.getElementById('commentsWarning').style.display = '';return false;}

var url=rootdomain+"RootIncludes/phpScript.php?req=contact-us.php";
for (i = 0; i < form.elements.length; i++) {
	var elName = form.elements[i].name;
	var elValue = cleanValue(form.elements[i].value);		
	url=url+"&"+elName+"="+elValue;
} //end for(

url=url+"&sid="+Math.random();

	if(!document.getElementById('loadingDiv')){loading('emailPanel');}
	alert('url: '+url);

	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){  alert ("Your browser does not support AJAX!");  return;  } 
	xmlHttp.onreadystatechange=function(){sendMailRsp()};
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function sendMailRsp(){
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
		if(document.getElementById('loadingDiv')){document.getElementById('loadingDiv').parentNode.removeChild(document.getElementById('loadingDiv'));}
		alert(xmlHttp.responseText);
		var theGoods = xmlHttp.responseText.split('|');
		if(theGoods[0] == 1){
			document.getElementById('emailPanel').innerHTML = '<div>Your email has been sent successfully. Thank you for your comments.  We will be in touch, shortly.</div>'
		}else{
			document.getElementById('emailPanelWarning').style.display = '';
			document.getElementById('emailPanelWarning').innerHTML = '<div>Unfortunately, your email was not sent. There appears to be a problem with this form.  Please contact us using the information above.</div>'			
		}

	}
}

function cleanValue(value){
	var cleanValue = value.replace(/&/g,'-,-');
	var cleanValue = cleanValue.replace(/#/g,'-,,-');
	var cleanValue = cleanValue.replace(/=/g,'-,,,-');
	var cleanValue = cleanValue.replace(/\+/g,'-,,,,-');
	return cleanValue;
}

function Trim(nStr){return nStr.replace(/(^\s*)|(\s*$)/g, "");}

function validate(aStr, type) {
/*------ Function to check inputs ------*/
	var expr = "";
	switch (type){
	case "date" : expr = /^[0-2]{0,1}([1-9]|30|31)\/0{0,1}([0-9]|11|12)\/[0-9]{4,4}$/; break;
	case "email" : expr = /^(.)+@{1,1}((.)+\.(.)+)+$/; break;
	case "integer" : expr = /^[0-9]*$/; break;
	case "decimal" : expr = /^[0-9]*\.{0,1}[0-9]*$/; break;
	case "username" : expr = /^([a-z]|[A-Z]|[0-9])*$/; break;
	case "password" : expr = /^([a-z]|[A-Z]|[0-9])*$/; break;
	case "string" : expr = /^([a-z]|[A-Z]|[ ]|[.])*$/; break;
	case "phonenumber" : expr = /^([0-9]|[ ]|[+]|[-])*$/; break;
	}
	if(expr != "")
		return expr.test(Trim(aStr));
	else
		return false;
}

function loading(area){
	if(document.getElementById('loadingDiv')){document.getElementById('loadingDiv').parentNode.removeChild(document.getElementById('loadingDiv'));return;}

	if(document.getElementById(area)){
		var iconArea = document.getElementById(area);
		div = document.createElement('div');
		div.style.position = 'fixed';
		div.id = 'loadingDiv';
		div.style.top = '50%';
		div.style.left = '50%';
		div.align = 'center';
	
		img = document.createElement('img');
		img.src = rootdomain + 'RootImages/onLoad.gif';
	
		div.appendChild(img);
		document.getElementById(area).appendChild(div);
	}
}


ddaccordion.init({
	headerclass: "expandable", //Shared CSS class name of headers group that are expandable
	contentclass: "categoryitems", //Shared CSS class name of contents group
	revealtype: "click", //Reveal content when user clicks or onmouseover the header? Valid value: "click", "clickgo", or "mouseover"
	mouseoverdelay: 200, //if revealtype="mouseover", set delay in milliseconds before header expands onMouseover
	collapseprev: true, //Collapse previous content (so only one open at any time)? true/false 
	defaultexpanded: [0], //index of content(s) open by default [index1, index2, etc]. [] denotes no content
	onemustopen: false, //Specify whether at least one header should be open always (so never all headers closed)
	animatedefault: false, //Should contents open by default be animated into view?
	persiststate: true, //persist state of opened contents within browser session?
	toggleclass: ["", "openheader"], //Two CSS classes to be applied to the header when it's collapsed and expanded, respectively ["class1", "class2"]
	togglehtml: ["prefix", "", ""], //Additional HTML added to the header when it's collapsed and expanded, respectively  ["position", "html1", "html2"] (see docs)
	animatespeed: "fast", //speed of animation: integer in milliseconds (ie: 200), or keywords "fast", "normal", or "slow"
	oninit:function(headers, expandedindices){ //custom code to run when headers have initalized
		//do nothing
	},
	onopenclose:function(header, index, state, isuseractivated){ //custom code to run whenever a header is opened or closed
		//do nothing
	}
})

/***********************************************
* Accordion Content script- (c) Dynamic Drive DHTML code library (www.dynamicdrive.com)
* Visit http://www.dynamicDrive.com for hundreds of DHTML scripts
* This notice must stay intact for legal use
***********************************************/

function fnEmploymentInquiry(){
var form = document.thisForm;
var vars = [];

	for(var i=0;i<form.elements.length;i++){	

		if(form.elements[i].type == 'checkbox' && !checked){
			var checked = 0;
			var employmentSought = [];
			for(var i2=form.employmentSought.length-1;i2>-1;i2--){if(form.employmentSought[i2].checked){employmentSought[employmentSought.length] = form.employmentSought[i2].value;checked = 1;}}
			if(checked != 1){$(form.elements[i].name+'Warning').innerHTML = "Please select at least one position";return;}
			continue;
		}else if(form.elements[i].type == 'file' && form.elements[i].value.indexOf('.doc') == '-1' && form.elements[i].value.indexOf('.docx') == '-1'){
			form.elements[i].style.border = '1px solid #ff0000';
			$(form.elements[i].id + 'Warning').innerHTML = 'This file extension is not valid. Please upload your CV as a Word document.';
			form.elements[i].focus();
			return false;
		}else if(form.elements[i].type == 'checkbox' && form.elements[i].checked != true){
			continue;
		}else if(form.elements[i].type == 'radio' && form.elements[i].checked != true){
			continue;
		}else if($(form.elements[i].id + 'Warning') && form.elements[i].value == ''){
			form.elements[i].style.border = '1px solid #ff0000';
			$(form.elements[i].id + 'Warning').innerHTML = ' - Required';
			form.elements[i].focus();
			return false;
		}else if(form.elements[i].id == 'email' && validate(Trim(form.elements[i].value), "email") == false){
			form.elements[i].style.border = '1px solid #ff0000';
			$(form.elements[i].id + 'Warning').innerHTML = ' - Email address not valid';
			form.elements[i].focus();			
			return false;
		}else if($(form.elements[i].id + 'Warning') && form.elements[i].value != ''){
			form.elements[i].style.border = '1px solid #b5b5b5';
			$(form.elements[i].id + 'Warning').innerHTML = '';
		}
		vars[vars.length] = form.elements[i].name + '=' + cleanValue(form.elements[i].value);
	}
	if(employmentSought){vars[vars.length] = 'employmentSought=' + employmentSought.join(',');}

	var url=rootdomain+"RootIncludes/phpScript.php?req=employment.php";
//	alert(url+'\n\n'+vars.join('\n'));return;
	var vars = vars.join('&');
	url=url+"&sid="+Math.random();
	
	if(!$('loadingDiv')){loading('responseDiv');}
	form.action = rootdomain+"employment.php";
	form.submit();
/*
	xmlHttp=GetXmlHttpObject(url);		
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", vars.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.onreadystatechange = function(){fnEmploymentInquiryRsp()};
	xmlHttp.send(vars);
*/
}

function fnEmploymentInquiryRsp(){
	if (xmlHttp.readyState == 4 && xmlHttp.status == 200){
		if($('loadingDiv')){$('loadingDiv').parentNode.removeChild($('loadingDiv'));}
//		alert(xmlHttp.responseText);
		var theGoods = xmlHttp.responseText.split('|');
		if(theGoods[0] == 1){
			$('responseDiv').innerHTML = '<p>Thank you for submitting your CV to Saladin Security. It will be reviewed and our human resources manager will contact you for an interview, if appropriate.</p>';
		}else{
			$('responseDivWarning').style.display = '';
			$('responseDivWarning').innerHTML = '<div>Unfortunately, your inquiry was not sent. There appears to be a problem with this form.  Please contact us at <a style=\'color:#ff0000\' href=\'mailto:salsec@saladin-security.com\'>salsec@saladin-security.com</a>.</div>'
		}

	}
}

