var old_card_id = 0;
var web_url = window.location.protocol + "//" + window.location.hostname;


// display the video in a product page
function create_video(product_id)
{
	var url_additional = JS_WEB_COMMON+"products/" + product_id + "/default/web/trailers/tv/trailer.swf";
	swfobject.embedSWF(url_additional, "video_container", "100%", "100%", "8.0.0"); 
	show_media();
	disableInProgress();
}

// display the description in a product page
function show_description(product_id)
{
	// prevent a user from navigating away if an AJAX request in underway
	if (ajax_in_progess && ajax_in_progess == true)
	{
		return;
	}

	var media = document.getElementById("week-presentation-media");
	var text = document.getElementById("week-presentation-text");
	
	media.innerHTML = '&nbsp;';
	media.style.display = 'none';
	text.style.display = 'block';
}

// display any media in a product page (screenshot, video, ...)
function show_media()
{
	var media = document.getElementById("week-presentation-media");
	var text = document.getElementById("week-presentation-text");
	
	media.style.display = 'block';
	text.style.display = 'none';
}

// enable all credit card informations in the payment process
function enableCCInfos()
{
	var infos_to_enable = new Array('holdername','card_type_1','card_type_2','card_type_3','card_type_4','card_number','validation_code','expiration_month','expiration_year');
	for(var cpt_info = 0; cpt_info < infos_to_enable.length; cpt_info++)
	{
		if(document.getElementById(infos_to_enable[cpt_info]))
		{
			document.getElementById(infos_to_enable[cpt_info]).disabled = false;
		}
	}
	// we display the div with these informations
	if(document.getElementById('div_infos_cc'))
	{
		document.getElementById('div_infos_cc').style.display = '';
	}
}


// disable all credit card informations in the payment process
function disableCCInfos()
{
	var infos_to_disable = new Array('holdername','card_type_1','card_type_2','card_type_3','card_type_4','card_number','validation_code','expiration_month','expiration_year');
	for(var cpt_info = 0; cpt_info < infos_to_disable.length; cpt_info++)
	{
		if(document.getElementById(infos_to_disable[cpt_info]))
		{
			document.getElementById(infos_to_disable[cpt_info]).disabled = true;
		}
	}
	// we hide the div with these informations
	if(document.getElementById('div_infos_cc'))
	{
		document.getElementById('div_infos_cc').style.display = 'none';
	}
	
}

// enable all credit card informations in the payment process
function enableUkashInfo()
{
	var infos_to_enable = new Array('voucher_number','voucher_value','ticket_value');
	for(var cpt_info = 0; cpt_info < infos_to_enable.length; cpt_info++)
	{
		if(document.getElementById(infos_to_enable[cpt_info]))
		{
			document.getElementById(infos_to_enable[cpt_info]).disabled = false;
		}
	}
	// we display the div with these informations
	if(document.getElementById('info_ukash'))
	{
		document.getElementById('info_ukash').style.display = '';
	}
}


// disable all credit card informations in the payment process
function disableUkashInfos()
{
	var infos_to_disable = new Array('voucher_number','voucher_value','ticket_value');
	for(var cpt_info = 0; cpt_info < infos_to_disable.length; cpt_info++)
	{
		if(document.getElementById(infos_to_disable[cpt_info]))
		{
			document.getElementById(infos_to_disable[cpt_info]).disabled = true;
		}
	}
	// we hide the div with these informations
	if(document.getElementById('info_ukash'))
	{
		document.getElementById('info_ukash').style.display = 'none';
	}
	
}

// display a waiting icon
function displayWait(div_to_display)
{
	document.getElementById(div_to_display).innerHTML = '<img src="'+JS_WEB_STATIC_COMMON+'images/billing/loading.gif">';
}

// hide a waiting icon
function stopDisplayWait(div_to_display)
{
	document.getElementById(div_to_display).innerHTML = '';
	
	if(document.getElementById('product-billing-process'))
	{
		var xhr_text =document.getElementById('product-billing-process').innerHTML;
		if(xhr_text.startsWith("http://dl.gameloft.com/cm/t0/"))
		{
			document.getElementById('product-billing-process').style['display']="none";
			
			document.getElementById('product-billing-processtop').style['display']="block";
			document.getElementById('product-billing-processtop').innerHTML=thanks_for_purchase;
			document.getElementById('nextEulaBtn').style['display']="none";
            if(document.getElementById('goHomeBtn'))
			document.getElementById('goHomeBtn').style['display']="block";
			window.location=xhr_text;				
		}
		else
		{
            if(document.getElementById('goHomeBtn'))
			document.getElementById('goHomeBtn').style['display']="block";
			document.getElementById('nextEulaBtn').style['display']="none";
			document.getElementById('product-billing-processtop').style['display']="none";
			document.getElementById('product-billing-process').style['marginTop']="18px";
			document.getElementById('product-billing-process').style['display']="block";
			
		}
		
	}
}

