var PageSizes = null;
var PageScrolls = null;
var signOut = function()
{
	new Ajax.Request(Url+'admin',{
		parameters:'SignOut=1',
		method:'post',
		onSuccess: function()
		{
			window.location.href = Url;
		}
	})
}

var showOverlay = function()
{
	PageSizes = getPageSize();
	$('overlay').style.height = (PageSizes[1] + 'px');	
	$('FlashHeader').hide();
	$('overlay').show();
	$('SecondContent').update(Loader);
	$('SecondContent').show()
}

var hideOverlay = function()
{
	$('overlay').hide();
	$('FlashHeader').show();	
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//take a news
var getNews = function(NewsId)
{
	try
	{
		if (!NewsId) NewsId = 0;
		showOverlay();
		//show loader
		//extract the info about the news with Ajax, and, populate the SecondContent div
		var objToEdit = 'GetNews';
		new Ajax.Request(Url+'?action=ajaxOperation&objectToEdit='+objToEdit, {
			method: 'post',
			parameters: 'NewsId='+NewsId,
			onSuccess: function(transport)
			{
				$('SecondContent').update(transport.responseText);
				$('SecondContent').show();
				Event.observe($('BtnSaveNews'), 'click', AN.saveNews);
				Event.observe($('SupportCheckbox'), 'change', function(){
					if ($('SupportCheckbox').checked)
					{
						$('Brands').options[0].text = 'Loading...';
						new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=getAllPhoneBrands', {
							method:'post',
							onSuccess: function(t)
							{
								try{
								if (t.responseText == null)
								{
									$('Brands').options[0].text = 'No records found';
									$('Brands').disable();
									$('Models').disable();					
									return;
								}
								var BrandsArray = t.responseText.evalJSON();
									if (!BrandsArray.length)
									{
										$('Brands').options[0].text = 'No records found';
										$('Brands').disable();
										$('Models').disable();
									}
									else
									{
										removeOptions($('Brands'));
										var opt = document.createElement('option');
										opt.value = 0;
										opt.text = 'Please select a brand';
										try
									    {
										    $('Brands').add(opt,null); // standards compliant
									    }
					        			catch(ex)
									    {
										    $('Brands').add(opt); // IE only
									    }
										for (var i=0; i<BrandsArray.length; i++)
										{
											var opt = document.createElement('option');
											opt.value = BrandsArray[i]['Id'];
											opt.text = BrandsArray[i]['Brand'];
											try
										    {
											    $('Brands').add(opt,null); // standards compliant
										    }
						        			catch(ex)
										    {
											    $('Brands').add(opt); // IE only
										    }
										    $('Brands').enable();
										    $('Brands').onchange = loadPhoneModels;
										}
									}
								}catch(Ex){alert(Ex.message)}
							}
						});
						
					}
				});
			}
		});
	}
	catch(Ex)
	{
		alert(Ex.message)
	}
}
var loadPhoneModels = function()
	{
		var BrandId = $F('Brands');
		if (BrandId == 0) return;
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=getPhoneModelsForBrand', {
			method:'post',
			parameters:'BrandId='+BrandId,
			onSuccess: function(t)
			{
				try
				{
					if (t.responseText == null)
					{
						$('Models').options[0].text = 'No records found';
						$('Models').disable();					
						return;
					}
					var ModelsArray = t.responseText.evalJSON();
					if (!ModelsArray.length)
					{
						$('Models').options[0].text = 'No records found';
						$('Models').disable();
					}
					else
					{
						removeOptions($('Models'));
						var opt = document.createElement('option');
						opt.value = 0;
						opt.text = 'Please select a model';
						try
					    {
						    $('Models').add(opt,null); // standards compliant
					    }
	        			catch(ex)
					    {
						    $('Models').add(opt); // IE only
					    }
						for (var i=0; i<ModelsArray.length; i++)
						{
							var opt = document.createElement('option');
							opt.value = ModelsArray[i]['Id'];
							opt.text = ModelsArray[i]['Model'];
							try
						    {
							    $('Models').add(opt,null); // standards compliant
						    }
		        			catch(ex)
						    {
							    $('Models').add(opt); // IE only
						    }
						}
					    $('Models').enable();
					}
				}
				catch(Ex)
				{
					alert(Ex.message)
				}
			}
		});		
	}
