// Newfront JS
function getxmlhttp(){
var xmlhttp = false;
//Check if we are using IE.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using IE.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}

function dEI(obj){
	return document.getElementById(obj);	
}

function gotoPage(page){
	window.location.href = page;	
}

function getPos(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	var mycoors = new Array(posx, posy);
	return mycoors;
}



function showMouseCords(e){
	var coords = getPos(e);
	dEI("tooltip").style.top = (coords[1] - 155) + "px";
	dEI("tooltip").style.left = (coords[0] - 50) + "px";
}

function closeToolTip(){
if(dEI("tooltip").className == "show"){
		dEI("tooltip").className = "hide";
		dEI("tooltip").style.top = "0px";
		dEI("tooltip").style.left = "0px";
	}	
}

var Preloader = {
  callbacks: [],
  images: [],
  loadedImages: [],
  imagesLoaded: 0,

  add: function(image){
    if (typeof image == 'string') this.images.push(image);
    if (typeof image == 'array' || typeof image == 'object'){
      for (var i=0; i< image.length; i++){
        this.images.push(image[i]);
      }
    }
  },
  onFinish: function(func){
    if (typeof func == 'function') this.callbacks.push(func);
    if (typeof func == 'array' || typeof func == 'object'){
      for (var i=0; i< func.length; i++){
        this.callbacks.push(func[i]);
      }
    }
  },
  load: function(){ 
    for(var i=0; i<this.images.length; i++){
      this.loadedImages[i] = new Image();
      this.loadedImages[i].onload = function(){ Preloader.checkFinished.apply(Preloader) }
      this.loadedImages[i].src = this.images[i];
    }
  },

  checkFinished: function(){
    this.imagesLoaded++;
    if (this.imagesLoaded == this.images.length) this.fireFinish();
  },
  fireFinish: function(){
    for (var i=0; i<this.callbacks.length; i++){
      this.callbacks[i]();
    }
    this.images = [];
    this.loadedImages = [];
    this.imagesLoaded = 0;
    this.callbacks = [];
  }
}


function loadImage(param1, param2, param3, param4){
	Preloader.add(param1);
	dEI(param2).style.opacity = "0";
	dEI(param3).className = "show";
	finishFunc = function(){
		dEI(param3).className = "hide";
		dEI(param4).src = param1;
		Effect.Appear(param2);
		
	}
	Preloader.onFinish(finishFunc);
	Preloader.load();	
}

var curShowing = 0;

function loadHpModule(obj,page,id,img1){
	var xmlhttp = getxmlhttp();
	var serverPage = page+"?id="+id;
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			dEI(obj).innerHTML = xmlhttp.responseText;
			loadImage(img1,'mainview','loading','replacable');
			Effect.Appear(obj);
			if(dEI("viewsite")){
				dEI("viewsite").onmouseover = function(){
					if(this.className == "viewsite_btn_up"){
						this.className = "viewsite_btn_over";
					}
				}
				dEI("viewsite").onmouseout = function(){
					if(this.className == "viewsite_btn_over"){
						this.className = "viewsite_btn_up";
					}
				}
			}
			if(dEI("privatesite")){
				dEI("privatesite").onmouseover = function(){
					if(this.className == "private_btn_up"){
						this.className = "private_btn_over";
					}
					if(dEI("tooltip").className == "hide"){
		dEI("tooltip_title").innerHTML = "Private Website";
		dEI("tooltip_txt").innerHTML = "Private Website's are any websites that have not been officially released to the public yet. Or whose project data is classified or meant for internal company affairs.";
		dEI("tooltip").className = "show";
	}
				}
				dEI("privatesite").onmousemove = function(event){
					showMouseCords(event);		
				}
				dEI("privatesite").onmouseout = function(){
					if(this.className == "private_btn_over"){
						this.className = "private_btn_up";
					}
					closeToolTip();
				}
			}
			if(dEI("nextbtn")){
				dEI("nextbtn").onmouseover = function(){
					if(this.className == "nextbtn_up"){
						this.className = "nextbtn_over";	
					}
				}
				dEI("nextbtn").onmouseout = function(){
					if(this.className == "nextbtn_over"){
						this.className = "nextbtn_up";	
					}
				}
			}
			if(dEI("prevbtn")){
				dEI("prevbtn").onmouseover = function(){
					if(this.className == "prevbtn_up"){
						this.className = "prevbtn_over";	
					}
				}
				dEI("prevbtn").onmouseout = function(){
					if(this.className == "prevbtn_over"){
						this.className = "prevbtn_up";	
					}
				}
			}
			
		}
	}
	xmlhttp.send(null);
}

