var COOKIE_NAME='tinn_cust';

/*
if(w4ie()) {
        window.attachEvent("onload",ola);
} else {
        document.addEventListener("DOMContentLoaded",ola,false);
}
*/

var d;

var destinationPage;
var destNewWindow;
var formToSubmit;
function requestDataSubmitForm(form,top, height) {
	formToSubmit=form;
	top = top ? top : 310;
	height = height ? height : 255;
	if (request(top,height)) {
	} else {
		redirectBrowser();
	}
}

function requestData(redirect, newwindow, top, height) {
	destinationPage = redirect;
	destNewWindow = ! newwindow ? newwindow : true;
	top = top ? top : 310;
	height = height ? height : 255;
	if (request(top,height)) {
	} else {
		redirectBrowser();
	}
}


function request(top,height) {
	var c = getCookie(COOKIE_NAME);
	if (! c) {
		d = document.createElement("div");
		d.style.position="absolute";
//		d.style.top="410px";
		d.style.top=top+"px";
		d.style.left='240px';
//		d.style.width = "478px";
		d.style.width = "678px";
		d.style.height = height+"px";
		d.style.border = "1px solid #ccc";
		d.style.zIndex='1';
		d.style.backgroundColor="#fff";
		d.style.filter="alpha(opacity=96)";
		d.style.opacity="0.96";
		d.style.fontFamily="Arial, Helvetica, sans";
//		d.style.paddingLeft = "100px";
//		d.style.paddingRight = "100px";
		
		
		var spn = document.createElement("div");
		spn.style.marginLeft="100px";
		spn.style.marginRight="100px";
		spn.innerHTML = "<h3>Information requested</h3><p style='font-size: 12px' align='left'>Before accessing this portion of our website, we request that you provide some information about yourself and your company. Please note this information will not be disclosed to any party outside of Tinnerman, for any reason.</p>";
		
//		var f = document.createElement("form");
//		f.setAttribute("action","collect_data.php");
//		f.setAttribute("name","prospectform");
//		f.setAttribute("method","POST");

		var tablo = document.createElement("table");
		tablo.setAttribute("border","0");
		tablo.setAttribute("cellspacing","8");
		tablo.setAttribute("align","left");
		tablo.style.border="1px solid #999";
		tablo.setAttribute("bgcolor","#000");
		tablo.style.color="#000";
		tablo.style.backgroundColor="#DEDEDE";
		tablo.style.fontWeight="bold";
		tablo.style.fontSize="13px";
		tablo.style.marginRight="16px";
		tablo.style.marginLeft="100px";
		
		
		var tr1 = tablo.insertRow(0);
		var td11 = tr1.insertCell(0);
		td11.setAttribute("align","left");
		var td12 = tr1.insertCell(1);

		var tr2 = tablo.insertRow(1);
		var td21 = tr2.insertCell(0);
		td21.setAttribute("align","left");
		var td22 = tr2.insertCell(1);

		var tr3 = tablo.insertRow(2);
		var td31 = tr3.insertCell(0);
		td31.setAttribute("align","left");
		var td32 = tr3.insertCell(1);

		var tr4 = tablo.insertRow(3);
		var td41 = tr4.insertCell(0);
		var td42 = tr4.insertCell(1);
	
		var namel = document.createElement("span");
		namel.innerHTML = "Your Name: ";		
		var nm = document.createElement("input");
		nm.setAttribute("type","text");
		nm.setAttribute("name","name");
		nm.setAttribute("id","name");

		td11.appendChild(namel);
		td12.appendChild(nm);

		var companyl = document.createElement("span");
		companyl.innerHTML = "Company Name: ";		
		var compan = document.createElement("input");
		compan.setAttribute("type","text");
		compan.setAttribute("name","company");
		compan.setAttribute("id","company");

		td21.appendChild(companyl);
		td22.appendChild(compan);
		
		var emaill = document.createElement("span");
		emaill.innerHTML = "Email Address: ";		
		var eml = document.createElement("input");
		eml.setAttribute("type","text");
		eml.setAttribute("name","email");
		eml.setAttribute("id","email");
		
		td31.appendChild(emaill);
		td32.appendChild(eml);	
		
		var s = document.createElement("input");
		s.setAttribute("type","button");
		s.setAttribute("value","Submit");
		if (w4ie()) {
			s.attachEvent("onclick",loading,false);
		} else {
			s.setAttribute("onclick","javascript:loading()");
		}
		
		
		td41.innerHTML="&nbsp;";
		td42.appendChild(s);
			
		d.appendChild(spn);
		d.appendChild(tablo);
		
		document.body.appendChild(d);
		return true
	} else {
		return false;
	}
}

var xmlHttp;
function loading() {
	xmlHttp = xmlRequest();
	var nm = document.getElementById("name").value;
	var compan = document.getElementById("company").value;
	var eml = document.getElementById("email").value;
	var err = document.getElementById("error");
	if (err) d.removeChild(err);
	xmlHttp.onreadystatechange=checkResponse;
	if (xmlHttp!=null) {
		xmlHttp.open("GET","/en/scripts/collect_data.php?name="+nm+"&company="+compan+"&email="+eml,true);
		xmlHttp.send(null);
	}
}

function checkResponse() {
	if(xmlHttp.readyState==4) {
		var response = xmlHttp.responseText;
		if (response == 'ok') {			
			while (d.firstChild) d.removeChild(d.firstChild);
			d.style.textAlign="center";
			var spn = document.createElement("div");
			spn.style.paddingTop="20px";
			spn.innerHTML = "<h3>Thank you for your interest in Tinnerman.  Please click below to continue</h3><br><br>";
			spn.style.textAlign="center";
			var b = document.createElement("input");
			b.setAttribute("type","button");
			b.setAttribute("value","Continue");			
			spn.appendChild(b);
			d.appendChild(spn);
			if (w4ie()) {
				b.attachEvent("onclick",redirectBrowser,false);
			} else {
				b.setAttribute("onclick","redirectBrowser()");
			}
			setCookie(COOKIE_NAME, "1", 180);			
		} else {
			var err = document.createElement("div");
			err.setAttribute("id", "error");
			//err.innerHTML="<h4 style='color:#990000; font-size: 13px; font-weight: bold;'>Please enter valid information for name, company and email address to proceed</h4>";
			err.innerHTML="<h4 style='color:#990000; font-size: 13px; font-weight: bold;'>"+response+"</h4>";
			d.appendChild(err);
		}
	}
}

function redirectBrowser() {
	removeDiv();
	if (destNewWindow) {
		window.open(destinationPage);
	} else if (formToSubmit) {
		formToSubmit.submit();
	} else {
		document.location=destinationPage;
	}
}
function removeDiv() {
	if (d) 
		document.body.removeChild(d);
}

function w4ie() {
        version=0;
        if (navigator.appVersion.indexOf("MSIE")!=-1){
                temp=navigator.appVersion.split("MSIE");
                version=parseFloat(temp[1]);
        }
        return version;
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}

function setCookie(c_name,value,expiredays) {

  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+
  ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}


function xmlRequest() {
	var xmlHttp;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) {	  // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				//alert("Your browser does not support AJAX!");
			  return false;
		  	}
		}
	}
	return xmlHttp;
}