var closeSecondDialog = function()
{
	try
	{
		tinyMCE.execCommand('mceRemoveControl', false, 'NewsContent');
	}
	catch(Ex){}
	$('SecondContent').hide();
	$('SecondContent').update('');
	hideOverlay();

}

var loadNews = function()
{
	$('NewsPlacer').update('Loading...');
	new Ajax.Request(Url+'?action=ajaxOperation&objectToEdit=GetAllNews', {
		onSuccess: function(transport)
		{
			$('NewsPlacer').update(transport.responseText);
		}
	})
}

var newsletterSubscribe = function()
{
	try
	{
		$('NewsletterErrorPlacer').update('');
		$('NewsletterBtnSubscribe').value = 'Processing...';
		$('NewsletterBtnSubscribe').disable();
		if (!validEmail($F('NewsletterEmailAddress')))
		{
			$('NewsletterErrorPlacer').update('Please insert a valid E-Mail address');
			$('NewsletterBtnSubscribe').value = 'Subscribe';
			$('NewsletterBtnSubscribe').enable();
			return;
		}
		var EmailAddress = $F('NewsletterEmailAddress').strip();
		//try to insert the address in database
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=addNewsletterEmail', {
			method:'post',
			parameters: 'EmailAddress='+encodeURI(EmailAddress),
			onSuccess: function(transport)
			{
				var Error = '';
				if (isNaN(transport.responseText))
				{
					Error = transport.responseText;
				}
				else
				{
					Error = '<font color="green">Succesfully subscribed</font>';
				}
				$('NewsletterErrorPlacer').update(Error)
				$('NewsletterBtnSubscribe').value = 'Subscribe';
				$('NewsletterBtnSubscribe').enable();
			},
			onFailure: function(){alert('something went wrong')}
		})
	}
	catch(Ex)
	{
		alert(Ex.message)
	}
}

//save the e-mail address for an phone type support
var saveEAforPHoneSupport = function()
{
	if ($F('Models') < 1) 
	{
		$('PhoneSupportErrorPlacer').update('Please select a phone model');
		return;
	}
	else if ($F('PhoneSupportEmailAddress').blank())
	{
		$('PhoneSupportErrorPlacer').update('Please insert a e-mail address');
		return;		
	}
	else
	{
		$('PhoneSupportErrorPlacer').update(Loader);
		$('BtnSavePhoneForSupport').disable();
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=saveEAforPhoneSupport', {
			method:'post',
			parameters:'PhoneId='+$F('Models')+'&EmailAddress='+escape($F('PhoneSupportEmailAddress')),
			onSuccess: function(t)
			{
				if (t.responseText == 'success')
				{
					$('PhoneContainer').update('<font color=green>Address succesfully added</font>');
					return;
				}
				else
				{
					$('PhoneSupportErrorPlacer').update(t.responseText)
				}
				$('BtnSavePhoneForSupport').enable();
			}
		});
		
	}
}
var removeOptions= function(OptionObject)
	{
		try
		{
			for (var i=OptionObject.length; i>-1; i--)
			{
				if (OptionObject.options[i])
				{
					OptionObject.remove(i)
				}
			}
		}
		catch(Ex)
		{
			alert(Ex.message+' on removeOptions function')		
		}
	}
	
var reloadBanner = function(BannerName)
{
	try
	{
	}
	catch(x)
	{
		alert(Ex.message);
	}
}
Event.observe(window, 'load', function(){
	var PageSizes = getPageSize();
	//positioning the second content div
	$('SecondContent').style.left = parseInt(PageSizes[0]) / 2 - parseInt($('SecondContent').style.width) / 2 + 'px';
	
	//put action on the newsletter subscription button
	if ($('NewsletterBtnSubscribe'))
	Event.observe($('NewsletterBtnSubscribe'), 'click', newsletterSubscribe);
	if ($('PageHasLeftBoxes') && ($F('PageHasLeftBoxes') == 1))
	{
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=getRightBoxes', {
			method:'post',
			parameters:'CategoryId='+$F('CategoryId'),
			onSuccess: function(t)
			{
				$('right').update(t.responseText);
			}
		});
	}
})