/* #############################################################################
Auxiliary section extensions and utilites
################################################################################
    All JS in thes file by:
		tim.curzon@pipex.net
	Oct 2006 onwards ...
    (c)2006 PIPEX
--------------------------------------------------------------------------------
	Dependancies:
	[01] Prototype Javascript library v1.5+ 
		 /library/javascript/prototype.js
	[02] Error Trapping Javascript library
		 /library/javascript/errortrap.js
############################################################################# */


/* #############################################################################
Utility functions: check enter key was pressed
############################################################################# */

// declare new keyPress object ==========
//
var keyInput = new Object();

// [checkEnterKey] function ==========
//
keyInput.enterKey = function(ev) {
	
	// store key / char code
	var key = ev.charCode ? ev.charCode : ev.keyCode;
	
	// if enter key was pressed return true
	if (key == 13) {
		return true; // return
	
	// otherwise return false
	} else {
		return false; // return
	}
}


/* #############################################################################
Contact support page: show / hide decision pods and email form
############################################################################# */

// declare new contactPod object ==========
//
var contactPod = new Object();

// [initialise] function ==========
//
contactPod.initialise = function() {
	
	// check the page loaded has the contact pod
	contact_pod_flag = $("contact_pod");
	if (contact_pod_flag) {
		
		// get contact pod sections		
		var faqs				= $("cpod_faqs");
		var contactInfo			= $("cpod_contactinfo");
		var checks				= $("cpod_checks");
		var faqCheckedYes		= $("faq_check_yes");
		var faqCheckedNo		= $("faq_check_no");
		var contactInfoButton   = $("cpod_contactinfobutton");

		// hide the faq / contact info sections
		if (faqs) 		 faqs.style.display 	   = "none";
		if (contactInfo) contactInfo.style.display = "none";
		
		// if the "checked faqs" radio buttons exist, show them
		if (checks) checks.className = checks.className.replace(/hidden/,"");

		// attach faqsChecked function to faqs checked "Yes" / "No" radio button onclick
		if (faqCheckedYes) faqCheckedYes.onclick = function(event) { if (!event) event=window.event; contactPod.faqsChecked("yes"); }
		if (faqCheckedNo)  faqCheckedNo.onclick  = function(event) { if (!event) event=window.event; contactPod.faqsChecked("no"); }

		// attach fullContactInfo function to contactInfoButton onclick
		if (contactInfoButton) contactInfoButton.onclick = function(event) { if (!event) event=window.event; contactPod.fullContactInfo(); return false; }

	}
}

// [faqsChecked] function ==========
//
contactPod.faqsChecked = function(action) {
		
	// get contact pod sections		
	var faqs				= $("cpod_faqs");
	var contactInfo			= $("cpod_contactinfo");
	var checks				= $("cpod_checks");
	var faqCheckedYes		= $("faq_check_yes");
	var faqCheckedYesLabel	= $("faq_check_yes_label");
	var faqCheckedNo		= $("faq_check_no");
	var faqCheckedNoLabel	= $("faq_check_no_label");
	var faqLinksIntro		= $("cpod_faqlinks_intro");
	var contactInfoButton	= $("cpod_contactinfobuttonholder");

	// if customer says faq's checked
	if (action == "yes") {
		
		// show the contact details form
		if (contactInfo) contactInfo.style.display = "block";
		if (faqs) faqs.style.display = "none";

	// if customer says faq's not checked
	} else if (action == "no") {

		// if the faqLinksIntro paragraph exists, show it
		if (faqLinksIntro) faqLinksIntro.className = faqLinksIntro.className.replace(/hidden/,"");

		// if the contactInfoButton button exists, show it
		if (contactInfoButton) contactInfoButton.className = contactInfoButton.className.replace(/hidden/,"");

		// show the faqs form
		if (faqs) faqs.style.display = "block";
		if (contactInfo) contactInfo.style.display = "none";

		// disable the faqs checked "Yes" radio button
		// faqCheckedYes.disabled = true;
		// faqCheckedYesLabel.style.color = "#bbb";

	}
}
	
// [fullContactInfo] function ==========
//
contactPod.fullContactInfo = function() {
			
	// get contact pod elements		
	var contactInfo			= $("cpod_contactinfo");
	var contactInfoButton	= $("cpod_contactinfobuttonholder");
	
	// hide the contact details button
	if (contactInfoButton) contactInfoButton.style.display = "none";
	
	// show the contact details form
	if (contactInfo) contactInfo.style.display = "block";
	
	// jump to top of contact info form
	window.location = "#cpod_contactinfo";

}

// object initialisation (PROTOTYPE EVENT HOOKING) ==========
//
Event.observe(window,'load',contactPod.initialise,false);


