﻿var bcode =  '10126100071000010000'; 
var tcode =  'AKMLS';
 
if (document.documentMode==8){
	var ie8 = true;
	//User is in IE8, no compatability mode
}
var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
// Means user is in IE7, or IE7 compatability

var resetTimer = 0;

function phields(phieldName){
	var phield = document.getElementById(phieldName);
	phield.style.width = "30px";
	phield.style.backgroundColor = "#e7e8ea";
	phield.style.border = "1px solid #818286";
	phield.style.marginBottom = "2px";
	phield.style.padding = "0px";
}

function heads(headingName){
	var sahead = document.getElementById(headingName);
	sahead.style.display = "block";
	sahead.style.color = "#990000";
	sahead.style.width = "100%";
	sahead.style.paddingBottom = "0px";
	sahead.style.fontWeight = "bold";
	sahead.style.fontSize = "14px";
	sahead.style.paddingTop = "2px";
}

function instructs(instructName){
	var saInstructions = document.getElementById(instructName);
	saInstructions.style.display = "block";
	saInstructions.style.width = "100%";
	saInstructions.style.paddingBottom = "0px";
	saInstructions.style.paddingTop = "3px"
}

function styleDefaults(){
	clearTimeout(resetTimer);
	
	var sa = document.getElementById('smarterAgent');
	sa.style.fontFamily = "Arial, Helvetica, sans-serif";
	sa.style.width = "100%";
	sa.style.height = "100%";
	sa.style.textAlign = "center";
	sa.style.fontSize = "12px";
	sa.style.paddingTop = "0px";
	
	phields('sa_p1');
	phields('sa_p2');
	phields('sa_p3');
	heads('line2');
	heads('line3');
	instructs('line1');
	instructs('line4');

	var touch = document.getElementById('touch');
	touch.style.cursor = "pointer";
	touch.style.color = "#444444";
	touch.style.fontWeight = "bold";
	touch.style.textDecoration = "underline";
	touch.setAttribute('id','touch');
	//touch.setAttribute('href','#');
	touch.onclick = emailDownloader;
	
	var submitB = document.getElementById('sa_submit');
	submitB.style.backgroundImage = "url('http://smarteragent.com/assets/new_btn2.jpg')";
	submitB.style.height = "27px";
	submitB.style.width = "77px";
	submitB.style.backgroundColor = "#ffffff";
	submitB.style.fontSize = "12px";
	submitB.style.borderWidth = "0px";
	submitB.style.fontWeight = "bold";
	submitB.style.display = "block";
	submitB.style.margin = "0 auto";
	submitB.style.marginBottom = "3px";
	submitB.style.marginTop= "3px";
	submitB.onclick = callScript;

    var line4 = document.getElementById('line4');
	line4.style.paddingBottom = "5px";

	if(nocode == true){
		var line1 = document.getElementById('line1');
		line1.style.display = "none";
		var or = document.getElementById('or');
		or.style.display = "none";
		var line2 = document.getElementById('line2');
		line2.style.display = "none";
		var line3 = document.getElementById('line3');
		line3.style.position = "relative";
		line3.style.marginTop = "30px";
	}

	var saForm = document.getElementById('saForm');
	if(ie7 == true){
		saForm.style.display = "block";
		saForm.style.position = "relative";
		saForm.style.marginTop = "0px";
	}
	
	if(ie8 == true){
	
	}

}

function results(message, action){
	var smarterAgent = document.getElementById('smarterAgent');
	smarterAgent.innerHTML = '<br>';
	var results = document.createElement('span');
	var displayResultsName = 'results';
	results.setAttribute('id',results);
	results.setAttribute('name',results);
	smarterAgent.appendChild(results);
    results.innerHTML = message;
	if(action !== 'url'){
		resetTimer = setTimeout("downloader()", 10000);
	}
	if(action == 'numberFail'){
		
		var action = document.createElement('button');
		var actionName = 'action';
		action.setAttribute('id',actionName);
		action.setAttribute('value',' ');
		action.style.backgroundImage = "url('http://smarteragent.com/assets/reset.png')";
		action.style.height = "27px";
		action.style.width = "50px";
		action.style.backgroundColor = "#ffffff";
		action.style.fontSize = "12px";
		action.style.borderWidth = "0px";
		action.style.fontWeight = "bold";
		action.style.display = "block";
		action.style.margin = "0 auto";
		action.onclick = downloader;
		
		smarterAgent.appendChild(action);
		resetTimer = setTimeout("downloader()", 15000);
	}
	if(action == 'emailFail'){
		var action = document.createElement('span');
		var actionName = 'action';
		action.setAttribute('id',actionName);
		action.setAttribute('value',' ');
		action.style.backgroundImage = "url('http://smarteragent.com/assets/reset.png')";
		action.style.height = "27px";
		action.style.width = "50px";
		action.style.backgroundColor = "#ffffff";
		action.style.fontSize = "12px";
		action.style.borderWidth = "0px";
		action.style.fontWeight = "bold";
		action.style.display = "block";
		action.style.margin = "0 auto";
		action.onclick = downloader;
		smarterAgent.appendChild(action);
		resetTimer = setTimeout("downloader()", 15000);
	}
	
	
}

