// JavaScript Document
function gotoSellLinkStep(stepNum)
{
	var stepNum;
	if(stepNum == 1)
	{
		$('Step1').show();
		$('Step2').show();
		$('Step3').hide();
	}
	if(stepNum == 2)
	{
		$('Step1').hide();
		$('Step2').hide();
		$('Step3').show();
		$('processingstatus').innerHTML = "Validating your Link..";
		setTimeout("SubmitLinkListing('"+USERID+"',"+linkid+")",1);
	}
}
function SubmitLinkListing(USERID,linkid)
{
	var catid, webaddr, title, linktype, desc, emailid, usrname, pwd, params;
	catId = $('searchcat').value;
	title = $('txttitle').value;
	webaddr = urlencode($('txturl').value);
	linktype = $('listingtype').value;
	desc = $('txtdescription').value;
	emailid = $('txtemailid').value;
	$('divtitle').innerHTML = title;
	$('divurlgrey').innerHTML = $('txturl').value;
	$('divdescgrey').innerHTML = desc.substring(0,200) + " ... ";
	desc = escape(encode(desc));
	title = escape(encode(title));
	if (USERID == "") {
		usrname = $('txtuserid').value;
		pwd = $('txtpwd').value;
	}
	else
		usrname = USERID;
	params = "linkid="+linkid+"&cid="+catId+"&webaddr="+webaddr+"&t="+title+"&ltype="+linktype+"&d="+desc+"&e="+emailid+"&u="+usrname+"&pw="+pwd;
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'from=CHECKLINKLISTING&' + params,
		onSuccess: function(transport) {
			if (transport.responseText.indexOf("Error:") != -1) {
				gotoSellLinkStep(1);
				$('errorStep2').innerHTML = transport.responseText;
				$('errorStep2').show();
			}
			else {
				newurl = transport.responseText.split("|")[0];
				$('txturl').value = newurl;
				$('divurlgrey').innerHTML = newurl;
				$('processingstatus').innerHTML = transport.responseText.split("|")[1];
				$('errorStep2').hide();
				SaveLinkListing(params);
			}
		}
	});
}
function SaveLinkListing(params)
{
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'from=SAVELINKLISTING&' + params,
		onSuccess: function(transport) {
			if (transport.responseText.indexOf("Error:") != -1) {
				gotoSellLinkStep(1);
				$('errorStep2').innerHTML = transport.responseText;
				$('errorStep2').show();
			}
			else {
				linkid = transport.responseText;
				$('imgProcessing').hide();
				$('listingfunctions').show();
				$('processingstatus').innerHTML = "Your Link has been saved. It will be ONLINE for users to view only after you click on Submit.";
				$('onoffline').show();
			}
		}
	});
	$('onoffimgid').src = 'http://www.heyev.com/images/offline.gif';
	$('divtitle').className = 'offlinetitle';
	$('submitlistingid').show();
}
function DeleteLink(lid,Id)
{
	linkid = lid || linkid;
	id = Id || '';
	if(confirm("Are you sure you want to delete this Link?"))
	{
		$('imgProcessing' + id).show();
		$('listingfunctions' + id).hide();
		$('processingstatus' + id).innerHTML = "Deleting this Link from the system..";
		new Ajax.Request(url, {
			method: 'post',
			parameters: 'from=DELETELINK&linkid=' + linkid,
			onSuccess: function(transport) {
				if (transport.responseText.indexOf("Error:") != -1) {
					$('imgProcessing' + id).hide();
					$('listingfunctions' + id).show();
					$('processingstatus' + id).innerHTML = "Link could not be deleted.";
				}
				else {
					$('deldiv' + id).hide();
					$('spacer' + id).show();
					ShowSuccessTab(3, "Your Link has been Deleted");
				}
			}
		});
	}	
}
function MakeLinkOnline(lid,Id)
{
	linkid = lid || linkid;
	id = Id || '';
	$('imgProcessing' + id).show();
	$('listingfunctions' + id).hide();
	$('processingstatus' + id).innerHTML = "Submitting your Link..";
	$('processingstatus' + id).show();
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'from=SUBMITLINK&linkid=' + linkid,
		onSuccess: function(transport) {
			if (transport.responseText.indexOf("Error:") != -1) {
				$('processingstatus' + id).innerHTML = transport.responseText;
				$('imgProcessing' + id).hide();
				$('listingfunctions' + id).show();
			}
			else {
				$('processingstatus' + id).innerHTML = transport.responseText;
				SendLinkEmailConfirmation(linkid,id);
				$('onoffimgid' + id).src = 'http://www.heyev.com/images/online.gif';
				$('divtitle' + id).className = 'onlinetitle';
				$('submitlistingid' + id).hide();
				if($('submitteddt' + id)) {
					var today = new Date();
					$('submitteddt' + id).innerHTML = formatDate(today);
				}
			}			
		}
	});
}
function SendLinkEmailConfirmation(lid,Id)
{
	linkid = lid || linkid;
	id = Id || '';
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'from=SENDLINKEMAILCONFIRMATION&linkid=' + linkid,
		onSuccess: function(transport) {
			$('imgProcessing' + id).hide();
			$('listingfunctions' + id).show();
			$('processingstatus' + id).innerHTML = transport.responseText;
			$('onoffline' + id).show();
		}
	});
}
function ShowPreviewLink(lid,Id)
{
	setTimeout('showdialogboximg()',0);
	linkid = lid || linkid;
	id = Id || '';
	var wid = document.viewport.getWidth() - 100;
	var hit = document.viewport.getHeight() - 100;
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'from=GETLINKPREVIEWHTML&linkid=' + linkid,
		onSuccess: function(transport) {
			if (transport.responseText.indexOf("Error:") == 0) {
				$('errorPreview').innerHTML = transport.responseText;
				$('errorPreview').show();
				setTimeout("$('DlgBoxImg').hide()",1);
				setTimeout("$('errorPreview').hide()",5000);
			}
			else {
				var dbox = new al_DialogBox(wid,hit,transport.responseText);
				dbox.ShowDialog();
			}
		}
	});
}
function showdialogboximg()
{
	$('DlgBoxImg').show();
	scrollFix();
}
function getMyLinks(orderby)
{
	orderby = orderby || '';
	ShowProcessingTab(-1,"Retrieving your listings...");
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'from=GETMYLINKSHTML&orderby='+orderby,
		onSuccess: function(transport) {
			if (transport.responseText.indexOf("Error:") != -1) {
			}
			else {
				$('mylistingsdiv').innerHTML = transport.responseText;
			}
			HideProcessingTab(-1);
		}
	});
}
function ShowPreviewForMyLinks(lid,Id)
{
	setTimeout('showdialogboximg()',0);
	linkid = lid || linkid;
	id = Id || '';
	var wid = document.viewport.getWidth() - 100;
	var hit = document.viewport.getHeight() - 100;
	new Ajax.Request(url, {
		method: 'post',
		parameters: 'from=GETLINKPREVIEWHTML&linkid=' + linkid,
		onSuccess: function(transport) {
			if (transport.responseText.indexOf("Error:") == 0) {
				$('errorPreview').innerHTML = transport.responseText;
				$('errorPreview').show();
				setTimeout("$('DlgBoxImg').hide()",1);
				setTimeout("$('errorPreview').hide()",5000);
			}
			else {
				var dbox = new al_DialogBox(wid,hit,transport.responseText);
				dbox.ShowDialog();
			}
		}
	});
}
function DeleteLinksFromMyLinks(lid,Id)
{
	linkid = lid || linkid;
	id = Id || '';
	if(confirm("Are you sure you want to delete this Link?"))
	{
		$('imgProcessing' + id).show();
		$('listingfunctions' + id).hide();
		$('processingstatus' + id).innerHTML = "Deleting this Link from the system..";
		new Ajax.Request(url, {
			method: 'post',
			parameters: 'from=DELETELINK&linkid=' + linkid,
			onSuccess: function(transport) {
				if (transport.responseText.indexOf("Error:") != -1) {
					$('imgProcessing' + id).hide();
					$('listingfunctions' + id).show();
					$('processingstatus' + id).innerHTML = "Link could not be deleted.";
				}
				else {
					ToggleVert('userlistingsbox' + id, 0, $('userlistingsbox' + id).getHeight());
					ShowSuccessTab(id, "Your Link has been Deleted");
				}
			}
		});
	}	
}
function EditForMyLinks(lid)
{
	var lid;
	location.href = '/internet/editlink/'+lid+'/';
}