/* #############################################################################
Lost login request page: show contact support link
############################################################################# */

// declare new lostLoginError object ==========
//
var loginReqSupportLink = new Object(); // object to hold contact error processing functions

// [initialise] function ==========
//
loginReqSupportLink.initialise = function() {
	
	// get elements
	var loginreq_helplink 	= $("loginreq_helplink");
	var loginreq_contact	= $("loginreq_contact");
	
	// check the page loaded is the "lost your login" page
	loginrequest_flag = $("loginreq_flag");
	
	// if we're on the right page and the right elements are present
	if (loginrequest_flag && loginreq_helplink) {

		// show the help link
		loginreq_helplink.className = ""

		// hide the contact paragraph
		loginreq_contact.style.display = "none";
		
		// attach show/hide function to link onclick event
		Event.observe(loginreq_helplink,'click',loginReqSupportLink.show,false);
	} 

}

// [show] function ==========
//
loginReqSupportLink.show = function() {
	
	// get elements
	var loginreq_helplink	= $("loginreq_helplink");
	var loginreq_contact	= $("loginreq_contact");
	
	// show "contact" blurb, hide the "help!" link
	loginreq_contact.style.display = "block";
	loginreq_helplink.style.display = "none";
		
}

// object initialisation (PROTOTYPE EVENT HOOKING) ==========
//
Event.observe(window,'load',loginReqSupportLink.initialise,false);


/* #############################################################################
Lost login request page: error trapping
############################################################################# */

// declare new lostLoginError object ==========
//
var loginReqError = new Object(); // object to hold login request error processing functions

// declare object to hold variables
//
var ss_vars = new Object();

// [initialise] function ==========
//
loginReqError.initialise = function() {
	
	// check the page loaded is the "lost your login" page
	loginrequest_flag = $("loginreq_flag");

	if (loginrequest_flag) {
		
		// error message class constructor
		function err(code,cust) {
			this.code 	= code;
			this.cust 	= cust;
		}
		
		// define error messages
		ss_vars.errors = new Array(
			new err(
				code = "-99",
				cust = "The form is empty. Please enter your email address, username or a domain name active in your account."
			)
		);

		// attach the checkForm function to onkeydown attribute for selected fields
		// but trigger only when enter key is pressed
		if ($("loginreq_data"))$("loginreq_data").onkeydown = function(event) { if (!event) event=window.event; if(keyInput.enterKey(event)) return loginReqError.checkForm(); }

		// attach the checkForm function to onsubmit attribute
		if ($("loginreq_form")) $("loginreq_form").onsubmit = function(event) { return loginReqError.checkForm(); }

	}
}

// [checkForm] function ==========
//
loginReqError.checkForm = function() {

	// clear error list
	errorTrap.errors.clear();

	// hide server error box
	if ($("errormsg")) $("errormsg").style.display = "none";

	// process the form 
	// gather payment form values
	var loginreq_form 		= $("loginreq_form");
	var loginreq_data		= $("loginreq_data").value;
	
	// trap for errors
	// also modify label & input field classes according to error status 
	// (add or remove emphasis / red stripes)

		// data field
		errorTrap.test("isNull",loginreq_data,"-99","trigMsg")

		// end traps

	// if errors found and error box element exists, display error box,
	var err_box = $("errormsg_clientbox");
	var err_msgholder = $("errormsg_clientmsgs");
	
	if (errorTrap.errors.length > 0 && err_box && err_msgholder) {

		errorTrap.display(err_box,err_msgholder); // display error box
		window.location = "#errormsg_clientbox"; // scroll to error box
		return false;
	
	// otherwise if no errors found submit form
	} else if (errorTrap.errors.length < 1) {

		return true;	
	}
}

// object initialisation (PROTOTYPE EVENT HOOKING) ==========
//
Event.observe(window,'load',loginReqError.initialise,false);


/* #############################################################################
Lost login password change page: form setup & password strength checking
############################################################################# */

// declare new lostLoginError object ==========
//
var loginPassForm = new Object(); // object to hold contact error processing functions

// [initialise] function ==========
//
loginPassForm.initialise = function() {
	
	// check the page loaded is the "change your password" page
	loginpassword_flag = $("loginpasschange_flag");

	if (loginpassword_flag) {
		
		// gather payment form elements
		var pass_form 		= $("pass_form");
		var pass_old 		= $("pass_old");
		var pass_new 		= $("pass_new");
		var pass_confirm	= $("pass_confirm");
		var pass_meter		= $("pass_meter");
		
		// reduce left margin on form fields (apply stdform_minifields class)
		if (pass_form) pass_form.className = pass_form.className + " stdform_minifields";
		
		// reduce form field size
		if (pass_old) 		pass_old.size = 25;
		if (pass_new)	 	pass_new.size = 25;
		if (pass_confirm)	pass_confirm.size = 25;
		
		// show password strength meter
		if (pass_meter)	pass_meter.className = null;
	}
}