function emailStyles(){

	clearTimeout(resetTimer);
	
	var theDiv = document.getElementById('smarterAgent');
	
	var sahead = document.getElementById('sahead');
	sahead.style.display = "block";
	sahead.style.fontWeight = "bold";
	sahead.style.fontSize = "12px";
	sahead.style.paddingTop = "30px";
	
	var saInstructions = document.getElementById('saInstructions');
	saInstructions.style.display = "block";
	saInstructions.style.fontWeight = "bold";
	saInstructions.style.fontSize = "12px";
	
	var saInstructions2 = document.getElementById('saInstructions2');
	saInstructions2.style.display = "block";
	saInstructions2.style.fontWeight = "bold";
	saInstructions2.style.fontSize = "12px";
	
	var saform = document.createElement('form');
	var formName = 'saForm';
	saform.setAttribute('id',formName);
	saform.setAttribute('name',formName);
	theDiv.appendChild(saform);
	
	var brandcode = document.createElement('input');
	var brandcodeName = 'brandcode';
	brandcode.setAttribute('id',brandcodeName);
	brandcode.setAttribute('name',brandcodeName);
	brandcode.setAttribute('type','hidden');
	brandcode.setAttribute('value','10126100071000010000');
	saform.appendChild(brandcode);
	
	var email = document.createElement('input');
	var emailName = 'sa_emailAddress';
	email.setAttribute('id',emailName);
	email.setAttribute('name',emailName);
	email.setAttribute('type','text');
	email.setAttribute('class','phield');
	email.setAttribute('size','30');
	email.style.width = "150px";
	email.style.backgroundColor = "#e7e8ea";
	email.style.border = "1px solid #818286";
	email.style.marginBottom = "5px";
	
	saform.appendChild(email);
	
	var suButton = document.createElement('input');
	var suButtonName = 'sa_submit';
	suButton.setAttribute('id',suButtonName);
	suButton.setAttribute('name',suButtonName);
	suButton.setAttribute('type','button');
	suButton.setAttribute('value','Send');
	suButton.setAttribute('onclick',"callScript();");
	suButton.style.backgroundImage = "url('http://smarteragent.com/assets/new_btn2.jpg')";
	suButton.style.height = "27px";
	suButton.style.width = "77px";
	suButton.style.backgroundColor = "#ffffff";
	suButton.style.fontSize = "12px";
	suButton.style.borderWidth = "0px";
	suButton.style.fontWeight = "bold";
	suButton.style.display = "block";
	suButton.style.margin = "0 auto";  
	saform.appendChild(suButton);  
	
	var displayResults = document.createElement('span');
	var displayResultsName = 'results';
	displayResults.setAttribute('id',displayResultsName);
	displayResults.setAttribute('name',displayResultsName);
	saform.appendChild(displayResults);
  
    var prev = document.createElement('span');
	var prevName = 'prev';
	prev.setAttribute('id',prevName);
	prev.style.cursor = "pointer";
	prev.style.color = "#444444";
	prev.style.fontWeight = "bold";
	prev.style.textDecoration = "underline";
	prev.setAttribute('id','prev');
	prev.onclick = downloader;
	
	displayResults.appendChild(prev);
	prev.innerHTML = 'Previous';
  
    if(ie7 == true){

	}
	
	if(ie8 == true){
	
	}
  
}

