// JavaScript Document
function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

MM_preloadImages('images/logout.gif');
function HdrLogin() {
	//SDC, 30-Jan-2006: Trigger login/logout popup form.
	var wi=350;
	var hi=250;
	pagecc_custform = window.open('loginpopup.php?hdrmode=1'+(G_loggedin?'&logout=1':''), 'cc_login', 'width='+wi+',height='+hi+',left='+((screen.width-wi)/2)+',top='+((screen.height-(hi*2))/2)+',toolbar=0,scrollbars=1,statusbar=0,menubar=0,resizable=0');
	G_LoginOffered=true;
	if(document.getElementById('leftBoxSignup')) {
		document.getElementById('leftBoxSignup').style.display = 'block';
	}
}

function HdrLoginFlip(loggedin,userstring) {
	//alert('Calling HdrLoginFlip: '+loggedin);
	G_loggedin=loggedin;
	//Change the login button to a logout button and vice-versa.
	document.getElementById("header_login_button").src=(loggedin?"images/logout.gif":"images/login.gif");

	//also change the text under it to show login info if applicable.
	//document.getElementById('lpd_showuser').innerHTML="Logged in"+(userstring?(" as "+userstring):"");
	if (document.getElementById('lpd_showuser')) {
	document.getElementById('lpd_showuser').style.visibility=loggedin?"":"hidden";
	document.getElementById('lpd_showuser').style.display=loggedin?"":"none";
	}
	document.getElementById('lpd_lostlogin').style.visibility=loggedin?"hidden":"";
	document.getElementById('lpd_lostlogin').style.display=loggedin?"none":"";
	//-DCK:Refresh prices after login -->Czech Republic
	try{ 
        if (currpage=="1" || currpage=="2" || currpage=="3"){ReloadCurrentNums(); }
    }
    catch(e){}
	//+
	
}

var G_LoginFormAvail=false;	//this is used when you click the login/logout button to determine whether to pop up a login box, or whether the current page already has a login form on it to display.

function PopupCustForm() {
	// used somewhere else...
	//.......................
	//Allow user to create a new customer account.
	//(Might be a problem if you're blocking popups)
	// uncomment when making live on site - use correct URL though... pagecc_custform = window.open('https://www.skycomuk.com/signupform/create_account.php?tid=1', 'cc_custform', 'width=580,height=600,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');
	pagecc_custform = window.open('account.php?tid=1&folder=account_form', 'cc_custform', 'width=580,height=600,toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');
}

function hideSignupButton() {
	if(document.getElementById('leftBoxSignup')) {
		document.getElementById('leftBoxSignup').style.display = 'none';
	}
}

function forgotten()
{
  alert("Please give us a call on 08453 887744 and we'll re-send you your login details.");
}

function AddToCart(numberID,fullnumber,lookupID,redir) {
	//if(!G_username && !G_LoginOffered) {
		//var wi=350;
		//var hi=300;
		//pagecc_custform = window.open('loginpopup.php', 'cc_login', 'width='+wi+',height='+hi+',left='+((screen.width-wi)/2)+',top='+((screen.height-(hi*2))/2)+',toolbar=0,scrollbars=1,statusbar=0,menubar=0,resizable=0');
		//G_LoginOffered=true;
	//}

	var payg=1;
	if(document.getElementById("usagecheck_"+numberID)) {
		if(document.getElementById("usagecheck_"+numberID).checked) { payg=2; }
	}
	
	if (redir==null) { sucFunc='GoToCheckout'; } else { sucFunc='DisplayCartInfo'; }
	
	//alert(sucFunc);
	
	apilite_call('callout/addtocart.php?number_id='+numberID+"&lookup_id="+lookupID+"&payg="+payg, sucFunc, numberID+","+fullnumber+",a", 'ShowCartError', numberID+","+fullnumber,true);
}