// [chkpass] function ==========
//
loginPassForm.chkpass = function() {

	var np = document.getElementById('pass_new').value;
	
	var reg1 = /\w/;
	var regDigit = /\d/;
	var regUpperCase = /[A-Z]/;
	var regLowerCase = /[a-z]/;
	var regNonLetterDigit = /\W/;
	
	var msg = '';
	var securityCount = 0;

	// if less than 6 characters not enough
	if (np.length < 6){
		securityCount = 0;
	}

	// if 6 characters and a lower case - weak
	if ( (np.length >= 6) && (regLowerCase.test(np)) ){
		securityCount = 1;
	}
	// if 6 characters and a upper case - weak
	if ( (np.length >= 6) && (regUpperCase.test(np)) ){
		securityCount = 1;
	}

	// if 6 characters and a digit - weak
	if ( (np.length >= 6) && (regDigit.test(np)) ){
		securityCount = 1;
	}

	// if 7 characters and a lower case - weak
	if ( (np.length >= 7) && (regLowerCase.test(np)) ){
		securityCount = 2;
	}
	// if 7 characters and a upper case - weak
	if ( (np.length >= 7) && (regUpperCase.test(np)) ){
		securityCount = 2;
	}

	// if 7 characters and a digit - weak
	if ( (np.length >= 7) && (regDigit.test(np)) ){
		securityCount = 2;
	}

	// if 6 characters and a lower case letter and a upper case - average
	if ( (np.length >= 6) && (regLowerCase.test(np)) && (regUpperCase.test(np)) ){
		securityCount = 2;
	}

	// if 6 characters and a lower case letter and a digit - average
	if ( (np.length >= 6) && (regDigit.test(np)) && (regLowerCase.test(np)) ){
		securityCount = 2;
	}

	// if 6 characters and a upper case letter and a digit - average
	if ( (np.length >= 6) && (regDigit.test(np)) && (regUpperCase.test(np)) ){
		securityCount = 2;
	}

	// if 6 characters and an upper case letter and a lower case letter - average
	if ( (np.length >= 6) && (regUpperCase.test(np)) && (regLowerCase.test(np)) ){
		securityCount = 2;
	}


	//if over 6 characters, and upper and lower case letters, and a digit - strong
	if ( (np.length > 6) && (regDigit.test(np)) && (regUpperCase.test(np)) && (regLowerCase.test(np)) ){
		securityCount = 3;
	}

	//if over 7 characters and has upper case and digit - strong
	if ( (np.length > 6) && (regDigit.test(np)) && (regLowerCase.test(np)) ){
		securityCount = 3;
	}

	//if over 7 characters and has upper case and digit - strong
	if ( (np.length > 6) && (regDigit.test(np)) && (regUpperCase.test(np)) ){
		securityCount = 3;
	}

	//if over 7 characters and has lower case and digit - strong
	if ( (np.length > 6) && (regDigit.test(np)) && (regLowerCase.test(np)) ){
		securityCount = 3;
	}

	//if over 8 characters and has lower case - strong
	if ( (np.length > 7) &&  (regLowerCase.test(np)) ){
		securityCount = 3;
	}

	//if over 8 characters and has lower case - strong
	if ( (np.length > 7) &&  (regUpperCase.test(np)) ){
		securityCount = 3;
	}

	//if over 8 characters and has digit - strong
	if ( (np.length > 7) &&  (regDigit.test(np)) ){
		securityCount = 3;
	}

	if (securityCount == 1){
		document.getElementById('meter_weak').style.background = "#AF0000";
		document.getElementById('meter_weak').style.color = "#FFFFFF";
		document.getElementById('meter_average').style.background = "silver";
		document.getElementById('meter_average').style.color = "gray";
		document.getElementById('meter_strong').style.background = "silver";
		document.getElementById('meter_strong').style.color = "gray";
	} else if (securityCount == 2){
		document.getElementById('meter_weak').style.background = "silver";
		document.getElementById('meter_weak').style.color = "gray";
		document.getElementById('meter_average').style.background = "#F39638";
		document.getElementById('meter_average').style.color = "#FFFFFF";
		document.getElementById('meter_strong').style.background = "silver";
		document.getElementById('meter_strong').style.color = "gray";
	} else if (securityCount == 3){
		document.getElementById('meter_weak').style.background = "silver";
		document.getElementById('meter_weak').style.color = "gray";
		document.getElementById('meter_average').style.background = "silver";
		document.getElementById('meter_average').style.color = "gray";
		document.getElementById('meter_strong').style.background = "#91B821";
		document.getElementById('meter_strong').style.color = "#FFFFFF";
	} else {
		document.getElementById('meter_weak').style.background = "silver";
		document.getElementById('meter_weak').style.color = "gray";
		document.getElementById('meter_average').style.background = "silver";
		document.getElementById('meter_average').style.color = "gray";
		document.getElementById('meter_strong').style.background = "silver";
		document.getElementById('meter_strong').style.color = "gray";
	}
}

