// JavaScript Document
var me //object reference

function friendSend(strType, strTitle){
	
	this.strType 	= strType; 				//type of thing we're sending to a friend (press release/feature etc)
	this.strURL		= escape(location.href);		//URL of thing
	this.strTitle	= (strTitle.escapeHTML()).replace("?", "");				//title of thing (used on email)

	this.hostName 			= location.hostname;														//host name of site (so we don't have to arse around making dev and test versions of the code.	
	this.strAspInterface	= 'http://'+this.hostName+'/code/classes/ajax/reader/friendsend.asp';		//url of asp client interface which is called by the ajax functions

	this.parameters 		= new Object();																			//object containing any parameters to be passed to the server side script via ajax.

	
}

friendSend.prototype.submitForm = function(){

	this.parameters["fldFriendsEmail"]	= $("fldFriendsEmail").value;
	this.parameters["fldEmail"]			= $("fldEmail").value;
	this.parameters["fldComment"]		= $("fldComment").value;
	this.parameters["fldType"]			= this.strType;
	this.parameters["fldURL"]			= this.strURL;
	this.parameters["fldTitle"]			= this.strTitle;
	this.parameters["Verify"]			= $("Verify").value;
	
	var pars = this.buildParameterString();

	me = this;

	//communicate with webserver via ajax
	var myAjax = new Ajax.Request(
		this.strAspInterface,
		{ 	method: 'post',
			parameters: pars,
			contentType: 'application/x-www-form-urlencoded',				
			onComplete: this.parseFormErrors
		})	

	
}


friendSend.prototype.parseFormErrors = function(objResponse){
	var strErrors;
	var aErrors;
	
	strErrors = objResponse.responseText;
			
	aErrors = strErrors.split("|");
		
	$("error_fldFriendsEmail").innerHTML 	= aErrors[0];
	$("error_fldEmail").innerHTML 			= aErrors[1];
	$("error_fldComment").innerHTML			= aErrors[2];	
	$("error_Verify").innerHTML				= aErrors[3];
	
	if (strErrors.length == 3){
		Modalbox.hide();
//		$("quickLogin_content").innerHTML = "<h1>Thank you</h1><p><a href='certificate.asp' onclick='javascript:Windows.closeAll();' target='_blank'>Please click here to produce your certificate</a></p>";
//		Windows.closeAll();
//		window.open("certificate.asp");
	} else {
		me.reloadCaptcha();	
	}
	
}

//refresh the captcha image
friendSend.prototype.reloadCaptcha = function(){
	
	$('verifyImage').hide();
	$('verifyImage').appear({duration:1.5});
	var img = document.createElement('img');
    img.onload = function (evt) {
        $('verifyImage').src=this.src;
        $('verifyImage').width=this.width;
        $('verifyImage').height=this.height;
    }
	img.src = '/_private/aspinc/verifyImage.asp?'+Math.random();

	
	
}


friendSend.prototype.showForm = function(){
	
	this.reloadCaptcha();
	Modalbox.show($('frmSend'), {title: "Send this " + this.strType + " to a friend", width: 400});	
	
}

//build the parameter string to be passed into the ajax call from the parameters object
friendSend.prototype.buildParameterString = function(){
	
	var strPars = '';
	
	for (var i in this.parameters){
		strPars = strPars + i + "=" + this.parameters[i] + "&";
	}
	
	return strPars;
	
}

