///////////////////////////////////////////////////////////
// Common javascript for Display section
// Last Updated: 08-Feb-2007
///////////////////////////////////////////////////////////

function openWindow(url,name,widgets) {
	popupWin = window.open (url,name,widgets);
	popupWin.focus();
}

function clear_message() {
	document.getElementById("txtHint").innerHTML='<p class="msg-blank">&nbsp;<br />&nbsp;</p>'
}

function clear_message2() {
	document.getElementById("txtHint2").innerHTML='<p class="msg-blank">&nbsp;<br />&nbsp;</p>'
}

function checkmail() {
	clear_message();
	var f = window.document.theform;
	if (f.EmailHtml.checked == true) {
		f.RecvEmailOk.checked=true;
		f.RecvEmailOk.disabled=true;
	} else {
		f.RecvEmailOk.disabled=false;
	}
}

// use xml http request ///////////////////////////////////

// used to distinguish between more than one <div id="txtHint"> on a page.
var iTag="";

// global request and XML document objects
var req;

function update_Prefs() {
	var f = window.document.theform;
	var str="?RecvEmailOk=" + f.RecvEmailOk.checked +
			"&EmailHtml=" + f.EmailHtml.checked +
			"&ShowAddr=" + f.ShowAddr.value +
			"&ShowPhnFax=" + f.ShowPhnFax.value +
			"&ShowEmail=" + f.ShowEmail.value +
			"&ShowMobile=" + f.ShowMobile.value +
			"&ShareData=" + f.ShareData.checked;
	var url="profile_change_prefs_action.asp" + str;
	loadXMLDoc(url);
} 

function update_Details() {
	var f = window.document.theform;
	var str="?NameDisp=" + f.NameDisp.value +
			"&JobTitle=" + f.JobTitle.value +
			"&Addr=" + escape(f.Addr.value) +
			"&City=" + f.City.value +
			"&PostCd=" + f.PostCd.value +
			"&CtryCd=" + f.CtryCd.value +
			"&StateID=" + f.StateID.value +
			"&PhnCd=" + f.PhnCd.value +
			"&PhnDir=" + f.PhnDir.value +
			"&FaxDir=" + f.FaxDir.value +
			"&Email=" + f.Email.value +
			"&SkypeName=" + f.SkypeName.value +
			"&Mobile=" + f.Mobile.value +
			"&Sex=" + f.Sex.value +
			"&MemoInfo=" + escape(f.MemoInfo.value);
	var url="profile_change_action.asp" + str;
	loadXMLDoc(url);
} 

function update_LoginName() {
	var f = window.document.LoginName;
	var str="?MembLogin=" + f.MembLogin.value;
	var url="profile_change_name_action.asp" + str;
	loadXMLDoc(url);
} 

function update_Password() {
	var p = window.document.pass;
	var str="?OldPwd=" + p.OldPwd.value +
			"&MembPwd=" + p.MembPwd.value +
			"&ReMembPwd=" + p.ReMembPwd.value;
	var url="profile_change_pass_action.asp" + str;
	iTag="2";
	loadXMLDoc(url);
} 

function select_Mailists() {
	var url="profile_mail_lists_select_action.asp";
	loadXMLDoc(url);
}

function update_Mailists() {
	var f = window.document.theform;
	var str="?";
	for (i=0; i <= f.MailListID.length-1; i++) {
		if (eval("f.MailListID[i].checked") == true) {
			str += "MailListID" + "=" + f.MailListID[i].value;
			if (i != f.MailListID.length-1) {
				str +="&";
			}
		}
	}
	var url="profile_mail_lists_change_action.asp" + str;
	loadXMLDoc(url);
}

function loadXMLDoc(url) {
	req = false;
	// branch for native XMLHttpRequest object
	if(window.XMLHttpRequest) {
		try {
			req = new XMLHttpRequest();
		}
		catch(e) {
			req = false;
		}
	}
	// branch for IE/Windows ActiveX version
	else if(window.ActiveXObject) {
		try {
			// pre MSIE 5.5
			req = new ActiveXObject("MSXML2.XMLHTTP");
			if(req) {
				// bodge to force IE to renew values passed to it.
				req.abort();
			}
	 	}
		catch(e) {
			try {
				// MSIE 5.5 onwards
		 		req = new ActiveXObject("Microsoft.XMLHTTP");
				if(req) {
					// bodge to force IE to renew values passed to it.
					req.abort();
				}
			}
			catch(e) {
		 		req = false;
			}
		}
	}
	if(req) {
		req.onreadystatechange = processReqChange;
		req.open("post",url,true);
		req.send("");
	}
	else {
		alert('Error: Possibly your Browser security settings\n\nprevent you from making these changes.');
	}
}

function processReqChange() {
	// only if req shows "complete"
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			// write out the message in the <div> tag
			document.getElementById("txtHint" + iTag).innerHTML=req.responseText;
			iTag="";
        }
		else {
			alert('There was a problem retrieving the XML data:\nError: '+ req.status +' - '+ req.statusText);
		}
	}
}

///////////////////////////////////////////////////////////

function textCounter(field,countfield,maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
		countfield.value = maxlimit - field.value.length;
	}
}

///////////////////////////////////////////////////////////