// signal to a script that the AJAX loading is over
function disableInProgress()
{
	if (ajax_in_progess)
	{
		ajax_in_progess = false;
	}
}


/**
 *
 */

String.prototype.isEmail=function(){
		var regex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		return regex.test(this);
}
// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

function validateEmail(addr) 
{
	var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
	for (i=0; i<invalidChars.length; i++) {
	   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
	      return false;
	   }
	}
	for (i=0; i<addr.length; i++) {
	   if (addr.charCodeAt(i)>127) {
	      return false;
	   }
	}
	
	var atPos = addr.indexOf('@',0);
	if (atPos == -1) {
	   return false;
	}
	if (atPos == 0) {
	   return false;
	}
	if (addr.indexOf('@', atPos + 1) > - 1) {
	   return false;
	}
	if (addr.indexOf('.', atPos) == -1) {
	   return false;
	}
	if (addr.indexOf('@.',0) != -1) {
	   return false;
	}
	if (addr.indexOf('.@',0) != -1){
	   return false;
	}
	if (addr.indexOf('..',0) != -1) {
	   return false;
	}
	var suffix = addr.substring(addr.lastIndexOf('.')+1);
	if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
	   return false;
	}
	return true;
}


//flush the "my account" menu
function showAccountMenu()
{
	stopDisplayWait("account-loading");
	document.getElementById('account-menu').style.display = 'block';
}

//flush the "my account" menu
function clearAccountMenu()
{
	stopDisplayWait("account-loading");
	document.getElementById('account-menu').style.display = 'none';
}


//Confirm that a user wants to delete a card from his account
function confirmDeleteMyCard(id, in_payment, text)
{
	if (confirm(text))
	{
		if (in_payment)
		{
			ajaxPaymentDeleteMyCard(id);
		}
		else
		{
			ajaxDeleteMyCard(id);
		}
	}
}

//Confirm that a user wants to delete a phone from his account
function confirmDeleteMyPhone(id, text)
{
	if (confirm(text))
	{
		ajaxDeleteMyPhone(id);
	}
}

//Redirect to a specified page
function redirect(page)
{
	/*	Special page code to perform a specific action	*/
	
	//On successful login, show the user menu
	if (page == "myAccountLogin")
	{
	}
	
	window.location = page;
}

//Load the content of a "My Account" section depending of the AJAX answer
function loadContentMyAccount()
{
	stopDisplayWait('account-loading');
	
	//check if user is simply logging in or wishes to post a comment (1= log in 2= rate)
	if (document.getElementById('ajax_answer').value == "1")
	{
		showAccountMenu();
	}
	else if(document.getElementById('ajax_answer').value == "2")
	{
		//redirect to page ajax_redirect
		redirect(document.getElementById('ajax_redirect').value);
	}
}

function checkRedirectOnRating()
{
	//if user is either not logged or do not have a nickname
	if(document.getElementById('ajax_answer').value == "1")
	{
		redirect(JS_WEB_URL+'my_account.php?wc='+JS_FS_FOLDER_LOCAL_NAME+'&ratingMessage=1');
	}
	else if(document.getElementById('ajax_answer').value == "2")
	{
		redirect(JS_WEB_URL+'my_account.php?wc='+JS_FS_FOLDER_LOCAL_NAME+'&ratingMessage=2');
	}
	else
	{
		//if everything is set, let's do what we do best !
		show_media();
	}
}

// allows only numbers in an input (everywhere there's a phonenumber input)
function onlyNumbers(event) {

	// Compatibility IE / Firefox  
	if (!event&&window.event)
	{ 
		event = window.event; 
	}  
	// IE  
	if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 9 && event.keyCode != 39 && event.keyCode != 37 && event.keyCode != 8 && event.keyCode != 46) 
	{  
		event.returnValue = false;
		event.cancelBubble = true; 
	}  
	// DOM  
	if ((event.which < 48 || event.which > 57) && event.which != 9 && event.which != 39 && event.which != 37 && event.keyCode != 8 && event.keyCode != 46) 
	{  
		event.preventDefault();
		event.stopPropagation(); 
	}
}


// check if a given sentence is an integer
function isInteger(string)
{
	var valid_chars = "0123456789";
	var char;
	
	// an empty string is NOT a number
	if (string.length == 0)
	{
		return false;
	}
	
	// check the validity of each character
	for (i = 0; i < string.length; i++) 
	{ 
		char = string.charAt(i); 
		if (valid_chars.indexOf(char) == -1) 
		{
			return false;
		}
	}
	return true;
}


// display the country list only if a phone number is entered (no strings allowed)
function loginDisplayCountries(elem)
{
	if (isInteger(elem.value))
	{
		document.getElementById('country_select').style.display = 'block';
	}
	else
	{
		document.getElementById('country_select').style.display = 'none';
	}
	return false;
}


