if (top.location != self.location) {top.location = self.location.href}

function setCheckboxes(xelts,act)
{
	//elts = document.forms[xform].elements[xelts + '[]'];
	elts = document.getElementsByName(xelts + '[]');
	var elts_cnt  = (typeof(elts.length) != 'undefined') ? elts.length : 0;
	if (elts_cnt)
	{
		for (var i = 0; i < elts_cnt; i++)
		{
			elts[i].checked = (act == 1 || act == 0) ? act : (elts[i].checked ? 0 : 1);
		}
	}
}

function checkscript(thefields)
{
	var boxesTicked = "";
	
	for (i = document.getElementsByName(thefields).length - 1; i >= 0; i--)
	{
		if (document.getElementsByName(thefields)[i].checked)
		{
			boxesTicked = boxesTicked + document.getElementsByName(thefields)[i].value + "\n";
		}
	}
	
	if (boxesTicked == "")
	{
		alert ("You must first select one or more items!");
		return false;
	}
	else
	{
		return confirm ("Are you sure you want to continue?");
	}
}

function generateMULinks()
{
	var re = new RegExp(document.linksform.regex.value, "g");
	var replacement = document.linksform.replacement.value;
	var links = document.linksform.targetlinks.value;
	var newlinks = document.linksform.targetlinks;
	newlinks.value = links.replace(re, replacement);
}

$(function()
{
	$('div#error').click(function()
	{
		$(this).fadeOut();
	});
	
	$('#accountinfo').click
	(
		function()
		{
			//Set_Cookie('acinfo', 'off', 30, '/');
			$(this).fadeOut();
			$('#accinfobutton').fadeIn();
		}
	);

	$('#accinfobutton').click
	(
		function()
		{
			//Delete_Cookie('acinfo', '/');
			$(this).fadeOut();
			$('#accountinfo').fadeIn();
		}
	);
});

function showError(message)
{
	$('div#error').html(message);
	$('div#error').fadeIn();
	$('#linksformsubmit').removeAttr('disabled');
	$('#linksformsubmit').attr("value", "Generate");
	return;
}

function generateLinks()
{
	$('div#error').fadeOut();

	var inputlinks = document.linksform.links;
	if (inputlinks.value == inputlinks.defaultValue || inputlinks.value == '')
	{
		alert('You didn\'t enter any links!\nEnter either a single valid link, or several links (they can be mixed) for any of the following hosts:\n\nRapidshare\nMegaupload\nMegavideo\nHotfile\nFilefactory\nDepositfiles\nUploading\nEasy-Share\n\n(available account(s) for respective site assumed)');
		return false;
	}
	var regexN = new RegExp("\n","g");
	var arrayLinks = inputlinks.value.split(regexN);
	var maxlinks = 20;
	if (arrayLinks.length > maxlinks)
	{
		alert('Please convert maximum of ' +maxlinks+ ' links at once, thanks!');
		return false;
	}

	$.ajax(
	{
		type: "POST",
		url: '/ajax.php?sub=linkgen',
		//dataType: 'html',
		data: ({
			links: $('#links').val(),
			prv: $('#prv').attr('checked')
		}),
		beforeSend: function()
		{
			$('#loader').fadeIn();
			$('#linksformsubmit').attr("disabled", "true");
			$('#linksformsubmit').attr("value", "Please Wait...");
		},
		error: function()
		{
			$('#linksformsubmit').attr("value", "Failed to Submit");
			$('#linksformsubmit').removeAttr('disabled');
			return;
		},
		success: function(data)
		{
			$.fn.colorbox({title: 'Your Generated Links', open:true, html:data});
		},
		complete: function()
		{
			$('#loader').fadeOut();
			$('#linksformsubmit').removeAttr('disabled');
			$('#linksformsubmit').attr("value", "Generate");
		}
	});
}

function getPage(page)
{
	$.ajax(
	{
		type: "GET",
		url: page,
		beforeSend: function()
		{
			$('#loader').fadeIn(500);
		},
		success: function(msg)
		{
			$('#freeinfo').hide();
			$('#content').hide();
			$('#content').html(msg).fadeIn('slow');
		},
		complete: function()
		{
			$('#loader').fadeOut(1000);
		}
	});
}

function setcookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function getcookie( check_name )
{
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );

		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

// this deletes the cookie when called
function delcookie( name, path, domain )
{
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function makeClickable(divid)
{
	var thediv = '#' + divid;
	var contents = $(thediv).val();
	var linksArray = contents.split("\n");
	var newLinks = '';
	for (i=0; i<linksArray.length; i++)
	{
		newLinks += '<a href="' + linksArray[i] + '">' + linksArray[i] + '</a>' + '<br />';
	}
	$(thediv, '.linkcontainer').fadeOut('slow', function(){
		$('.linkcontainer').html(newLinks);
		$('.linkcontainer').fadeIn('slow');
	});	
}