// JavaScript Document
var tempX;
var tempY;
var cc_timeout;
var cc_still_loading=true;


var cc_prescriptload = window.onload;
window.onload = function() {
	if (typeof (cc_prescriptload) == 'function') {
		cc_prescriptload();
	}
	write_cc_div();
};


function write_cc_div(){
	document.body.innerHTML+="<div id='clicktocalldiv' style='width:298px;z-index:101; position:absolute; left:0px; top:0px; display:none;height:164px'><iframe id='cciframe' style='width:1px; height:1px; display:none'></iframe><iframe id='cciframe2' style='width:1px; height:1px; display:none'></iframe><div id='ccimage' style='position:absolute'></div><div id='cc_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'>ClickConnect&trade;</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:20px; position:relative;'><input type='text' id='cc_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><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:12px;'>Enter your phone number in the field above to be immediately connected</p><div onclick='javascript:cc_call_now()' style='cursor:pointer;width:110px; height:32px; position:relative; margin:0px auto 0px auto' ><div id='ccbutton' style='position:absolute; left:0px; top:0px'></div><div style='position:absolute; cursor:pointer; 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'>Call Now</span></div></div></div></div><div id='cc_calling_div' style='display:none; position:absolute; width:248px;left:25px;top:50px; text-align:center;line-height:12px; color:#222222; font-family:Arial; font-size:12px; font-weight:bold'>Now Calling<br/><br/><span style='font-size:11px'>Answer your phone when it rings and you will be automatically connected.</span></div><a href='javascript:cc_close()'><img style='position:absolute; left:260px; border-width:0px; top:10px' src='http://www.elocallink.tv/sandbox2/modules/click_to_call/cc_close.jpg'/></a></div>"
	writeImageToDiv("http://www.elocallink.tv/sandbox2/modules/click_to_call/ccbackground.png", 298, 164, 0, 0, "ccimage", "");
	writeImageToDiv("http://www.elocallink.tv/sandbox2/modules/click_to_call/ccbutton.png", 110, 32, 0, 0, "ccbutton", "");
	
	
	cc_still_loading=false;
}

function cc_call_now(){
	num_to_check=document.getElementById("cc_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)){
		alert("Please enter phone number in the format (123) 456-7890");
	}
	else{
		document.getElementById("cc_main_contents").style.display="none";	
		document.getElementById("cc_calling_div").style.display="block";	
		
		clearTimeout(cc_timeout);
		cc_timeout=setTimeout(cc_close, 6000);
		num_to_call=clicktocall;
		
		document.getElementById('cciframe').src='http://www.elocallink.tv/zyte/call_it_s.php?number='+num_to_check+'&destcall='+num_to_call;
		
		plain_num="";
		for(i=0;i<num_to_check.length;i++){
			if(num_to_check.charAt(i)!="(" && num_to_check.charAt(i)!=")" && num_to_check.charAt(i)!=" " && num_to_check.charAt(i)!="-")
				plain_num+=num_to_check.charAt(i);
		}
		num_to_check=plain_num;
		
		document.getElementById('cciframe2').src="http://www.smartsite.tv/tracking/universal_tracking.php?pid="+encoded_pid+"&insert_type=clicktocall&phonenumber="+"("+num_to_check.substring(0,3)+") "+num_to_check.substring(3,6)+"-"+num_to_check.substring(6);
	}
	

}

function click_to_call(num){
	if(cc_still_loading==true) return;
	cc_close();
	
	cc_screenWidth=document.documentElement.clientWidth;
	
	cc_offsetX=20;
	if(tempX+cc_offsetX+300>cc_screenWidth){
			cc_offsetX=-280;
	}
	
	document.getElementById("clicktocalldiv").style.left=(tempX+cc_offsetX)+'px';
	document.getElementById("clicktocalldiv").style.top=(tempY+20)+'px';
	document.getElementById("clicktocalldiv").style.display="block";
	
}

function click_to_call_with_offsets(num, xoff, yoff){
	if(cc_still_loading==true) return;
	cc_close();
	
	cc_screenWidth=document.documentElement.clientWidth;
	
	document.getElementById("clicktocalldiv").style.left=(tempX+xoff)+'px';
	document.getElementById("clicktocalldiv").style.top=(tempY+yoff)+'px';
	document.getElementById("clicktocalldiv").style.display="block";
	
}

function cc_close(){
	clearTimeout(cc_timeout);
	document.getElementById("clicktocalldiv").style.display="none";
	document.getElementById("cc_main_contents").style.display="block";	
    document.getElementById("cc_calling_div").style.display="none";	
}