/*
NB - this requires FormHelper.js from _hbi and HBI.js from _hbi and the prototype library
*/
SendToAFriend = function(){}
SendToAFriend.sHashCode = "";

SendToAFriend.getForm = function(sDivName, sURL, sHashCode, sMagazineFolder, nNewsID){
	SendToAFriend.sHashCode = sHashCode;
	var pars = 'sMagazineFolder=' + sMagazineFolder + '&nNewsID=' + nNewsID;
	var SendToAFriendUpdater = new Ajax.Updater(
		sDivName,
		sURL,
		{
			method: 'get',
			parameters: pars,
			//onLoading: SendToAFriend.updateLoading,
			onComplete: SendToAFriend.updateSeachForm
		});
}

SendToAFriend.sendMail = function(sDivName, sURL){
	var sUserName = $F('sUserName');
	var sUserEmail = $F('sUserEmail');
	var sFriendName = $F('sFriendName');
	var sFriendEmail = $F('sFriendEmail');
	var sURLToArticle = $F('sURLToArticle');
	var nNewsID = $F('nNewsID');
	var sMagazineFolder = $F('sMagazineFolder');
	
	//do some validation,
	var isValid = true;
	if(!FormHelper.hasValue(sUserName, true)){	
		$('sErrorUserName').innerHTML = 'enter your name';
		isValid = false;
	}else{		
		$('sErrorUserName').innerHTML = '';		
	}
	
	if(!FormHelper.hasValue(sFriendName, true)){			
		$('sErrorFriendName').innerHTML = 'enter your friends name';
		isValid = false;
	}else{	
		$('sErrorFriendName').innerHTML = '';		
	}
		
	if(!HBI.emailCheck(sUserEmail)){			
		$('sErrorUserMail').innerHTML = 'enter a valid email address';
		isValid = false;
	}else{		
		$('sErrorUserMail').innerHTML = '';		
	}
		
	if(!HBI.emailCheck(sFriendEmail)){		
		$('sErrorFriendMail').innerHTML = 'enter a valid email address';
		isValid = false;
	}else{		
		$('sErrorFriendMail').innerHTML = '';		
	}
	
	if(isValid){
		var pars = 'sUserName=' + sUserName + '&sUserEmail=' + sUserEmail + '&sFriendName=' + sFriendName + '&sFriendEmail=' + sFriendEmail + '&sHashCode=' + SendToAFriend.sHashCode + '&sURLToArticle=' + sURLToArticle + '&sMagazineFolder=' + sMagazineFolder + '&nNewsID=' + nNewsID;
		var SendToAFriendUpdater = new Ajax.Updater(
			sDivName,
			sURL,
			{
				method: 'get',
				parameters: pars
				//onLoading: SendToAFriend.updateSending
			});	
	}
}

SendToAFriend.cancel = function(sDivName, sURL){
	var SendToAFriendUpdater = new Ajax.Updater(
		sDivName,
		sURL,
		{
			method: 'get',
			parameters: ''
			//onLoading: SendToAFriend.updateLoading
		});
}

SendToAFriend.updateLoading = function(request){
	SendToAFriend.setStatus('Loading...');
}

SendToAFriend.updateSending = function(request){
	SendToAFriend.setStatus('Sending...');	
}

SendToAFriend.setStatus = function(sStatus){
	$('sendToAFriend').innerHTML = sStatus;
}

SendToAFriend.updateSeachForm = function(request){
	//after the form is there, populate the hidden field sURLToArticle
	var sDocumentLocation		= document.location.toString();
	
	// check if sHashCode already defined in URL string - remove it if it is
	if (sDocumentLocation.indexOf('sHashCode')) {
		var codeRegEx = new RegExp("sHashCode.*");
		sDocumentLocation = sDocumentLocation.replace(codeRegEx, "");
	}
	if (sDocumentLocation.charAt(sDocumentLocation.length-1) == '#') {
		sDocumentLocation		= sDocumentLocation.slice(0,-1);
	}
	
	// all variables are in query string - add hash code as part of query string
	if (sDocumentLocation.indexOf('&') >= 0) {
		if (sDocumentLocation.charAt(sDocumentLocation.length-1) != '&') {
			sDocumentLocation		= sDocumentLocation + "&";
		}
		sDocumentLocation			= sDocumentLocation + "sHashCode=" + SendToAFriend.sHashCode;
		
		var regEx = new RegExp(/\&/g);
		sDocumentLocation = sDocumentLocation.replace(regEx, "%26");
		
	// all variables are in folder format - add hasch code as folder	
	} else {
		if (sDocumentLocation.charAt(sDocumentLocation.length-1) != '/') {
			sDocumentLocation		= sDocumentLocation + "/";
		}
		sDocumentLocation			= sDocumentLocation + SendToAFriend.sHashCode + "/";
	}
	
	$('sURLToArticle').value		= sDocumentLocation;
}