
var hdlnObj = {
	
	contentBuffer: new ContentBuffer(),
	
	init: function(){	
		//addEvent($('submitform'), "click", hdlnObj.submitForm);
	},
	cbCallback: function(cb){
		if(cb.getResult() == 0){
			document.location = "confirm.asp?email="+escape($('email').value);
			//$('errorbox').innerHTML = "Registration successful. A confirmation email will be sent to you shortly."
			//$('errorbox').style.color = "#009F07";
		} else if (cb.getResult() == 2)  {
			$('errorbox').innerHTML = "ERROR: The URL you are trying to register has already been registered."
			$('errorbox').style.color = "#B60C36";
		} else {
			$('errorbox').innerHTML = "Registration failed.";
			$('errorbox').style.color = "#B60C36";
		}
	},
	submitForm: function(site){
		//reset state
		$('errorbox').innerHTML = "";
		$('agreelabel').style.backgroundColor = "#FFFFFF";
		$('firstname').style.backgroundColor = "#FFFFFF";
		$('lastname').style.backgroundColor = "#FFFFFF";
		$('email').style.backgroundColor = "#FFFFFF";
		//$('country').style.backgroundColor = "#FFFFFF";
		$('url').style.backgroundColor = "#FFFFFF";
		var errs = document.getElementsByClassName('error_icon');
		for(var x = 0; x < errs.length; x++){
			errs[x].style.display = 'none';
		}
		//validate input data
		var errors = [];
		var FirstName = $('firstname').value;
		if (FirstName == ''){
			errors.push('firstname');
		}
		var LastName = $('lastname').value;
		if (LastName == ''){
			errors.push('lastname');
		}
		var EmailAddress = $('email').value;
		if (EmailAddress == ''  || !hdlnObj.isValidEmail(EmailAddress)){
			errors.push('email');
		}
		var Country = $('country').options[$('country').selectedIndex].value; 
		if (Country == ''){
			errors.push('country');
		}
		var WebsiteURL = $('url').value;
		if (WebsiteURL == '' || !hdlnObj.isUrl(WebsiteURL)){
			errors.push('url');
		}
		var agree = $('agree').value;
		if ($('agree').checked == false){
			errors.push('agree');
		}
		
		if(errors.length == 0){
			var url= "../asplib/addRegistrant.asp?FirstName="+escape(FirstName)+"&LastName="+escape(LastName)+"&EmailAddress="+escape(EmailAddress)+"&Country="+escape(Country)+"&WebsiteURL="+escape(WebsiteURL)+"&site="+site;
			hdlnObj.contentBuffer.load({
			  url: url,
			  onload: hdlnObj.cbCallback,
			  debug: true,
			  contentType: "text/html"
			})/**/
		} else {
			$('errorbox').style.color = "#B60C36";
			if (site != 'jp'){
				$('errorbox').innerHTML = "Please review form for errors.";
			}
			for(var i =0; i< errors.length; i++){

				if(errors[i] == 'agree'){
					$('agreelabel').style.backgroundColor = "#FFEFF1";
					$(errors[i]).nextSibling.nextSibling.style.display = 'inline';
				} else if(errors[i] == 'email'){
					$(errors[i]).style.backgroundColor = "#FFEFF1";
					if($(errors[i]).nextSibling.nodeName.toLowerCase() == "img"){
						$(errors[i]).nextSibling.style.display = 'inline';
					}
					if (site == 'jp'){
						$('errorbox').innerHTML += "&#12513;&#12540;&#12523;&#12450;&#12489;&#12524;&#12473;&#12434;&#27491;&#12375;&#12356;&#24418;&#24335;&#12391;&#12372;&#20837;&#21147;&#12367;&#12384;&#12373;&#12356;&#65288;&#20363;:&#12288;user@domain.com&#65289;";
					} else {
						$('errorbox').innerHTML += "<br />Please enter your email address in a valid format (user@domain)";
					}
				} else if(errors[i] == 'url'){
					$(errors[i]).style.backgroundColor = "#FFEFF1";
					if($(errors[i]).nextSibling.nodeName.toLowerCase() == "img"){
						$(errors[i]).nextSibling.style.display = 'inline';
					}
					if (site == 'jp'){
						$('errorbox').innerHTML += "<br />URL&#12434;&#27491;&#12375;&#12356;&#24418;&#24335;&#12391;&#12372;&#20837;&#21147;&#12367;&#12384;&#12373;&#12356;&#65288;&#20363;:&#12288;www.yourdomain.com&#65289;";
					} else {
						$('errorbox').innerHTML += "<br />Please enter a valid URL format (ex. www.YOUR_DOMAIN.com)";
					}
				}else {
					$(errors[i]).style.backgroundColor = "#FFEFF1";
					if($(errors[i]).nextSibling.nodeName.toLowerCase() == "img"){
						$(errors[i]).nextSibling.style.display = 'inline';
					}
				}
			}
		}
		return false;
	},
	isUrl: function (s) {
		var regexp = /(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
		//(ftp|http|https):\/\/
		return regexp.test(s);
	},
	isValidEmail: function (x){;
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		return filter.test(x);
	},
	PopupWin: function (url,title,w,h,scroll,resize) {
		if ( arguments.length < 6 )
			resize = "yes"

		newWin=window.open(url,title,"scrollbars=" +scroll+ ",width=" +w+ ",height=" +h+ ",resizable=" + resize);
 		if (newWin && newWin.open && !newWin.close && url.indexOf("http") == -1)
  			newWin.moveTo(200,25)
 		newWin.focus();
	}

	
}
addEvent(window, "load", hdlnObj.init);

function commonPopup(url, width, height, toolsInd, wname){
	var options = "width=" + width + ",height=" + height + ",top=" + ((screen.height - height) / 4).toString() + ",left=" + ((screen.width - width) / 2).toString();

	switch (toolsInd){
		case 1:
			options += ",toolbar=no,status=no,resizable=no,scrollbars=yes";
			break;
		case 2:
			options += ",menubar=yes,toolbar=yes,status=yes,resizable=yes,location=yes,scrollbars=yes";
			break;
		default:
			//do nothing
			break;
	}

	if (!wname){
		wname = "reutersPopup";
	}
	popupWindow = window.open(url, wname, options);
	if (popupWindow){
		popupWindow.focus();
	}
}