﻿var _ddlReferredBy;
var _invitedBy;
var _referredBy;
var _voucherID;
var _countryID;
var _path;

$(document).ready(function () {

	$.each($(":text, :password"), function () {
		$(this).watermark({ "placeholder": $(this).attr("placeholder1") });
	});

	$(":text, :password, select").change(function () {
		Validate($(this));
	});
});

function DataBindRegistration(ddlReferredBy, invitedBy, referredBy, voucherID, countryID, path) {
	_ddlReferredBy = ddlReferredBy;
	_invitedBy = invitedBy;
	_referredBy = referredBy;
	_voucherID = voucherID;
	_countryID = countryID;
	_path = path;
}

function Submit() {
	var isValid = true;
	$.each($(":text, :password, select"), function () {
		if (!Validate($(this)))
			isValid = false;
	});

	if (isValid) {
		var params = new Object();
		var isSuccess = false;
		params.foreName = $("#txtFirst_name").val();
		params.surname = $("#txtSurname").val();
		params.email = $("#txtEmail").val();
		params.password = $("#txtPassword").val();
		params.referredBy = _ddlReferredBy.length > 0 ? _ddlReferredBy.find("option:selected").val() : _referredBy;
		params.invitedBy = _invitedBy;
		//params.referredBy = _referredBy;
		params.voucherID = _voucherID;
		params.countryID = _countryID;
		$.ajaxTools({
			url: 'Registration.aspx/Submit',
			data: params,
			beforeSend: function () {
				ShowWaitDialog();
				return true;
			},
			success: function (data) {
				if (!data.Result) {
					ShowMessageDialog(data.Message);
					return false;
				}
				isSuccess = true;

			},
			complete: function () {
				if (isSuccess)
					window.location = _path;
					//__doPostBack('ctl00$MainContent$btnSubmit', '');
				else
					HideWaitDialog();
			},
			error: function (req, status) {
				ShowMessageDialog("is currently experiencing technical difficulties with registration please<br />try again later. Please contact us if problems persist.<br />");
				return false;
			}
		});
	}
}

function Validate(obj) {
	var parent = obj.parent();
	var img;
	var isValid;
	var isSelect = (obj.attr("nodeName") == "SELECT");
	if (isSelect)
	    parent = $(".selectedTxt");

	parent.find("img").remove();

	isValid = obj.isValid();
	if (!isValid)
	    img = "<img src='Resources/Images/newSkin/2009/buttons/cross.png' style='margin-left:-30px; margin-top: -2px' />";
	else
		img = "<img src='Resources/Images/newSkin/2009/buttons/tick.png' style='margin-left:-30px; margin-top: -2px' />";

	parent.append(img);

	img = parent.find("img");
	img.addClass("ui-watermark-label");
	img.css({
		left: 328,//parseInt(obj.css("borderLeftWidth")) + parseInt(obj.css("borderRightWidth")) + parseInt(obj.css("width")) + 16,
		top: parseInt(obj.css("borderTopWidth")) + parseInt(obj.css("paddingTop"))
	});

	return isValid;
}
