

function getCardsByName()
{
	var cardName = document.SearchForm["CallingCardName"].value;
	
	var url = "/search/";
	url = url + encode(cardName) + "_calling_card.html";	
	
	document.location = url;
	
}		


function filterSelectChanged(filterSelect)
{
	var url = document.location.href;
	var lastIndex = url.lastIndexOf("s");
	var index = url.indexOf('?');
	
	if (index > -1) {
		url = url.substring(0, index);
	}
	url = url.substring(0, lastIndex + 1); // remove page number
	url += ".html";
	url += "?CardType=" + filterSelect.value;
	document.location.href = url;
}



function cardNameKeyPressed()
{
	
	var code;
	var e = window.event;
	
	
	if (e.keyCode == 13 || e.keyCode == 3)	{
		if (e && e.preventDefault)
			e.preventDefault();
		getCardsByName(); 
		return false;
	}  
	
}				

function phoneCardSearchKeyPressed()
{
	
	var code;
	var e = window.event;
	
	if (e.keyCode == 13 || e.keyCode == 3)	{
		//alert("code: " + e.keyCode);
		getCards();
	}

}

function getCards()
{
	if (document.SearchForm["SearchControl$CountrySelectControl1$CountrySelect"] != null) 
	{
		var fromCountry = document.SearchForm["SearchControl$CountrySelectControl1$CountrySelect"].value;
		var toCountry = document.SearchForm["SearchControl$CountrySelectControl2$CountrySelect"].value;
	
		if (fromCountry == "0") {
			alert("Please select \"From Country\"");
		}
		else if  (toCountry == "0")	{
		
			alert("Please select \"To Country\"");
		}
		else {
			var url = "/best_rates/";
			url = url + encode(fromCountry) + "/";	
			url = url + encode(toCountry) + "_calling_cards_1.html";							
			document.location = url;
		}
	}
	else
	{
		getCardsMain();
	}
}

function getCardsMain()
{
	var fromCountry = document.SearchForm["MainSearchControl$CountrySelectControl1$CountrySelect"].value;
	var toCountry = document.SearchForm["MainSearchControl$CountrySelectControl2$CountrySelect"].value;
	
	if (fromCountry == "0") {
		alert("Please select \"From Country\"");
	}
	else if  (toCountry == "0")	{
	
		alert("Please select \"To Country\"");
	}
	else {
		var url = "/best_rates/";
		url = url + encode(fromCountry) + "/";	
		url = url + encode(toCountry) + "_calling_cards_1.html";							
		document.location = url;
	}
}

function encode(str)
{
    var myStr = replaceIt(str, " ", "_");
    myStr = replaceIt(myStr, "*", "-star-");
    myStr = replaceIt(myStr, "&", "-and-");
    myStr = replaceIt(myStr, ".", "-dot-");
    myStr = replaceIt(myStr, "%", "-percent-");
    myStr = replaceIt(myStr, "?", "-question-");
    myStr = replaceIt(myStr, "/", "-slash-");
    
	return myStr;
	
}

function replaceIt(sString, sReplaceThis, sWithThis) { 
	if (sReplaceThis != "" && sReplaceThis != sWithThis) { 
		var counter = 0; 
		var start = 0; 
		var before = ""; 
		var after = ""; 
		while (counter<sString.length) { 
			start = sString.indexOf(sReplaceThis, counter); 
			if (start == -1) { 
				break; 
			} 
			else { 
				before = sString.substr(0, start); 
				after = sString.substr(start + sReplaceThis.length, sString.length); 
				sString = before + sWithThis + after; 
				counter = before.length + sWithThis.length; 
			} 
		} 
	} 
	return sString; 
} 


window.onload=function()
{
	document.onkeypress = cancel_Enter;
};

function cancel_Enter(e)
{
	if (!e) var e = window.event;  //for IE
	
	var code;
	if(e.keyCode) code = e.keyCode; //for IE
	if(e.which) code = e.which;  //for other browsers

	if (code == 13) return false;
	return true;
}
				
				

function buy(id, value)
{
	var str;
	
	str = location.href;
	//str = "http://secure.getmecallingcards.com/cart.asp?addcart.x=22&addcart.y=7"
	str = "http://www.getmecallingcards.com/buy/Buy.aspx";
	//cart.asp?product=918&action=Add&VALUE=5&addcart.x=25&addcart.y=8
	window.location.href = str + "?PhoneCardID=" + id  
				+ "&Action=add"
			//	+ "&Qty=" + document.getElementById(id + '_' + value).value
				+ "&Value=" + value;
	
}			 

