﻿var al;
var yf;
function onLoad()
{
	al = new alerts();
	yf = new yourfeeds();
}

function alerts()
{
	var value = -1;
	var alertlist = new Array();
	var list = getobj("alertsinit").value.split(",");
	for( var i = 0, n = list.length - 3; i < n; i += 4 )
	{
		alertlist.push( new Array( list[i], list[i+1], list[i+2], list[i+3] ) );
	}
	update();
	
	this.save = function()
	{
		if( !logined() )
		{
			window.location = "/login.aspx";
			return;
		}

		ajaxrequest( "optionsajax.aspx?cmd=savealerts&alerts=" + alertlist.toString(), doContents);
		function doContents( str )
		{
			alert( "Saved successfully" );
			window.location = "/";
		}
	}
	
	this.addalert = function()
	{
		if( !veri() )
			return;

		var pair = getobj("Select2").value;
		alertlist.push( new Array( pair, getobj("Select3").value, getobj("Text1").value, getobj("Select5").value ) );
		update();
		this.setValue(alertlist.length-1);
	}
	
	this.updaterow = function()
	{
		if( value < 0 )
			return;

		var pair = getobj("Select2").value;
		alertlist.splice( value, 1, new Array( pair, getobj("Select3").value, getobj("Text1").value, getobj("Select5").value ) );
		update();
		this.setValue(value);
	}
	
	this.deleterow = function()
	{
		if( value < 0 )
			return;

		alertlist.splice( value, 1 );
		update();
		setnull();
	}
	
	function update()
	{
		var htmlstr = "";
		for( var i = 0, n = alertlist.length; i < n; i++ )
			htmlstr += "<div onclick='al.setValue(" + i + ")'>" +
				"<div>" + alertlist[i][0] + "</div>" +
				"<div>" + alertlist[i][1] + "</div>" + 
				"<div>" + priceformat(alertlist[i][0], alertlist[i][2]) + "</div>" + 
				"<div>" + alertlist[i][3] + "</div>" +
				"</div>";
		getobj("aler").innerHTML = htmlstr;
	}
	
	function setnull()
	{
		value = -1;
		document.getElementById( "AlertDelete" ).disabled = true;
		document.getElementById( "AlertUpdate" ).disabled = true;
	}
	
	this.setValue = function( num )
	{
		var items = getobj("aler").getElementsByTagName("div");
		if( value >= 0 )
		{
			items[value*5].className = items[value*5].className.replace(new RegExp(" dd", "g"), "");
			value = -1;
			document.getElementById( "AlertDelete" ).disabled = true;
			document.getElementById( "AlertUpdate" ).disabled = true;
		}
			
		if( num >= 0 )
		{
			items[num*5].className += " dd";
			value = num;
			document.getElementById( "Select2" ).value = items[num*5+1].innerText;
			document.getElementById( "Select3" ).value = items[num*5+2].innerText;
			document.getElementById( "Text1" ).value = items[num*5+3].innerText;
			document.getElementById( "Select5" ).value = items[num*5+4].innerText;
			document.getElementById( "AlertDelete" ).disabled = false;
			document.getElementById( "AlertUpdate" ).disabled = false;
		}
	}
	
	function veri()
	{
		var ve = new verify();
		try
		{
			ve.message = "Please enter the alert value."
			ve.isfloat( "Text1" );
			return true;
		}
		catch( err )
		{
			ve.obj.focus();
			alert( err.message );
		}
		return false;
	}
}

function soundchanged()
{
	document.getElementById( "sound" + document.getElementById('Select5').value ).play();
}

function changefeed( id )
{
	getobj( "idfeeds" ).innerHTML = "<br /><br />Loading ...";
	ajaxrequest( "optionsajax.aspx?cmd=changefeed&id=" + id, doContents);
	function doContents( str )
	{
		getobj( "idfeeds" ).innerHTML = str;
	}
}

function yourfeeds()
{
	var feedlist = new Array();
	var value = -1;
	var list = getobj("feedsinit").value.split(",");
	for( var i = 0, n = list.length - 2; i < n; i += 3 )
	{
		feedlist.push( new Array( list[i], list[i+1], list[i+2] ) );
	}
	update();

	this.insertfeed = function(id, source, feed )
	{
		var i = 0, n = feedlist.length;
		for( ; (i < n) && (id != feedlist[i][0]); i++ );
		if( i == n )
		{			
			feedlist.unshift( new Array( id, source, feed ) );
			update( 0 );
			this.setValue( 0 );
		}
		else{
			this.setValue( i );
		}
	}
	
	this.save = function()
	{
		if( !logined() )
		{
			window.location = "/login.aspx";
			return;
		}

		ajaxrequest( "optionsajax.aspx?cmd=savefeed&feeds=" + feedlist.toString(), doContents);
		function doContents( str )
		{
			alert( "Saved successfully" );
			window.location = "/news/news.aspx";
		}
	}
	
	function update( )
	{
		var html1 = "";
		for( var i = 0, n = feedlist.length; i <  n; i++ )
			html1 += "<div class='cc' onclick='yf.setValue(" + i + ")'><div class='aa'>" + feedlist[i][1] + "</div><div class='bb'>" + feedlist[i][2] + "</div></div>";
		getobj( "yourfeeds" ).innerHTML = html1;
		
	}
	
	this.setValue = function( num )
	{
		var items = getobj("yourfeeds").getElementsByTagName("div");
		if( value >= 0 )
		{
			items[value*3].className = items[value*3].className.replace(new RegExp(" dd", "g"), "");
			value = -1;
		}
			
		if( num >= 0 )
		{
			items[num*3].className += " dd";
			value = num;
		}
	}
	
	this.move = function( pos )
	{
		if( value < 0 )
			return;

		var num = pos;
		switch( pos )
		{
		case -1:
			num = value - 1;
			break;
		case -2:
			num = value + 1;
			break;
		case -3:
			num = feedlist.length - 1;
			break;
		}
		if( num < 0 )
			num = 0;
		if( num >= feedlist.length )
			num = feedlist.length - 1;
			
		var temparray = feedlist.splice( value, 1 );
		feedlist.splice( num, 0, temparray[0] );
		update();
		this.setValue( num );
	}
	
	this.remove = function()
	{
		if( value < 0 )
			return;

		feedlist.splice( value, 1 );
		update();
		value = -1;
	}
	
	this.removeall = function()
	{
		if( feedlist.length == 0 )
			return;

		var aa = confirm("Are you sure you want to remove all?");
		if( aa )
		{
			feedlist.splice( 0, feedlist.length );
			update();
			value = -1;
		}
	}
}