//
function goform(obj){
	var xmlhttp = getxmlhttp();
	var serverPage = "../../contact_form.html";
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			dEI(obj).innerHTML = xmlhttp.responseText;
			dEI(obj).style.display = "block";
			dEI("dim").style.display = "block";
			goForm();
			if(window.ActiveXObject){
				goDim();
			}
			dEI("contactformbtm").style.display = "block";
		}
	}
	xmlhttp.send(null);
	
}

function closewin(obj){
	dEI(obj).innerHTML = "";
	dEI(obj).style.display = "none";
	if(dEI("dim")){
		dEI("dim").style.display = "none";	
	}
	
}

function dEI(obj){
	return document.getElementById(obj);	
}

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function checkForm(obj){
	var check1 = dEI("name").value;
	var check2 = dEI("email").value;
	var check3 = dEI("phone").value;
	var check4 = dEI("message").value;
	var check5 = dEI("heard").value;
	var emailErrString = "";
	
	if(dEI("otherval")){var check6 = dEI("otherval").value;}
	var passed = true;
	if(check1 == ""){
		passed = false;
		dEI("nameerr").style.display = "block";
		dEI("nameerr").innerHTML = "Please add your Name";
	} else {
		dEI("nameerr").style.display = "none";
		dEI("nameerr").innerHTML = "";
	}
	if(check2 == ""){
		passed = false;
		dEI("emailerr").style.display = "block";
		emailErrString = emailErrString + "Please add your Email. "
		dEI("emailerr").innerHTML = emailErrString;
	}
	if(check2.indexOf('@') == -1 && check2 != "" && check2.indexOf('.') == -1){
		passed = false;
		dEI("emailerr").style.display = "block";
		emailErrString = emailErrString + "Email is not in correct format";
		dEI("emailerr").innerHTML = emailErrString;
	}
	
	if(check2.charAt(0) == "@"){
		passed = false;
		dEI("emailerr").style.display = "block";
		emailErrString = emailErrString + "Email is not in correct format"
		dEI("emailerr").innerHTML = emailErrString;
	}
	if(emailErrString == ""){
		dEI("emailerr").style.display = "none";
		dEI("emailerr").innerHTML = "";
	}
	if(check3 == ""){
		passed = false;
		dEI("phoneerr").style.display = "block";
		dEI("phoneerr").innerHTML = "Please add your Phone Number";
	} else {
		dEI("phoneerr").style.display = "none";
		dEI("phoneerr").innerHTML = "";
	}
	if(check4 == ""){
		passed = false;
		dEI("messageerr").style.display = "block";
		dEI("messageerr").innerHTML = "Please add a message so we can further assist you.";
	} else {
		dEI("messageerr").style.display = "none";
		dEI("messageerr").innerHTML = "";
	}
	if(dEI("otherval")){
		if(check6 == ""){
			dEI("othererr").style.display = "block";
			dEI("othererr").innerHTML = "Please Specify";
		} else {
			dEI("othererr").style.display = "none";
			dEI("othererr").innerHTML = "";
		}
	}
	if(passed == true){
		var xmlhttp = getxmlhttp(); // create new request objectyZ
		var url = "../../mail.php";
		check4 = escape(check4)
		var params = "name="+check1+"&email="+check2+"&message="+check4+"&phone="+check3+"&found="+check5;
		xmlhttp.open("POST", url, true);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", params.length);
		xmlhttp.setRequestHeader("Connection", "close");
		
		xmlhttp.onreadystatechange = function() {
			
			if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				document.getElementById("theform").innerHTML = "<span class='pass'><p>Mail Sent</p></span>";
				dEI("submitbtn").style.display = "none";
				sleep(1000, function(){Effect.Fade('popwin');});
				sleep(1000, function(){Effect.Fade('dim');});
			}
		}
		xmlhttp.send(params);
	}
}

function sleep(time, functionw){
	var cool = setTimeout(functionw, time);
}

function checkForOther(val){
	if(val == "other"){
		dEI("otheradd").style.display = "block";
		dEI("otheradd").innerHTML = '<span class="pass" id="otherid"><input type="text" id="otherval" class="sInput"/><div id="othererr" class="err"></div></span>';
	} else {
		dEI("otheradd").style.display = "none";
		dEI("otheradd").innerHTML = "";
	}
}

function goDim(){
	var pageSizeInfo = getPageSize();
	var pageWidth = pageSizeInfo[0];
	var pageHeight = pageSizeInfo[1];
	dEI("dim").style.width = pageWidth + "px";
	dEI("dim").style.height = pageHeight + "px";
	dEI("dim").style.top = "0px";
	dEI("dim").style.left = "0px";
	dEI("dim").style.position = "absolute";
}

function goForm(){
	var pageSizeInfo = getPageSize();
	var pageWidth = pageSizeInfo[0];
	var pageHeight = pageSizeInfo[1];
	var leftOffset = ((pageWidth / 2) - 280);
	dEI("popwin").style.left = leftOffset + "px";
	dEI("popwin").style.top = "10%";
}