// Get server url
var BaseUrl = location.protocol+'//'+location.host;
var SPA = location.pathname.split('/');
for(var i=0; i<SPA.length; i++){
	BaseUrl += SPA[i]+'/';
	if(SPA[i] == 'appstore'){
		break;
	}
}
var _Width = screen.availWidth;
var _Height = screen.availHeight;
var showtime = 0;
var divCont = '';



// End
document.write('<div id="idiv" style="position:absolute;"></div>');


function ShowCatApps(_name, cntid)
{
	cntid = cntid+'/' || null;
	_URL = BaseUrl+'ajax.php?aq=getcat&ajxid='+encodeURIComponent(_name);
	_GCN = _str = '';
	Const1 = 'app';
	new Ajax.Request(_URL, {
		method: 'get', 
		onSuccess: function(ajx){
			_GCN = ajx.responseText;
			if(_GCN != ''){
				Const1 = 'apps'
				_str = _GCN+'/';
			}
			__url = _str+_name;
			ActionUrl = BaseUrl+cntid+Const1+'/'+__url;
			document.forms['_cfcntid'].method = 'post';
			document.forms['_cfcntid'].action = ActionUrl;
			document.forms['_cfcntid'].submit();
		}
	});
	
	return true;
}

function searchF(_url, sort, type, _bol)
{
	var _d = document;
	var _bool = _bol || 0;
	var sort = sort || 0;
	var type = type || 'avgrating';
	var _catN = $('iseoc').value;
	var _scatN = $('ajxctn').value;
	var sqryVal = _d.getElementById('qs').value;
	if(sqryVal.length <= 0 || sqryVal == 'Search for keywords...'){
		alert('Please enter kywords to search!');
		_d.getElementById('qs').focus();
		return false;
	}
	_d.getElementById('hsrc').value = sqryVal;
	var paterns = /[^a-z0-9]/gi;
	// Replace string
	var repSTR = sqryVal.replace(paterns, '_');
	// Split string
	var spltSTR = repSTR.split('_');
	var _str = __cststr = '';
	for(var i=0; i<spltSTR.length; i++){
		if(spltSTR[i] != ''){
			_str += spltSTR[i]+'_';
		}
	}
	_str = _str.substr(0, (_str.length-1));
	
	
	if(_bool == 1 || _bool == 2){
		_URL = BaseUrl+'ajax.php?aq=getcat&ajxid='+encodeURIComponent(_catN);
		new Ajax.Request(_URL, {
			method: 'get', 
			onSuccess: function(ajx){
				_scatN = ajx.responseText;
				if(_catN == '-allcats'){
					_scatN = '';
				}
				if(_scatN != ''){
					__cststr = __cststr+_scatN+'/';
				}
				if(_catN != '-allcats'){
					__cststr = __cststr+_catN+'/';
				}
				var __url = _url+'/search/'+__cststr+''+_str+'/'+sort+'/'+type+'/page/1';
				window.location.href = __url;
			}
		});
		return false;
	}
	else{
		if(_catN == '-allcats'){
			_scatN = '';
		}
		if(_scatN != ''){
			__cststr = __cststr+_scatN+'/';
		}
		if(_catN != '-allcats'){
			__cststr = __cststr+_catN+'/';
		}
		var __url = _url+'/search/'+__cststr+''+_str+'/'+sort+'/'+type+'/page/1';
		_d.forms['sFrm01'].method = 'post';
		_d.forms['sFrm01'].action = __url;
		return true;
	}
	//return false;
}


// Function to book mark page
function BookMark(title, url)
{
	var ua = navigator.userAgent.toLowerCase();
	var isSafari = (ua.indexOf('webkit') != -1);
	
	// Firefox
	if(window.sidebar){
		window.sidebar.addPanel(title, url, "");
	}
	// Opera
	else if(window.home || (window.opera && window.print)){
		var elem = document.createElement('a');
		elem.setAttribute('href', url);
		elem.setAttribute('title', title);
		elem.setAttribute('rel', 'sidebar');
		elem.click();
	}
	// IE
	else if(document.all){
		window.external.AddFavorite(url, title);
	}
	else if(isSafari){
		alert('You need to press Command/Cmd + D to bookmark this page.');
	}
}