function goHome()
{
	var str;

	str = "http://www.getmecallingcards.com";
	//alert(str);
	//window.document.location = str;	
	parent.location=str;
	

}		


function goMyPlace(path)
{
	var str;

	
	
	str = replaceIt(path, "http://www.getmecallingcard.com/","");
		
	var pUrl;
	if (parent != null)	
	{
		pUrl = parent.location.href + "";
	}
	if (pUrl == null)	
	{
		pUrl = location.href;
	}

	//alert("location: " + pUrl);

	if (pUrl != null)	{
		if (pUrl.indexOf("/p/refill.php") == -1
			&& pUrl.indexOf("common/cmn/contact.php") == -1
			&& pUrl.indexOf("common/cmn/phone-card-feedback.php") == -1
			&& pUrl.indexOf("/users/?l=") == -1
			&& pUrl.indexOf("/common/watch.php/") == -1
			&& pUrl.indexOf("/users/?login_redirect=") == -1
			&& pUrl.indexOf("references/SMSCall") == -1
			&& pUrl.indexOf("/webcall/") == -1
			&& pUrl.indexOf("/complete.php") == -1
			&& pUrl.indexOf("/cart/invoice_history.php") == -1
			&& pUrl.indexOf("/smscall/") == -1
			&& pUrl.indexOf("/reg/update.php") == -1
			&& pUrl.indexOf("/reg/logout.php") == -1
			&& pUrl.indexOf("/p/toll-free.php") == -1
			&& pUrl.indexOf("/p/voip.php") == -1
			&& pUrl.indexOf("/references/PrepaidConferenceCalls") == -1
			&& pUrl.indexOf("/ctc/") == -1
			&& pUrl.indexOf("/cart/?id_invoice") == -1
			&& pUrl.indexOf("/calling-cards/USA?coupon") == -1
			&& pUrl.indexOf("Phone-Card/FastWorld/Global-Access") == -1
			&& pUrl.indexOf("/Phone-Card/") == -1
			
			)
		{
			
			if (pUrl.indexOf("p/card-rate.php/61") > -1) 
			{
				window.location.href = "http://www.getmecallingcards.com/rates/-/WorldCallback_calling_card.html";
			}
			else {
				var index = pUrl.indexOf("http://www.getmecallingcards.com/");
				if (index == -1)	{
					window.location.href = "http://www.getmecallingcards.com/" + str;
				}	
			}
		}
	}
}	



function calcHeight()
{
   /*if(document.getElementById && !(document.all)) {
	h = document.getElementById('PaymentGateway').contentDocument.body.scrollHeight;
	document.getElementById('PaymentGateway').style.height = h;
   }
   else if(document.all) {
	h = document.frames('PaymentGateway').document.body.scrollHeight;
	document.all.PaymentGateway.style.height = h;
   }*/


}

/*
moz=document.getElementById&&!document.all
mozHeightOffset=20

function resize_iframe(){
document.getElementById("iframe_id").height=100 // required for Moz bug, value can be "", null, or integer
document.getElementById('iframe_id').height=window.frames["iframe_name"].document.body.scrollHeight+(moz?mozHeightOffset:0)
}
*/

function calcHeight()
{
   /*if(document.getElementById && !(document.all)) {
	h = document.getElementById('PaymentGateway').contentDocument.body.scrollHeight;
	document.getElementById('PaymentGateway').style.height = h;
   }
   else if(document.all) {
	h = document.frames('PaymentGateway').document.body.scrollHeight;
	document.all.PaymentGateway.style.height = h;
   }*/

//document.getElementById("PaymentGateway").height=100 // required for Moz bug, value can be "", null, or integer
//document.getElementById('PaymentGateway').height=window.frames["PaymentGateway"].document.body.scrollHeight+(moz?mozHeightOffset:0)

	var loc = "";

	loc = document.getElementById("PaymentGateway").src;
	document.getElementById("PaymentGateway").height=1150;
	//alert (loc);	
/*	if (loc.indexOf("/reg/") > -1)	{
		document.getElementById("PaymentGateway").height=900;
	}
	else if (loc.indexOf("/common/") > -1)	{
		document.getElementById("PaymentGateway").height=1024;
	}

	else {
		document.getElementById("PaymentGateway").height=1024;
	}
*/


}