function validate_form() {
	var f = window.document.theform;
	//if Mobile number is entered, check it is only numbers
	var checkOK="0123456789";
	var checkStr=(f.Mobile.value);
	var allValid=true;
	var allNum="";
	for (i=0; i < checkStr.length; i++)	{
		ch=checkStr.charAt(i);
		for (j=0; j < checkOK.length; j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length) {
			allValid=false;
			break;
		}
	}
	if (!allValid) {
		alert("Your Mobile number must be made up of Numbers only,\nstarting with your Country Code, for example:\n 6619991234\nPlease do not include any spaces or non-numeric characters such as +()-");
		f.Mobile.focus();
		return (false);
	}
return true;
}

function form_save() {
	if (validate_form()) {
		update_Details();
	}
}

function Check_ValidPass() {

	var p = window.document.pass;

	var msg1 = "Password must be a minimum of 6 characters.";
	var msg2 = "The fields 'New Password' and 'Confirm New Password' are not the same. Please re-type.";

	if ((p.OldPwd.value == "") || (p.OldPwd.value.length == 0)) {
		alert(msg1);
		p.OldPwd.focus();
		return false;
	}
	if ((p.MembPwd.value == "") || (p.MembPwd.value.length < 6)) {
		alert(msg1);
		p.MembPwd.focus();
		return false;
	}
	if ((p.ReMembPwd.value == "") || (p.ReMembPwd.value.length < 6)) {
		alert(msg1);
		p.ReMembPwd.focus();
		return false;
	}
	if (p.MembPwd.value != p.ReMembPwd.value) {
		alert(msg2);
		p.MembPwd.value = "";
		p.ReMembPwd.value = "";
		p.MembPwd.focus();
		return false;
	}
	return true;
}

function change_Pass() {
	if (Check_ValidPass()) {
		update_Password();
	}	
}

function check_Passwords() {

	var p = window.document.change_pwd_form;

	var msg1 = "Password must be a minimum of 6 characters.";
	var msg2 = "The fields 'New Password' and 'Confirm Password' are not the same. Please re-type.";

	if ((p.Password1.value == "") || (p.Password1.value.length == 0)) {
		alert(msg1);
		p.Password1.focus();
		return false;
	}
	if ((p.Password2.value == "") || (p.Password2.value.length < 6)) {
		alert(msg1);
		p.Password2.focus();
		return false;
	}
	if (p.Password1.value != p.Password2.value) {
		alert(msg2);
		p.Password1.value = "";
		p.Password2.value = "";
		p.Password1.focus();
		return false;
	}
	return true;
}

function goto_PasswordChange() {
	if (check_Passwords()) {
		document.change_pwd_form.submit();
	}	
}

function change_LoginName() {
	if ((document.LoginName.MembLogin.value == "") || (document.LoginName.MembLogin.value.length < 4)) {
		alert("You Login Name must be at least 4 characters long!");
		document.LoginName.MembLogin.focus();
	}
	else {
		update_LoginName();
	}
}

//

function form_save_blog() {
	if (validate_form_blog()) {
		document.theform.submit();
	}
}

function validate_form_blog() {

	var b = window.document.theform;

	var msg1 = "Blog Subject cannot be blank!";
	var msg2 = "Blog cannot be blank, please enter some text!";

	if ((b.BlogSubject.value == "") || (b.BlogSubject.value.length == 0)) {
		alert(msg1);
		b.BlogSubject.focus();
		return false;
	}
	if ((b.BlogText.value == "") || (b.BlogText.value.length == 0)) {
		alert(msg1);
		b.BlogText.focus();
		return false;
	}
	return true;
}

function form_save_comment() {
	if (validate_form_comment()) {
		document.theform.submit();
	}
}

function validate_form_comment() {

	var b = window.document.theform;

	var msg = "Comment cannot be blank, please enter some text!";

	if ((b.ReplyText.value == "") || (b.ReplyText.value.length == 0)) {
		alert(msg);
		b.ReplyText.focus();
		return false;
	}
	return true;
}

function refreshParent() {
	window.opener.location.href = window.opener.location.href;
	if (window.opener.progressWindow) {
		window.opener.progressWindow.close();
	}
window.close();
}

// to hide/display an element

function show_hide(divID, show) {
	var d;
	d = document.getElementById(divID);
	if (d) {
		if (null == show) show = d.style.display == 'none';
		d.style.display = (show ? '' : 'none');
	}
}

function RemoveContent(d) {
	document.getElementById(d).style.display = "none";
	document.getElementById(d + 'H').style.display = "none";
	document.getElementById(d + 'S').style.display = "inline";
}

function InsertContent(d) {
	document.getElementById(d).style.display = "";
	document.getElementById(d + 'S').style.display = "none";
	document.getElementById(d + 'H').style.display = "inline";
}


//////////////////////////////////////

// used in /asp/results.asp

function org_mat_results_drop_downs1() {
	var ov = document.resultsForm.OrganisationID.value;
	var cv = document.resultsForm.MatchCatID.value;
	// if we change selections in drop-downs 'Organisation', or 'Category', then
	// we want to ensure MatchID is 0 so the user is forced to re-select the Match.
	var mv = document.resultsForm.MatchID.value = 0;
	location.href='results.asp?OrganisationID=' + ov + '&MatchCatID=' + cv + '&MatchID=' + mv;
}

function org_mat_results_drop_downs2() {
	var ov = document.resultsForm.OrganisationID.value;
	var cv = document.resultsForm.MatchCatID.value;
	var mv = document.resultsForm.MatchID.value;
	location.href='results.asp?OrganisationID=' + ov + '&MatchCatID=' + cv + '&MatchID=' + mv;
}