sessionWarnedaboutFreeNumbers=0;
function DisplayCartInfo(responsestring,args) {
	//This routine is called by HandleResponse if the HTTP call was successful.
	var buildstring='';
	var tmp=responsestring.split("\n");	//split the returned lines into an array.
	var argv=args.split(",");

	//This routine is called by HandleResponse if the HTTP call was successful.
	var buildstring='';
	var tmp=responsestring.split("\n");	//split the returned lines into an array.
	var argv=args.split(",");
	//var cartlist='';

	//var ci_obj=document.getElementById("cartinfo");
	//var cm_obj=document.getElementById("cartclickme");

    var d_x = tmp[0].split("|");
    if (d_x[0]==7 && sessionWarnedaboutFreeNumbers==0){
        d_x2 = d_x[1].split("\n");
        alert(d_x2[0]);
        document.getElementById('nodisacountwarning').style.display='block';
        sessionWarnedaboutFreeNumbers =1;
        tmp[0] = tmp[2];//DCK
        tmp[1] = tmp[4];//DCK
    }
    else if(d_x[0]==7){
        tmp[0] = tmp[2];//DCK
        tmp[1] = tmp[4];//DCK
    }

	if(argv[0]>0) {
		if(argv[2]=='a') {
			//document.getElementById("addlink_"+argv[0]).innerHTML="<a href='https://<?=$_SERVER['HTTP_HOST'];?>/showcart.php' title='Click here to go to the checkout area.'><img src='images/incart.gif' width='60' height='16' border='0' /></a>";
		} else {
			//document.getElementById("addlink_"+argv[0]).innerHTML="<a href='javascript:AddToCart("+argv[0]+",\""+argv[1]+"\","+G_lastid+");' title='Click to add this number to your shopping cart.'><img src='images/addtocart.gif' width='60' height='16' border='0' /></a>";
			if (gObj('add_link_'+argv[0])) { gObj('add_link_'+argv[0]).innerHTML=' [<a href="javascript:AddToCart('+argv[0]+',\''+argv[1]+'\',\'361\',true);">add</a>]'; }
		}
	} else {
		if(argv[2]=='r') {
			//removing item zero? that means we're removing all the items in the cart, so refresh the table to get rid of any 'remove' graphics.
			//(this is a slightly long-winded way of parsing through the table, but it does the job)
			gObj("cartinfo").innerHTML="<span id='quicklist_0'>Your cart is empty.</span><br />";	//remove all the cart lines from the quicklist.
// 			gObj("floating_cart").style.display='none';
// 			remCart();
		}
	}
	
	if (gObj('addlink_'+argv[0]))
	{
		gObj('addlink_'+argv[0]).innerHTML="<a href='"+showcartURL+"' title='Click here to go to the checkout area.'><img src='images/incart.gif' width='60' height='16' border='0' /></a>";
	}

   
	if(tmp[0]==0) {
		gObj("cartinfo").innerHTML="<span id='quicklist_0'>Your cart is empty.</span>";
// 		gObj("floating_cart").style.display='none';
// 		remCart();
		gObj("c_mid").innerHTML='';
		//document.getElementById("cartclickme").innerHTML=cartclickme_off;
		//document.getElementById("cartinfo").style.color='red';
	} else {
		var numberlist=tmp[1].split("|");
		cartlist="<span id='quicklist_0'>&nbsp;&nbsp;You have selected:</span><br /><br />";
		
		for(var count=0; count<numberlist.length; count++) {
			var thisnum=numberlist[count].split(":");
			if(thisnum[1]) {
				cartlist+="<span id='quicklist_"+thisnum[0]+"'>&nbsp;&nbsp;&nbsp;&nbsp;"+thisnum[1];
				cartlist+=" <a href='javascript:RemoveFromCart("+thisnum[0]+",\""+thisnum[1]+"\")'>[x]</a></span><br />";
			}
		}
		cartlist+="<br>&nbsp;&nbsp;<a href='javascript:GoToCheckout()'>Go to checkout...</a>";
		gObj("c_mid").innerHTML=cartlist;
// 		gObj("floating_cart").style.display='block';
// 		addCart();
		var s=((numberlist.length-1)<2)?'':'s';
		gObj('cartinfo').innerHTML="You have "+(numberlist.length-1)+" item"+s+" in your cart.";
		//alert(argv);
		if (gObj('add_link_'+argv[0])) { gObj('add_link_'+argv[0]).innerHTML=' [<span style="color:#36C828;">done</span>]'; }
		//document.getElementById("cartclickme").innerHTML=cartclickme_on;
		//document.getElementById("cartinfo").style.color='#4ead5c'; //'green'; //#00ff33
	}
	//MainCartButton();

/*<? if($level2) { ?>
	if(G_FirstCart==1) {
		G_FirstCart=0;
		document.getElementById("number").value="<?=$remainingdigits;?>";
		G_combotext='<?=$maintype.$level2;?>';
		document.getElementById("number").maxLength=G_phonenumlength-G_combotext.length;
		if(document.getElementById("number").value.length>G_phonenumlength-G_combotext.length) {document.getElementById("number").value=document.getElementById("number").value.substring(0,G_phonenumlength-G_combotext.length);}
		ValidateNumber();
	}
<? }?>*/

	if (gObj("cartitempanel_"+argv[0]))
	{
	buildstring='';
	switch (tmp[0]) {
		case "0" : {	//delete all...
			buildstring="<center><br />Your cart is empty.<br /></center>";
			document.getElementById("ssform").innerHTML=buildstring;
			break;
		}
		default :  {	//delete one...
			document.getElementById("cartitempanel_"+argv[0]).innerHTML='';
			document.getElementById("cartitempanel_"+argv[0]).className='hidecell';
			break;
		}
	}
	if(args.length>0) {
		grandtotaldue-=linetotaldue[argv[0]];
		RecalcTots();
	}
	}

}