function highlightDefaultCard(id)
{
	//Only run this if the user selected a different manufacturer
	if (old_card_id != id)
	{
		document.getElementById('card_type_' + id).className = 'account-element-highlight';
		document.getElementById('card_number_' + id).className = 'account-element-highlight';
		document.getElementById('card_holder_' + id).className = 'account-element-highlight';
		document.getElementById('card_expiration_' + id).className = 'account-element-highlight';
		document.getElementById('card_radio_' + id).className = 'account-element-highlight';
		if (old_card_id != 0)
		{
			document.getElementById('card_type_' + id).className = 'account-element';
			document.getElementById('card_number_' + id).className = 'account-element';
			document.getElementById('card_holder_' + id).className = 'account-element';
			document.getElementById('card_expiration_' + id).className = 'account-element';
			document.getElementById('card_radio_' + id).className = 'account-element';
		}
		old_card_id = id;
	}
}

//Catch when a user press Enter in a form and call another function for processing
function catchEnterKey(callFunction, e)
{
	var keycode;
	
	//Get the keycode based on each browser
	if (window.event)
	{
		keycode = window.event.keyCode;
	}
	else if (e)
	{
		keycode = e.which;
	}
	else
	{
		return true;
	}

	//If Enter was pressed, call the wanted function
	if (keycode == 13)
	{
		eval(callFunction);
		return false;
	}

	return true;
}
function showPaymentCarousel()
{
	var flashvars = {  urlStr: JS_WEB_URL + "xml_carousel.php?use_session=1"};  
	swfobject.embedSWF(JS_WEB_STATIC_COMMON+"swf/carousel.swf", "youmaylike-content", "500", "200", "9.0.0", false, flashvars); 
}

function step3_card_id(id)
{
	document.getElementById("card_id").value = id;
}

// enable all w_ha informations in the payment process
function enableWhaInfos()
{
	var infos_to_enable = new Array('home_phonumber','vars','pid','action');
	for(var wha_info = 0;wha_info < infos_to_enable.length; wha_info++)
	{
		if(document.getElementById(infos_to_enable[wha_info]))
		{
			document.getElementById(infos_to_enable[wha_info]).disabled = false;
		}
	}
	// we display the div with these informations
	if(document.getElementById('div_infos_w-ha'))
	{
		document.getElementById('div_infos_w-ha').style.display = '';
	}
}

// disable all w_ha informations in the payment process
function disableWhaInfos()
{
	var infos_to_disable = new Array('home_phonumber','vars','pid','action');
	for(var wha_info = 0; wha_info < infos_to_disable.length; wha_info++)
	{
		if(document.getElementById(infos_to_disable[wha_info]))
		{
			document.getElementById(infos_to_disable[wha_info]).disabled = true;
		}
	}
	// we hide the div with these informations
	if(document.getElementById('div_infos_w-ha'))
	{
		document.getElementById('div_infos_w-ha').style.display = 'none';
	}
}

//show all comments in product page
function showAllComments()
{
	var available = true;
	var counter = 1;
	while(available)
	{
		var all_comments = document.getElementById('invisible_comments' + counter)
		if(all_comments)
		{
			all_comments.style.display = 'block';
		}
		else
		{
			available = false;
			document.getElementById('link-more').style.display = 'none';
		}
		counter ++;
	}
}

//assign the product infos into the hidden input
function assignProductInfos(product_id, price, type)
{
	document.getElementById('product_id').value = product_id;
	document.getElementById('product_price').value = price;
	document.getElementById('product_type').value = type;
}

//launch the download of a UMPC game
function launchUMPCDemoDownload()
{
	if (document.getElementById('demo-download-url'))
	{
		url = document.getElementById('demo-download-url').innerHTML;
		url = url.replace(/&amp;/, '&');
				umpc_auto_start = umpc_auto_start.replace("aquí","<a href='" +url + "'>aquí</a>");
		umpc_auto_start = umpc_auto_start.replace("here","<a href='" +url + "'>here</a>");
		umpc_auto_start = umpc_auto_start.replace("qui","<a href='" +url + "'>qui</a>");
		umpc_auto_start = umpc_auto_start.replace("ici","<a href='" +url + "'>ici</a>");
		umpc_auto_start = umpc_auto_start.replace("hier","<a href='" +url + "'>hier</a>");
		document.getElementById("product-billing-processtop").innerHTML = umpc_auto_start;
		document.getElementById("nextEulaBtn").style.display = "none";
		
			document.getElementById('goHomeBtn').style['display']="block";
		document.getElementById('nextEulaBtn').style['display']="none";
		document.getElementById('product-billing-processtop').style['display']="block";
		document.getElementById('product-billing-process').style['marginTop']="18px";
		//document.getElementById('product-billing-process').innerHTML=errtext;
		document.getElementById('product-billing-process').style['display']="block";
		stopDisplayWait("error_box");
		document.getElementById('product-billing-processtop').style['display']="block";
		window.location = url;
	}
}


String.prototype.startsWith=function(s)
{
	return this.indexOf(s)==0;
}