// object initialisation (PROTOTYPE EVENT HOOKING) ==========
//
Event.observe(window,'load',loginPassForm.initialise,false);


/* #############################################################################
Lost login password change page: error trapping
############################################################################# */

// declare new lostLoginError object ==========
//
var loginPassError = new Object(); // object to hold password change error processing functions

// declare object to hold variables
//
var ss_vars = new Object();

// [initialise] function ==========
//
loginPassError.initialise = function() {

	// check the page loaded is the "change your password" page
	loginpassword_flag = $("loginpasschange_flag");

	if (loginpassword_flag) {
		
		// error message class constructor
		function err(code,cust) {
			this.code 	= code;
			this.cust 	= cust;
		}
		// define error messages
		ss_vars.errors = new Array(
			new err(
				code = "-100",
				cust = "You have not entered a new password"
			),
			new err(
				code = "-101",
				cust = "You have not confirmed your new password"
			),
			new err(
				code = "-102",
				cust = "Your new password and its confirmation do not match"
			),
			new err(
				code = "-103",
				cust = "Your new password must be at least 6 characters long"
			)
		);

		// attach the checkForm function to onkeydown attribute for selected fields
		// but trigger only when enter key is pressed
		if ($("pass_new")) $("pass_new").onkeydown = function(event) { if (!event) event=window.event; if(keyInput.enterKey(event)) return loginPassError.checkForm(); }
		if ($("pass_confirm")) $("pass_confirm").onkeydown = function(event) { if (!event) event=window.event; if(keyInput.enterKey(event)) return loginPassError.checkForm(); }

		// attach the checkForm function to onsubmit attribute
		if ($("pass_form")) $("pass_form").onsubmit = function(event) { return loginPassError.checkForm(); }

	}
}

// [checkForm] function ==========
//
loginPassError.checkForm = function() {

	// clear error list
	errorTrap.errors.clear();

	// hide server error box
	if ($("errormsg")) $("errormsg").style.display = "none";

	// process the form 
	// gather payment form values
	var pass_form 		= $("pass_form");
	var pass_new 		= $("pass_new").value;
	var pass_confirm	= $("pass_confirm").value;

	// trap for errors
	// also modify label & input field classes according to error status 
	// (add or remove emphasis / red stripes)

		// new password
		if (
		errorTrap.test("isNull",pass_new,"-100","trigMsg") 
		|| errorTrap.test("isNotReqLength",Array(pass_new,6,128),"-103","trigMsg") 
		){
			$("pass_new").className = $("pass_new").className + " error";
			$("pass_new_label").className = $("pass_new_label").className + " fieldname_emph";
		} else {
			$("pass_new").className = $("pass_new").className.replace(/error/g,"");
			$("pass_new_label").className = $("pass_new_label").className.replace(/fieldname_emph/g,"");
		}

		// password confirmation
		if (
		errorTrap.test("isNull",pass_confirm,"-101","trigMsg") 
		|| errorTrap.test("isNotMatch",Array(pass_new,pass_confirm),"-102","trigMsg") 
		){
			$("pass_confirm").className = $("pass_confirm").className + " error";
			$("pass_confirm_label").className = $("pass_confirm_label").className + " fieldname_emph";
		} else {
			$("pass_confirm").className = $("pass_confirm").className.replace(/error/g,"");
			$("pass_confirm_label").className = $("pass_confirm_label").className.replace(/fieldname_emph/g,"");
		}

		// end traps

	// if errors found and error box element exists, display error box,
	var err_box = $("errormsg_clientbox");
	var err_msgholder = $("errormsg_clientmsgs");
	
	if (errorTrap.errors.length > 0 && err_box && err_msgholder) {

		errorTrap.display(err_box,err_msgholder); // display error box
		window.location = "#errormsg_clientbox"; // scroll to error box
		return false;
	
	// otherwise if no errors found submit form
	} else if (errorTrap.errors.length < 1) {

		return true;	
	}
}

// object initialisation (PROTOTYPE EVENT HOOKING) ==========
//
Event.observe(window,'load',loginPassError.initialise,false);


/* The end ################################################################## */