function doNothing()
{
	return true;
}

function addedToCart(cart_item)
{
	
}

function RemoveFromCart(numberID,fullnumber) {
	apilite_call('callout/removefromcart.php?number_id='+numberID, 'DisplayCartInfo', numberID+","+fullnumber+",r", 'ShowCartError', numberID+","+fullnumber,true);
}

function GoToCheckout()
{
  urlparams='';
  if (gObj('assign_0844_to')) if (gObj('assign_0844_to').value!='') { urlparams='?numTo='+gObj('assign_0844_to').value+"&id="+gObj('index_free_num_id').value; }
  window.location='showcart.php'+urlparams;
}

function ShowCartError(responsestring,args) {
	alert(responsestring);
	var tmp=responsestring.split("\n");	//split the returned lines into an array.
	var argv=args.split(",");
	var buildstring = '';

	switch (parseInt(tmp[0])) {
		case 3 : { buildstring="Sorry, this number ("+argv[1]+") is already allocated."; break; }
		case 4 : {
			buildstring=argv[1]+" is already in your cart.";
			document.getElementById("addlink_"+argv[0]).innerHTML="<a href='https://<?=$_SERVER['HTTP_HOST'];?>/showcart.php' title='Click here to go to the checkout area.'><img src='images/incart.gif' width='60' height='16' border='0' /></a>";
			break;
		}
		case 5 : {
			buildstring="You don't have "+argv[1]+" in your cart.";
			if (document.getElementById("addlink_"+argv[0])) document.getElementById("addlink_"+argv[0]).innerHTML="<a href='javascript:AddToCart("+argv[0]+",\""+argv[1]+"\","+G_lastid+");' title='Click to add this number to your shopping cart.'><img src='images/addtocart.gif' width='60' height='16' border='0' /></a>";
			break;
		}
		default :{ buildstring="<h2>Error</h2>An error was reported while trying to add an item to the shopping cart. The error report was:<br />"+responsestring; break; }
	}
	//buildstring+="<br /><br /><a href='showcart.php'>Click Here</a> to view your cart and proceed to the checkout."
	//buildstring+="<br /><br /><a href='javascript:RemoveFromCart(0,\"\");' title='Click to empty your shopping cart.'>Click Here</a> to reset your cart."
	if (document.getElementById('cartinfo')) { document.getElementById('cartinfo').innerHTML=buildstring; }
	if (document.getElementById('cartinfo')) { document.getElementById("cartinfo").style.color='red'; }
}

function gObj(ele)
{
  return document.getElementById(ele);
}

function setAssignNumFocus()
{
  gObj('assign_0844_to').style.backgroundImage = 'url(images/spacer.gif)';
}

function setAssignNumBlur()
{
  if (gObj('assign_0844_to').value=='') {
    gObj('assign_0844_to').style.backgroundImage = 'url(images/assign_num_bg.gif)';
  }
}

function assignFree0844()
{
  numTo=gObj('assign_0844_to').value;
  numFrom=gObj('index_free_fullnumber').value;
  numId=gObj('index_free_num_id').value;
  numLookupId=gObj('index_free_lookup_id').value;
  if (numTo=='' || numTo.match(/[a-zA-Z]/)) { alert("You must enter a valid UK phone number"); } // very basic - needs to use global number checking thing
  else { AddToCart(numId,numFrom,numId) }
}

function showHideInfo(ele)
{
	isHidden=(gObj(ele+'_more').style.display=='none')?true:false;
	gObj(ele+'_link').innerHTML=(isHidden)?'&lt;&lt;&lt; View Less':'View More &gt;&gt;&gt;';
	gObj(ele+'_more').style.display=(isHidden)?'inline':'none';
}
