// JavaScript Document
var browserName;
var browserVersion;
var tempX;
var tempY;
var IE;
var ctt_timeout;
var ctt_still_loading=true;

function writeImageToDiv(imageName, width, height, xPos, yPos, divName, linkNameVar){
	
	if(browserName=="Explorer"){
		if(	Number(browserVersion<7)){
			if(linkNameVar!="")
				document.getElementById(divName).innerHTML+='<a href="'+linkNameVar+'"><img src="http://www.elocallink.tv/sandbox2/testing/spacer.png" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imageName+'\', sizingMethod=\'scale\'); border-width:0px;margin:0px; position:absolute; left:'+xPos+'px; top:'+yPos+'px; width:'+width+'px; height:'+height+'px;" ></a>';
			else
				document.getElementById(divName).innerHTML+='<img src="http://www.elocallink.tv/sandbox2/testing/spacer.png" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+imageName+'\', sizingMethod=\'scale\'); border-width:0px;margin:0px; position:absolute; left:'+xPos+'px; top:'+yPos+'px; width:'+width+'px; height:'+height+'px;" >';
			return;
		}
	}
	
	if(linkNameVar!="")
		document.getElementById(divName).innerHTML+="<a href='"+linkNameVar+"'><img src='"+imageName+"' style='border-width:0px;margin:0px; position:absolute; left:"+xPos+"px; top:"+yPos+"px;'></a>";
	else
		document.getElementById(divName).innerHTML+="<img src='"+imageName+"' style='border-width:0px; position:absolute;margin:0px; left:"+xPos+"px; top:"+yPos+"px;'>";
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
		browserName=this.browser;
		browserVersion=this.version;
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
var ctt_prescriptload = window.onload;
window.onload = function() {
	if (typeof (ctt_prescriptload) == 'function') {
		ctt_prescriptload();
	}
	write_ctt_div();
};


BrowserDetect.init();

function write_ctt_div(){
	document.body.innerHTML+="<div id='clicktotextdiv' style='width:298px;z-index:101; position:absolute; left:0px; top:0px; display:none;height:236px'><iframe id='cttiframe' style='width:1px; height:1px; display:none'></iframe><iframe id='cttiframe2' style='width:1px; height:1px; display:none'></iframe><div id='cttimage' style='position:absolute'></div><div id='ctt_main_contents'><div style='position:absolute;width:298px;'><p style='margin-left:16px; line-height:12px; margin-bottom:6px;  margin-top:17px; color:#222222; font-family:Arial; font-size:12px; font-weight:bold'>ClickToText</p><div style='overflow:hidden; background-color:#AAAAAA; height:1px; width:280px; margin-left:9px; margin-top:0px; margin-bottom:10px'></div><div style='width:260px;margin-left:19px;height:70px; position:relative;'><textarea onclick='javascript:ctt_clear_text()' id='ctt_text' multiline='multiline' style='font-family:Arial; font-size:12px;width:257px; overflow:auto; position:absolute; height:65px;left:0px; top:0px; border-width:1px; border-color:#AAAAAA; border-style:solid; color:#AAAAAA' >Enter your text message here</textarea></div><p style='font-family:Arial; font-weight:bold; font-size:11px; font-color:#4b4b4b; margin-left:18px;margin-right:18px; margin-top:5px; margin-bottom:5px;'>Enter your phone number in the field below if you wish to receive a reply:</p><div style='width:260px;margin-left:19px;height:20px; margin-bottom:12px; position:relative;'><input type='text' id='ctt_phone' style='font-family:Arial; font-size:12px;width:257px; position:absolute; height:15px;left:0px; top:0px; border-width:1px; border-color:#AAAAAA; border-style:solid'/></div><div onclick='javascript:ctt_call_now()' style='width:110px; cursor:pointer; height:32px; position:relative; margin:0px auto 0px auto' ><div id='cttbutton' style='position:absolute; left:0px; top:0px'></div><div style='position:absolute; width:110px; text-align:center; top:9px; left:0px; font-size:12px; font-weight:bold; font-family:Arial; color:#2c7afa'><span style='font-size:12px; font-weight:bold; font-family:Arial; color:#2c7afa; text-decoration:none'>Send Now</span></div></div></div></div><div id='ctt_calling_div' style='display:none; position:absolute; width:248px;left:25px;top:80px; text-align:center;line-height:12px; color:#222222; font-family:Arial; font-size:12px; font-weight:bold'>Now Sending<br/><br/><span style='font-size:11px'>Thank you for your message. If you've requested a response, we'll get back to you as soon as possible.</span></div><a href='javascript:ctt_close()'><img style='position:absolute; left:260px; border-width:0px; top:10px' src='http://www.elocallink.tv/sandbox2/modules/click_to_text/ctt_close.jpg'/></a></div>"
	writeImageToDiv("http://www.elocallink.tv/sandbox2/modules/click_to_text/cttbackground.png", 298, 236, 0, 0, "cttimage", "");
	writeImageToDiv("http://www.elocallink.tv/sandbox2/modules/click_to_text/cttbutton.png", 110, 32, 0, 0, "cttbutton", "");
	IE = document.all?true:false;
	if (!IE) document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = getMouseXY;
	ctt_still_loading=false;
}

function ctt_call_now(){
	num_to_check=document.getElementById("ctt_phone").value;
	num_to_check=num_to_check.replace(/ /g, "");
	var phoneRE = /^((\d\d\d-)|(\(\d\d\d\))|(\d\d\d))\d\d\d-*\d\d\d\d$/; 
	if(!num_to_check.match(phoneRE) && num_to_check!=""){
		alert("Please enter phone number in the format (123) 456-7890 or leave field blank if you are not seeking a reply");
	}
	else{
		
		if(document.getElementById("ctt_text").value=="Enter your text message here" || document.getElementById("ctt_text").value==""){
			alert("Please enter a message");
			return;
		}
		
		document.getElementById("ctt_main_contents").style.display="none";	
		document.getElementById("ctt_calling_div").style.display="block";	
		
		clearTimeout(ctt_timeout);
		ctt_timeout=setTimeout(ctt_close, 6000);
		num_to_call=clicktotext;
		
		scriptstring='http://www.elocallink.tv/zyte/text_it_s.php?carrier='+carrier+'&destcall='+clicktotext+'&phonenumber='+num_to_check+'&textcontent='+document.getElementById("ctt_text").value;
		//alert(scriptstring);
		document.getElementById('cttiframe').src=scriptstring;
		
		if(num_to_check!="") num_to_check="("+num_to_check.substring(0,3)+") "+num_to_check.substring(3,6)+"-"+num_to_check.substring(6);
		
		document.getElementById('cttiframe2').src="http://www.smartsite.tv/tracking/universal_tracking.php?pid="+encoded_pid+"&insert_type=clicktotext&phonenumber="+num_to_check+'&textcontent='+document.getElementById("ctt_text").value;
		
	}

}

function getMouseXY(e){
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.documentElement.scrollLeft
		tempY = event.clientY + document.documentElement.scrollTop
	 } else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX
		tempY = e.pageY
	}  
	
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
}