function gglScript(pre){
	var gglScript = document.createElement('script');
	
	if(pre == "https://secure."){pre = "https://ssl.";}
	try{
		document.write(unescape("%3Cscript src='" + pre + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	}catch(err1){
		gglScript.src = pre + "google-analytics.com/ga.js";
		gglScript.id="ggl";
		gglScript.type='text/javascript';
		document.body.appendChild(gglScript);
		if(typeof(pageTracker) == 'undefined'){
			try {
			var pageTracker = _gat._getTracker("UA-8899851-1");
			pageTracker._trackPageview();
			} catch(err) {}
		}
	}
}

var nocode;
var saHead = '<span id="line1">Text to get the application</span><span id="line2">Text: <strong>'+tcode+'</strong> To: <strong>87778</strong></span> <span id="or"> or </span><span id="line3"> Send it to my phone </span><span id="line4"> My mobile phone number is:</span>';

function getInternetExplorerVersion()
{
	var rv = -1; 
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		var ua = navigator.userAgent;
		var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
		if (re.exec(ua) != null)
			rv = parseFloat( RegExp.$1 );
	}
	return rv;
}

function smarter()
{
	var ver = getInternetExplorerVersion();
	if ( ver > -1 )
		{
		if ( ver >= 8.0 ) {
			window.downloader(); 
		}else{
			window.downloader(); 
		}
	} else {
		window.downloader();  
	}
	if(typeof(pageTracker) == 'undefined'){
		gglScript(pre);
	}
}

function downloader(){

	document.getElementById('smarterAgent').innerHTML = saHead;
	
	var theDiv = document.getElementById('smarterAgent');
	var saform = document.createElement('form');
	var formName = 'saForm';
	saform.setAttribute('id',formName);
	saform.setAttribute('name',formName);
	theDiv.appendChild(saform);
	
	var brandcode = document.createElement('input');
	var brandcodeName = 'brandcode';
	brandcode.setAttribute('id',brandcodeName);
	brandcode.setAttribute('name',brandcodeName);
	brandcode.setAttribute('type','hidden');
	brandcode.setAttribute('value','10126100071000010000');
	saform.appendChild(brandcode);
	
	var p1 = document.createElement('input');
	var p1Name = 'sa_p1';
	p1.setAttribute('id',p1Name);
	p1.setAttribute('name',p1Name);
	p1.setAttribute('type','text');
	p1.setAttribute('class','phield');
	p1.setAttribute('size','4');
	p1.setAttribute('maxLength','3');
	p1.setAttribute('onKeyUp',"return autoTab(this, 3, event);");
	saform.appendChild(p1);
	
	var dash = document.createTextNode(" - ");
	saform.appendChild(dash); 
	
	var p2 = document.createElement('input');
	var p2Name = 'sa_p2';
	p2.setAttribute('id',p2Name);
	p2.setAttribute('name',p2Name);
	p2.setAttribute('type','text');
	p2.setAttribute('class','phield');
	p2.setAttribute('size','4');
	p2.setAttribute('maxLength','3');
	p2.setAttribute('onKeyUp',"return autoTab(this, 3, event);");
	saform.appendChild(p2);
	
	var dash = document.createTextNode(" - ");
	saform.appendChild(dash);
	
	var p3 = document.createElement('input');
	var p3Name = 'sa_p3';
	p3.setAttribute('id',p3Name);
	p3.setAttribute('name',p3Name);
	p3.setAttribute('type','text');
	p3.setAttribute('class','phield');
	p3.setAttribute('size','5');
	p3.setAttribute('maxLength','4');
	p3.setAttribute('onKeyUp',"return autoTab(this, 4, event);");
	saform.appendChild(p3);
	
	var space = document.createTextNode("  ");
	saform.appendChild(space);
	
	/*var breaker = document.createElement("br");
	saform.appendChild(breaker);*/
	
	var suButton = document.createElement('input');
	var suButtonName = 'sa_submit';
	suButton.setAttribute('id',suButtonName);
	suButton.setAttribute('name',suButtonName);
	suButton.setAttribute('type','button');
	suButton.setAttribute('value','Send');
	//suButton.setAttribute('onclick',"callScript();");
	saform.appendChild(suButton);
	
	/* var breaker = document.createElement("br");
	saform.appendChild(breaker);*/
	
	var displayResults = document.createElement('span');
	var displayResultsName = 'results';
	displayResults.setAttribute('id',displayResultsName);
	displayResults.setAttribute('name',displayResultsName);
	saform.appendChild(displayResults);
	
	var touchContent = document.createElement('a');
	var touchName = 'touch';
	touchContent.setAttribute('id',touchName);
	
	
	displayResults.appendChild(touchContent);
	var touchText = document.createTextNode("iPod Touch Users");
	touchContent.appendChild(touchText);
	
	styleDefaults();
  
}

function emailDownloader(){

	var touchHead = '<span id="sahead">iPod Touch Users </span><span id="saInstructions">Enter your email address below </span><span id="saInstructions2">to receive an email with download instructions.</span>';

	document.getElementById('smarterAgent').innerHTML = touchHead;
	emailStyles();
}

////

function brand(url){

	var pdata = '';
	var NewScript = document.createElement('script');
	
	NewScript.src= url;
	NewScript.id="dlproc";
	document.body.appendChild(NewScript);
}

function callScript(){

	if(document.getElementById("sa_p1")){	
		var pdata1 = document.getElementById("sa_p1").value;
		var pdata2 = document.getElementById("sa_p2").value;
		var pdata3 = document.getElementById("sa_p3").value;
		var pdata = pdata1+pdata2+pdata3;
	} else if (document.getElementById("sa_emailAddress").value){
		var email = encodeURIComponent(document.getElementById("sa_emailAddress").value);
	}
	
	var bcode = document.getElementById("brandcode").value;

	if(email !== undefined){
		var url = "http://www.smarteragent.com/app/sa_return.php?emailAddress="+email+"&brandCode="+bcode;
	} else  { 
		var url = "http://www.smarteragent.com/app/sa_return.php?phoneNumber="+pdata+"&brandCode="+bcode;
	}


	var procmes = '<img src="http://smarteragent.com/assets/load.gif"> Processing...';
	document.getElementById('smarterAgent').innerHTML = procmes;
	brand(url);
}

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
	
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}

	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;
				
		return found;
	}
	
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)index = i;
			else i++;
		return index;
	}
	return true;
}

