

/** 
	Used for the pop-up window version
	variables set in object js file
**/
function launch_call_form()
{
	var mywindow = window.open(divurl+"callform.html","ClicktoCall","resizable=no, scrollbars=no, toolbar=no, width="+div_width+", height="+div_height+", top="+div_x+", left="+div_y);
  	mywindow.moveTo(div_x,div_y);
}

function get_url_var(urlVarName) 
{	
	//divide the URL in half at the '?'
	var urlHalves = String(document.location).split('?');
	var urlVarValue = '';
	if(urlHalves[1])
	{
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		
		//loop over the list, and find the specified url variable		
		for(i=0; i<=(urlVars.length); i++)
		{
			if(urlVars[i])
			{
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');

				if (urlVarPair[0] && urlVarPair[0] == urlVarName) 
				{
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}	
		}
	
	}
	return urlVarValue;   
}

/*** FORM FUNCTIONS ***/

function call_me(thispath, phone, name)
{	
	//get store
	thePage=unescape(get_url_var("ref"));
	
	//var store_name = thePage.substr(7, thePage.indexOf('.'));	
	var store_name_arr = thePage.replace("http://", "").split('.'); 
	var store_name = store_name_arr[0];
	
	// initiate call	
	var url = 'https://inlogin.com/webmanager/incontrol/SpawnScript.aspx?script=PushtoTalk&bus_no=4579283&skill_no=36178&p1='+store_name+'&p2='+escape(phone)+'&p3='+escape(name)+'&p4=&Guid=97f8a35f-64ca-43fe-8cab-f75c5f1e6c14';	
	//alert(store_name);
	window.frames.incontact.location = url; 
	
	
	/// add image
	var obj = document.getElementById("callform_div");
	if(typeof obj != "undefined" && obj != null)
	{
		obj.innerHTML = "<center><table width='70%'><tr><td><img src='"+thispath+"images/callrep.jpg' style='border: 1px solid #CCCCCC;' /></td><td>An associate will be contacting you soon.</td></tr></table>";
	}
}



/*
	Displays errors if values are not present for name or phone.
	Phone will also kick an error if not in the correct format.
*/
function error_check(tmp_url)
{
	var thispath = (tmp_url!="undefined" && tmp_url!=null)?tmp_url:divurl;
	var message = "";

	// strip non-digits
	var phone = document.getElementById('phone').value;
	phone = phone.replace(/[^\d]+/g,'');

	document.getElementById('phone').value = phone;

	// leave the name alone
	var name = document.getElementById('name').value;

	if(phone == "")
	{
		message = "Please enter a phone number";
	}
	else if(!checkPhone(phone))
	{
		message = "Please enter a valid phone number i.e. 5551234567";
	}

	if(name == "")
	{
		message = "Please enter your name";
	}
	
	document.getElementById('error').innerHTML	= "<font color=red><b>"+message+"</b></font>";
	if(message == "")
	{
		call_me(thispath, phone, name);
	}

}
/*
	strips all characters that are NOT digits then matches accordingly.
	function supported format(s):
	0953394248
	*095-339-4248
	*339-4248
	*339-42-48
	*339 42 48
	*339 4248
	*3394248
	*(095) #phone#
	*(095)#phone#
	*+7 (095) #phone#
	*+7 (095)#phone#
	*+7(095) #phone#
	*+7(095)#phone#

	* - these are no longer supported (commented out) because in my tests the didn't work
	with the calling script.
*/
function checkPhone(str)
{
	/// replace any odd characters, globally
	var phone_regex = /^\d{10}$/;
	if (str.match(phone_regex))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function IsIE()
{
	if (navigator.appVersion.indexOf("MSIE")!=-1)
	{
		return true;
	}
}

/*
	Provides "fade" effect on the call and cancel button.
*/
function SwitchImage(obj_name, img_src, tmp_url)
{
	var thispath = (tmp_url!="undefined" && tmp_url!=null)?tmp_url:divurl;
	var obj= document.getElementById(obj_name);
	if(typeof obj != "undefined" && obj != null)
	{
		obj.src = thispath+"images/"+img_src;
	}
}
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}
/*** END FORM FUNCTIONS ***/