function ctt_clear_text(){
	if(document.getElementById("ctt_text").value=="Enter your text message here"){
		document.getElementById("ctt_text").value="";
		document.getElementById("ctt_text").style.color="#000000";
	}
	document.getElementById("ctt_text").focus();
}

function click_to_text(num){
	if(ctt_still_loading==true) return;
	ctt_close();
	if(document.getElementById("ctt_text").value=="") {
		document.getElementById("ctt_text").value="Enter your text message here";
		document.getElementById("ctt_text").style.color="#AAAAAA";
		
	}
	
	ctt_screenWidth=document.documentElement.clientWidth;
	
	ctt_offsetX=20;
	if(tempX+ctt_offsetX+300>ctt_screenWidth){
			ctt_offsetX=-280;
	}
	
	document.getElementById("clicktotextdiv").style.left=(tempX+ctt_offsetX)+'px';
	document.getElementById("clicktotextdiv").style.top=(tempY+20)+'px';
	document.getElementById("clicktotextdiv").style.display="block";
	
}

function ctt_close(){
	clearTimeout(ctt_timeout);
	document.getElementById("clicktotextdiv").style.display="none";
	document.getElementById("ctt_main_contents").style.display="block";	
    document.getElementById("ctt_calling_div").style.display="none";	
}