//
function SubmitSelect(url, str, value, sort, page)
{
	var sort = 1;
	var value = value || 'releasedate';
	if((value=='paid' || value == 'application' || value == 'rank') && sort == 1){
		sort = 0;
	}
	if(value=='free'){
		sort=1;
	}
	var page = page || 1;
	var pgQ = '';
	if(page > 0){
		pgQ = '/page/'+page;
	}
	var _sort = sort+'/'+value;
	// Join string with forward slash
	var _url = url+'/'+str+'/'+_sort+pgQ;
	document.location.href = _url;
}

// Toggle functions
var Toggle = {
	show: function(){
		for(var i=0; i<arguments.length; i++){
			$(arguments[i]).style.display = 'block';
			$(arguments[i]+'img').src = BaseUrl+'/graphics/arrow_down.gif';
		}
	},
	hide: function(){
		for(var j=0; j<arguments.length; j++){
			$(arguments[j]).style.display = 'none';
			$(arguments[j]+'img').src = BaseUrl+'/graphics/arrow_up.gif';
		}
	},
	toggle: function(){
		for(var k=0; k<arguments.length; k++){
			if($(arguments[k]).style.display == 'none'){
				$(arguments[k]).style.display = 'block';
				$(arguments[k]+'img').src = BaseUrl+'/graphics/arrow_down.gif';
				if(arguments[k] == 'iFilterO'){
					$('pldiv1').src = BaseUrl+'/graphics/accordion_standard_left_opened.gif';
					$('pldiv2').src = BaseUrl+'/graphics/accordion_standard_right_opened.gif';
					$('pldiv3').style.display = 'block';
				}
			}
			else{
				$(arguments[k]).style.display = 'none';
				$(arguments[k]+'img').src = BaseUrl+'/graphics/arrow_up.gif';
				if(arguments[k] == 'iFilterO'){
					$('pldiv1').src = BaseUrl+'/graphics/accordion_standard_left_closed.gif';
					$('pldiv2').src = BaseUrl+'/graphics/accordion_standard_right_closed.gif';
					$('pldiv3').style.display = 'none';
				}
			}
		}
	}
};

// Dollar function
function $()
{
	var elements = new Array();
	for(var i=0; i<arguments.length; i++){
		var element = arguments[i];
		if(typeof element == 'string')
			element = document.getElementById(element);
		if(arguments.length == 1)
			return element;
		elements.push(element);
	}
	//alert(elements);
	return elements;
}



/*
Event.observe(window, 'load', function(){
	$('featureapp').observe('click', function(){
		var url = BaseUrl+'features.php';
		new Ajax.Request(url, {
			method: 'get', 
			onSuccess: function(ajx){
				$('idiv').update(ajx.responseText);
				$('idiv').style.left = '420px';
				$('idiv').style.top = '20px';
			}
		});
	});
});

*/
function CheckEmail(email)
{
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if(!filter.test(email)){
		return false;
	}
	return true;
}








/*
// Make featured applicatio
function MakeFeaturedApp(id, idiv, _e)
{
	var url = BaseUrl+'features.php?apid='+id;
	var __X = CordX(_e);
	//var __Y = CordY(_e);
	
	new Ajax.Request(url, {
		method: 'get', 
		onSuccess: function(ajx){
			$(idiv).innerHTML = ajx.responseText;
			$(idiv).style.left = (__X-320)+'px';
			//$(idiv).style.top = __Y +'px';
		}
	});
}
*/
function MakeFeaturedApp(idiv, _e)
{
	var url = BaseUrl+'features.php';
	var __X = CordX(_e);
	new Ajax.Request(url, {
		method: 'get', 
		onSuccess: function(ajx){
			$(idiv).innerHTML = ajx.responseText;
			$(idiv).style.left = (__X-320)+'px';
		}
	});
}


