﻿
function pageWidth()
{
	return window.innerWidth != null? 
		window.innerWidth: 
		document.documentElement && document.documentElement.clientWidth ? 
			document.documentElement.clientWidth:document.body != null? 
				document.body.clientWidth:
				null;
}
function pageHeight()
{
	return window.innerHeight != null? 
		window.innerHeight: 
		document.documentElement && document.documentElement.clientHeight ? 
			document.documentElement.clientHeight:document.body != null? 
				document.body.clientHeight:
				null;
}


function autoClick()
{
    var auto = $get("HiddenFieldAutoDial");
    if(auto)
    {
		if(auto.value == 1)
		{
			$get("HiddenFieldAutoDial").value = 0;
			document.forms[0].ButtonDial.click();
		}
    }
}

function setFocus()
{
    var fromNum = $get("TextBoxFromCountryNumber");
    if(fromNum)
    {
		fromNum.focus();
    }
}

function resizePic()
{
	var cpd = $get("cusPicDiv");
	var pic = $get("CustomerPic");
	
	if(cpd && pic)
	{
		var divWidth = parseInt(cpd.style.width);
		var divHeight = parseInt(cpd.style.height);
		
		var wDif = pic.width - divWidth;
		var hDif = pic.height - divHeight;

		
		if(hDif > wDif){
			if(hDif > 0){	
				pic.height = parseInt(cpd.style.height);}}
		else{
			if(wDif > 0){
				pic.width = parseInt(cpd.style.width);}}
	}
}


function EndRequestHandler(sender, args)
{
    // Error handler
    if (args.get_error())
    {
        args.set_errorHandled(true);
    }
    
    var state = $get("HiddenFieldState").value;
    if (state > 0)
    {
		// Hide Language Links
		var langdiv = $get("lang");
		langdiv.style.display = "none";

		// Start Timer
		if (state == 2)
			countdown_clock($get("HiddenFieldDuration").value);

		//Call GetState()
		document.forms[0].ButtonState.click();
	}
	else if (state == -9999)
		// Hang Up
		document.forms[0].ButtonEnd.click();
	else
		// Stop Timer
		countdown_stop();


	// Warn Window Close
	var hiddenLandline = $get("HiddenFieldLandlineCall");
	if (state > 0)
	{
		if(hiddenLandline && hiddenLandline.value != "1")
		{
			window.onbeforeunload = "";
		}
		else
		{
			var warningtxt = $get("HiddenFieldCloseWarning").value;
			window.onbeforeunload = new Function('return \''+warningtxt+'\';');
		}
	}
	else
		window.onbeforeunload = "";


	//Show/Hide AD
	var AdDiv = $get("dialer-ad-c2d");
	if(AdDiv && hiddenLandline)
	{
		if(state > 0 && hiddenLandline.value == "1")
		{
			AdDiv.style.display = "none";
			var warningtxt = $get("HiddenFieldCloseWarning").value;
			window.onbeforeunload = new Function('return \''+warningtxt+'\';');
		}
		else
		{
			AdDiv.style.display = "";
			window.onbeforeunload = "";
		}
	}
}



function HangUp()
{
    $get("HiddenFieldState").value = -9999;
    Click2Dial.Local.ServiceTier.HangUp($get("HiddenFieldL").value, $get("HiddenFieldA").value);
}

function numberChanged()
{
	var code = $get("TextBoxFromCountryCode");
	var number = $get("TextBoxFromCountryNumber");
	var fromnumber = $get("TextBoxFromNumber");
	if(code && number && fromnumber)
	{
		if(code.value.length > 0)
			fromnumber.value="(+" + code.value + ") " + number.value;
		else
			fromnumber.value= number.value;
	}
	
}

function countryChanged()
{
	numberChanged();
	
	var label = $get("LabelCountry");
	if(label)
	{
		var hasInnerText = ($get("LabelCountry").innerText != undefined) ? true : false;

		var arrayCountries = $get("HiddenFieldCountryCodes").value.split(";");
		var countryCode = "00" + $get("TextBoxFromCountryCode").value
		var nameIndex = arrayCountries.indexOf(countryCode) + 1;

		if(nameIndex > 0 && nameIndex < arrayCountries.length)
		{
			if(hasInnerText)
				label.innerText = arrayCountries[nameIndex];	//IE
			else
				label.textContent = arrayCountries[nameIndex];	//FireFox, NetScape
		}
		else
		{
			if(hasInnerText)
				label.innerText = "Unknown";	//IE
			else
				label.textContent = "Unknown";	//FireFox, NetScape
		}
	}	
}


//This prototype is provided by the Mozilla foundation and
//is distributed under the MIT license.
//http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
if (!Array.prototype.indexOf)
{
	Array.prototype.indexOf = function(elt /*, from*/)
	{
		var len = this.length;
		var from = Number(arguments[1]) || 0;
		from = (from < 0) ? Math.ceil(from) : Math.floor(from);
		if (from < 0)
			from += len;
		for (; from < len; from++)
		{
			if (from in this && this[from] === elt)
				return from;
		}
		return -1;
	};
}
