// JavaScript Library
// Copyright Lana-Design.com 2003
// Author: Slava Rossov

//
//
//
function Translate() {
    this.txt_prefix = "Prefix";
    this.txt_firstname = "First Name";
	this.txt_lastname = "Last Name";
    this.txt_email = "E-Mail Address";
	//this.txt_receive_updates = "Periodic E-mail Updates";
	this.txt_country = "Country";
	this.txt_how = "How did you hear about us?";
    this.txt_zip = "Zip code";
    this.txt_username = "Username";
    this.txt_password = "Password";
	this.txt_password2 = "Confirm Password";
	this.bln_agree = "Terms and Conditions";
}

//
// Set optional elements(fields) in forms, that can be skipped by user
//
function setOptionalElements(formName) {
	if (formName.name == "frm_register") {
		formName.txt_investortype.optional = true;
		formName.txt_other.optional = true;
		//formName.bln_level4.optional = true;
		//formName.bln_level3.optional = true;
		//formName.bln_level2.optional = true;
		//formName.bln_level1.optional = true;
		//formName.txt_other_checkbox.optional = true;
		//formName.txt_investortypeother.optional = true;
		//formName.txt_MI.optional = true;
		formName.txt_company.optional = true;
		formName.txt_companydescription.optional = true;
		formName.txt_title.optional = true;
		formName.txt_address1.optional = true;
		formName.txt_address2.optional = true;
		formName.txt_city.optional = true;
		formName.txt_state.optional = true;
		formName.txt_zip.optional = true;
		formName.txt_homephone.optional = true;
		formName.txt_workphone.optional = true;
		formName.txt_fax.optional = true;
	}
}

//
// Checks if the string is blank
//
function isBlank(s) {
	for(var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ( (c != ' ') && (c != '\t') && (c != '\n') && (c != '') ) return false;
	}
	return true;
}

//
// Checks if the ZIP code is valid
//
function isZip(zip) {
    var rcZip = /\d{5}/;
    var result = zip.match(rcZip);
    if (result != null) return true;
    return false;
}

//
// Checks if the e-mail address is valid
//
function isEmail(email) {
    var rcEmail = /^[\w.-]{2,}@[\w.-]{2,}[.]{1}[a-z]{2,}$/;
    var result = email.match(rcEmail);
    if (result != null) return true;
    return false;
}

//
// Validate form
//
function validateForm(formName) {
	var empty_fields = "";
	var errors = "";
	var msg;
	var t = new Translate();
	var passLen = 6;
	var pass1 = '';
	var pass2 = '';

	for(var i = 0; i < formName.length; i++) {
		var e = formName.elements[i];

		// check for empty fields
		if ( (e.type == "text" || e.type == "password") && !e.optional) {
			if ( (e.value == null) || (e.value == "") || isBlank(e.value) ) {
				empty_fields += "\n        " + t[e.name];
				continue;
			}
		}

		if (e.type == "select-one" && !e.optional) {
			var val = e.options[e.selectedIndex].value;

			if ( (val == null) || (val == "") || isBlank(val) ) {
				empty_fields += "\n        " + t[e.name];
				continue;
			}
		}
		/*
		if ( (e.type == "select-one") && !e.optional ) {
			var val = "";
			for (var i = 0; i < e.options.length; i++) {
				if (e.options[i].selected) {
					val += e.options[i].value;
				}
			}
			if ( (val == null) || (val == "") || isBlank(val) ) {
				empty_fields += "\n        " + t[e.name];
				continue;
			}
		}
		*/
		// check for specific fields values
		if (e.name == "txt_zip" && !isBlank(e.value)) {
            if (!isZip(e.value)) errors += " - Incorrect value for the " + t[e.name] + " field.\n";
			continue;
		}

		if (e.name == "txt_email") {
            if (!isEmail(e.value)) errors += " - Incorrect value for the " + t[e.name] + " field.\n";
			continue;
		}

		if (e.name == "txt_password") {
		    var pass = e.value;
		    pass1 += pass;
		    if (pass.length < passLen) errors += " - Password length should be more than " + (passLen - 1) + " characters.\n";
			continue;
		}

		if (e.name == "txt_password2") {
		    var pass = e.value;
		    pass2 += pass;
		    if (pass.length < passLen) errors += " - Confirm Password length should be more than " + (passLen - 1) + " characters.\n";
			continue;
		}
		
		if (e.name == "bln_agree" && !e.checked) {
			// Terms and conditions
			errors += " - You must read and agree with the terms and conditions \n   to become a registered user.\n";
		}
	
	}
    
    if (formName.name == "frm_register") {
    	if (pass1 != pass2) {
    	    errors += " - Passwords are not the same.\n";
    	}	
	}
	

	if (!empty_fields && !errors) return true;

	msg  = "__________________________________________________\n\n";
	msg += "The form was not submitted because of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "__________________________________________________\n\n";

	if (empty_fields) {
		msg += " - The following required field(s) are empty:" + empty_fields + "\n";
		if (errors) msg += "\n";
	}

	msg += errors;
	alert(msg);
	return false;
}

function checkErrors() {
	var allCookies = 'Nothing new';
	if (document.cookie) allCookies = document.cookie;
	alert("Here is your cookie:\n" + allCookies);
}


//
// Opens Pop-Up Window
//
function PopUp(windowUrl) {
    //var w = window.open(windowUrl, "popup-window", "width=300,height=300,status=yes,resizable=yes");
    var wnd = window.open(windowUrl,'popupWindow','width=420,height=200,scrollbars=yes');
    return false;
}