function Commit()
{
	var iAppName = encodeURIComponent($('iAppName').value);
	var iName = encodeURIComponent($('iName').value);
	var iEmail = $('iEmail').value;
	var iSubmit = encodeURIComponent($('iSubmit').value);
	var iComment = encodeURIComponent($('iComment').value);
	var _str = '';
	
	if(iAppName == '' || iAppName == encodeURIComponent('Application Name')){
		_str += 'Please enter application name.'+"\n";
	}
	if(iName == '' || iName == 'Name'){
		_str += 'Please enter your name.'+"\n";
	}
	if(!CheckEmail(iEmail)){
		_str += 'Please enter valid email address.'+"\n";
	}
	if(iSubmit == '' || iSubmit == 'Subject'){
		_str += 'Please enter subject.'+"\n";
	}
	if(iComment == '' || iComment == 'Comments'){
		_str += 'Please enter your comments.';
	}
	if(_str.length > 0){
		alert(_str);
	}
	else{
		$('msgdiv').innerHTML = '<b style="color:#F47A00">Please wait...</b>';
		var url = BaseUrl+'ajax.php?MFA=MailFetApp&N='+iName+'&E='+iEmail+'&S='+iSubmit+'&C='+iComment+'&A='+iAppName;
		new Ajax.Request(url, {
			method: 'get', 
			onSuccess: function(ajx){
				if(ajx.responseText == 1){
					$('msgdiv').innerHTML = '<div class="success">Thank you. Someone will be in touch with you shortly.</div>';
					AutoCloseDiv('feuappsdiv', 2000);
				}
				else{
					$('msgdiv').innerHTML = '<div class="errors">Unable to sent mail.</div>';
				}
			}
		});
	}
}

function CloseDiv(idiv, _bool)
{
	_bool = _bool || false;
	$(idiv).innerHTML = '';
	clearInterval(showtime);
	if(_bool){
		window.location.reload();
	}
		
}

function ClocsDivP(idiv)
{
	//alert(divCont);
	$(idiv).innerHTML = '';
	//$('_divCom').innerHTML = divCont;
}

function AutoCloseDiv(idiv, time, _bol)
{
	_bol = _bol || false;
	showtime = setInterval("CloseDiv('"+idiv+"', "+_bol+")", time);
}

// Make featured applicatio
function MakeAppComments(id, id1, idiv, _e)
{
	var divCont = $('_divCom').innerHTML;
	$(idiv).innerHTML = '';
	$('_divCom').innerHTML = 'Requesting...';
	var url = BaseUrl+'comments.php?apid='+id+'&cntid='+id1;
	var __X = CordX(_e);
	//var __Y = CordY(_e);
	new Ajax.Request(url, {
		method: 'get', 
		onSuccess: function(ajx){
			$(idiv).innerHTML = ajx.responseText;
			$(idiv).style.left = (__X-320)+'px';
			$('_divCom').innerHTML = divCont;
			//$(idiv).style.top = __Y+'px';
		}
	});
}