/****************************************
	Ajax call for calling card information.
****************************************/
var xmlHttp; // object for making http requests.
var name;
function showMainSearchBox()
{
	xmlHttp = getXmlHttpObject();
	if (xmlHttp != null)
	{
		var url = "/ajax/MainSearchBox.aspx";		
		//alert(url);
		xmlHttp.onreadystatechange = stateChangedMainSearch;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function showDetails(name, fromID, toID)
{
	
	
	xmlHttp = getXmlHttpObject();
	if (xmlHttp != null)
	{
		var url = "/ajax/PhoneCardPopup.aspx?CardName=" +  escape(name)
			+ "&CallFromID="
			+  fromID
			+ "&CallToID="
			+ toID;		
		//alert(url);
		xmlHttp.onreadystatechange = stateChanged;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function showSecureDetails(name)
{
	
	xmlHttp = getXmlHttpObject();
	if (xmlHttp != null)
	{
		var url = "/ajax/" +  name + "_phone_card.html";
		xmlHttp.onreadystatechange = stateChanged;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function showCashBack(name)
{
	
	
	xmlHttp = getXmlHttpObject();
	if (xmlHttp != null)
	{
		var url = "/ajax/CashBack.aspx?CardName=" +  escape(name);
			
		//alert(url);
		xmlHttp.onreadystatechange = messageStateChanged;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function showLocalAccess(productID)
{
	xmlHttp = getXmlHttpObject();
	if (xmlHttp != null)
	{
		var url = "/ajax/LocalAccess.aspx?ProductID=" +  escape(name);
			
		//alert(url);
		xmlHttp.onreadystatechange = stateChanged;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}
function getXmlHttpObject()
{
	var obj;
	obj = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	return obj;
}


/******************************************
	Event method registered for Ajax call completion.
*********************************************/
/*
function stateChanged() 									 
{
	if (xmlHttp.readyState == 4 
		|| xmlHttp.readyState == "complete")
	{
		var str = xmlHttp.responseText;

		if (isIE())	{
			overlib(str, SHADOW, WRAP, SHADOWCOLOR,'#cccccc', STICKY, FULLHTML, 
					 BORDER, 1, MOUSEOFF, VAUTO, HAUTO, CLOSECLICK);
		}
		else {
			overlib(str, STICKY, FULLHTML, 
					BORDER, 1, MOUSEOFF, VAUTO, HAUTO, CLOSECLICK);
		
		}
	}
}
*/
function stateChangedMainSearch() 									 
{
	
	if (xmlHttp.readyState == 4 
		|| xmlHttp.readyState == "complete")
	{
		var str = xmlHttp.responseText;
		var element = document.getElementById("MainSearchBox");
		
		element.innerHTML = str;
	}
}
function stateChanged() 									 
{
	var closeBox = 'close here >> <span title="Click to close" style="background-color: #CCCCCC; border:2px #B30922 outset; padding: 2px; color: #000000; font-size: 9px; font-weight: bold;">&nbsp;X&nbsp;</span>';
	if (xmlHttp.readyState == 4 
		|| xmlHttp.readyState == "complete")
	{
		var str = xmlHttp.responseText;

		
		overlib(str, 
				CAPTION,'Phone Card Details',
				CAPTIONSIZE, '14px', 
				CAPCOLOR, '#B30922', 
				CLOSETEXT, closeBox, 
				CLOSECLICK, 
				STICKY, 
				BGCLASS, 'popup-box-style', 
				CAPTIONFONTCLASS, 'popup-caption-style', 
				CLOSEFONTCLASS, 'popup-close-style',WRAP);
		
	}
}
function messageStateChanged() 									 
{
	var closeBox = 'close here >> <span title="Click to close" style="background-color: #CCCCCC; border:2px #B30922 outset; padding: 2px; color: #000000; font-size: 12px; font-weight: bold;">&nbsp;X&nbsp;</span>';
	if (xmlHttp.readyState == 4 
		|| xmlHttp.readyState == "complete")
	{
		var str = xmlHttp.responseText;

		
		overlib(str, 
				CAPTION,'Earn 3% Cash Back',
				CAPTIONSIZE, '14px', 
				CAPCOLOR, '#B30922', 
				CLOSETEXT, closeBox, 
				CLOSECLICK, 
				STICKY, 
				BGCLASS, 'popup-box-style', 
				CAPTIONFONTCLASS, 'popup-caption-style', 
				CLOSEFONTCLASS, 'popup-close-style',WRAP);
		
	}
}

function isIE()
{
	var ie = false;
	var browserName = navigator.appName;
	if (browserName == "Microsoft Internet Explorer")
	{
		
		ie = true;
		
	}

	return ie;
}

