/**
 * SlikSvnWeb - Client javascript functions
 *
 * @version $Id: SlikSvnWebClient.js 110 2009-11-02 10:28:42Z Jan $
 */


function specsShowDescription(messageTitle, messageBody) {
	specDescriptionElement = document.getElementById("specDescription");
	if (!specDescriptionElement) {
		return;
	}

	html  = "<h4>" + messageTitle + "</h4>\n";
	html += messageBody;

	specDescriptionElement.innerHTML = html;
}


function signupShowStep(step) {
	numSteps = 4;
	for (i = 1; i <= numSteps; i++) {
		document.getElementById("signupStep" + i).style.display = (step == i) ? "inline" : "none";
	}
	
	// go to the top of the page
	window.scrollTo(0,0);
	
	//	Call generic window init function from common.js to resize background again
	init();
}


function signupCalculateTotal() {
	planPrice = 0;
	donationPrice = 0;
	numUsers = 1;
	numRepos = 1;

	if (document.getElementById("planPiano").checked) {
		planPrice = 4.95;
		donationPrice = 1;
		numUsers = 2;
		numRepos = 3;
	}
	else if (document.getElementById("planMezzo").checked) {
		planPrice = 9.95;
		donationPrice = 2;
		numUsers = 10;
		numRepos = 5;
	}
	else if (document.getElementById("planForte").checked) {
		planPrice = 19.95;
		donationPrice = 5;
		numUsers = 20;
		numRepos = 10;
	}
	else if (document.getElementById("planDedicated").checked) {
		planPrice = NaN;
		donationPrice = NaN;
		numUsers = 100;
		numRepos = 100;
	}

	donationEnabled = document.getElementById("donationTrue").checked;

	totalPrice = planPrice;
	if (donationEnabled) {
		totalPrice += donationPrice;
	}

	totalInput = document.getElementById("total");
	if (isNaN(totalPrice)) {
		totalInput.value = "We will contact you";
	} else if (totalPrice == 0) {
		totalInput.value = "0.00";
	} else {
		totalInput.value = totalPrice;
	}

	if (numUsers == 1) {
		document.getElementById("usersCaption").innerHTML = 'Repository user';
		document.getElementById("usersSubCaption").innerHTML = 'Specify the username and password you want.';
	}
	else {
		document.getElementById("usersCaption").innerHTML = 'Repository usernames and passwords';
		document.getElementById("usersSubCaption").innerHTML = 'Specify as many usernames and passwords as you initially need.';
	}
	
	if (numRepos == 1) {
		document.getElementById("repositoriesCaption").innerHTML = 'Repository';
		document.getElementById("repositoriesSubCaption").innerHTML = '';
	}
	else {
		document.getElementById("repositoriesCaption").innerHTML = 'Repositories';
		document.getElementById("repositoriesSubCaption").innerHTML = 'Specify the repositories you want.<br />';
	}

	for (i=1; i<=20; i++) {
		userDiv = document.getElementById("user" + i);
		userDiv.style.display = (i <= numUsers) ? "inline" : "none";

		repoDiv = document.getElementById("repo" + i);
		repoDiv.style.display = (i <= numRepos) ? "inline" : "none";
	}

}


function isEmpty(field) {
	if (field.type == "checkbox") {
		empty = !field.checked;
	} else {
		//	Trim text values
		value = field.value.replace(/[^a-zA-Z0-9]/g, "");
		empty = (value=="") || (value=="na") || (value=="NA");		//	"N/A" is commonly entered
	}
	return empty;	
}


function checkRequiredFields(formId) {
	checkedForm = document.getElementById(formId);
	if (!checkedForm) {
		alert("Form not found: '" + formId + "'");
		return false;
	}

	invalidRepoName = false;
	repoCount = 0;
	emptyFieldsStr = "";
	
	for (i=0; i<checkedForm.elements.length; i++) {
		field = checkedForm.elements[i];
		if (field.className.indexOf("required") != -1) {
			if (isEmpty(field)) {
				emptyFieldsStr += "\n- " + field.name.replace(/_/g, " ").replace(/\[/g, "").replace(/\]/g, "");
			}
		}
		if (field.name == "repository_name[]") {
			// check if the repository name is correct
			if (field.value) {
				repoCount++;
				repoNamePattern = /^[0-9a-z_]{3,32}$/;
				if (!field.value.match(repoNamePattern)) {
					invalidRepoName = true;
				}
			}
		}
	}

	invalidPassword = false;
	invalidUsername = false;
	userCount = 0;
	
	if (formId == "signup") {	
		//	signup form: check the user names and passwords
		for (i=1; i<=20; i++) {
			var user = document.getElementById("user_name" + i).value;
			var password = document.getElementById("user_password" + i).value;
			if (user != "") {
				userCount++;
				if  (password.indexOf(user) > -1 || // username is (part of) the password
					(!password.match(/\W+/) && !password.match(/\d+/)) || // no digits or other special characters included
					password.length < 5 // too short
					) {
					invalidPassword = true;
				}
				usernamePattern = /^[0-9a-z_]{3,16}$/;
				if (!user.match(usernamePattern)) {
					invalidUsername = true;
				}
			}
		}
	}
	
	errorText = "";
	if (emptyFieldsStr) {
		errorText += "One or more required fields were missing:" + emptyFieldsStr + "\n\n";
	}
	
	if (invalidRepoName) {
		if (repoCount == 1) {
			errorText += "The repository name is invalid.\n";
		}
		else {
			errorText += "Some repository names are invalid.\n";
		}
		errorText += "A repository name can contain lower case characters, numbers and the character '_' and is between 3 and 32 characters long.\n\n";
	}

	if (invalidUsername) {
		if (userCount == 1) {
			errorText += "The username is invalid.\n";
		}
		else {
			errorText += "Some usernames are invalid.\n";
		}
		errorText += "A username can contain lower case characters, numbers and the character '_' and is between 3 and 16 characters long.\n\n";
	}

	if (invalidPassword) {
		if (userCount == 1) {
			errorText += "The password you supplied is not safe enough,\n";
		}
		else {
			errorText += "Some of the passwords you supplied are not safe enough,\n";
		}
		errorText += "a safe password:\n";
		errorText += "- contains 5 or more characters\n";
		errorText += "- contains at least one digit (0-9) or special character (.,-+# etc.)\n";
		errorText += "- does not equal or include the username\n\n";
	}

	if (errorText) {
		errorText += "Please check the input and resend your request.";		
		alert (errorText);
		
		if (formId == "signup") {
			//	Show the right section of the signup form
			if (emptyFieldsStr) {
				// some contact info missing, go to contact page
				signupShowStep(2);
			}
			else if (invalidRepoName || invalidUsername || invalidPassword) {
				// incorrect user or repo info, go to there
				signupShowStep(3);
			}
		}
		return false;	
	}
	
	//	Nothing wrong
	return true;
}