// Function to check comments
function MakeYourComments()
{
	var iName = encodeURIComponent($('iName').value);
	var iSubmit = encodeURIComponent($('iSubmit').value);
	var iComment = encodeURIComponent($('iComment').value);
	var iCountry = $('iCountry').value;
	var iAppsID = $('iAppID').value;
	var iSeoPnam = $('iSeoPnam').value;
	
	var _str = '';
	if(iName == ''){
		_str += 'Please enter your name.'+"\n";
	}
	if(iSubmit == ''){
		_str += 'Please enter subject.'+"\n";
	}
	if(iComment == ''){
		_str += 'Please enter your comments.';
	}
	if(_str.length > 0){
		alert(_str);
		return false;
	}
	else{
		$('msgdiv').innerHTML = '<b style="color:#F47A00">Please wait...</b>';
		$('submitdiv').style.disabled = true;
		$('closediv').style.disabled = true;
		var url = BaseUrl+'ajax.php?HSC=HSubmit&cAID='+iAppsID+'&cN='+iName+'&cS='+iSubmit+'&cC='+iComment+'&cCT='+iCountry;
		new Ajax.Request(url, {
			method: 'get', 
			onSuccess: function(ajx){
				if(ajx.responseText == 1){
					$('msgdiv').innerHTML = '<div class="success">Thank you. Someone will be in touch with you shortly.</div>';
					AutoCloseDiv('cmdivabs', 500, false);
					GetUserComments('UCommntDiv', iAppsID, iCountry, iSeoPnam);
				}
				else{
					$('msgdiv').innerHTML = '<div class="errors">Unable to post comments.</div>';
				}
			}
		});
	}
	return true;
}


function GetUserComments(did, iAppID, iCountry, pseo)
{
	var url = BaseUrl+'ajax.php?UCMNTS=GEtUserCoMMents&apId='+iAppID+'&cCT='+iCountry+'&pseo='+pseo;//&cAID='+iAppsID+'&cN='+iName+'&cS='+iSubmit+'&cC='+iComment+'&cCT='+iCountry;
	new Ajax.Request(url, {
		method: 'get', 
		onSuccess: function(ajx){
			$(did).innerHTML = ajx.responseText;
		}
	});
}











// Love it
function LoveIt(id, cntid){
	$('_hdhide1').style.display = 'none';
	$('_hdhide2').style.display = 'none';
	var url = BaseUrl+'ajax.php?LHIT=LoveIt&AID='+id+'&CT='+cntid;
	new Ajax.Request(url, {
		method: 'get', 
		onSuccess: function(ajx){
			$('lovediv').innerHTML = ajx.responseText+'&nbsp;&nbsp;';
			//$('_hdhide1').style.display = 'none';
			//$('_hdhide2').style.display = 'none';
			$('chclslhl').className = 'LoveHateHandUp1';
			$('chclslhh').className = 'LoveHateHandUp1';
		}
	});
}

// Hate it
function HateIt(id, cntid){
	$('_hdhide1').style.display = 'none';
	$('_hdhide2').style.display = 'none';
	var url = BaseUrl+'ajax.php?LHIT=HateIt&AID='+id+'&CT='+cntid;
	new Ajax.Request(url, {
		method: 'get', 
		onSuccess: function(ajx){
			$('hatediv').innerHTML = ajx.responseText;
			//$('_hdhide1').style.display = 'none';
			//$('_hdhide2').style.display = 'none';
			$('chclslhl').className = 'LoveHateHandUp1';
			$('chclslhh').className = 'LoveHateHandUp1';
		}
	});
}


// Subscribe for application
function Subscribe()
{
	return true;
}

// Share application
function Share()
{
	return true;
}

// Get Coordinate X
function CordX(_e)
{
	var _E = _e || window.event;
	var PgX = _E.layerX || _E.clientX || _E.offsetX || _E.pageX || _E.x;
	PgX = PgX + (document.body.scrollLeft || 0);
	return PgX;
}

// Get Coordinate Y
function CordY(_e)
{
	var _E = _e;// || window.event;
	var PgY = _E.layerY || _E.clientY || _E.offsetY || _E.pageY || _E.y;
	//alert(PgY);
	//alert(document.body.scrollTop);
	
	PgY = PgY + (document.body.scrollTop || 0);
	return PgY;
}



function changeUrlByCountry(id)
{
	var _urlB = location.protocol+'//'+location.host;
	var _str = _urlB+location.pathname.replace(/appstore\/([0-9]+)?/i, 'appstore/'+id);
	//alert(_str);
	window.location.href = _str;
}