/** scripts.js
 */

function showSample(src) {
	document.getElementById('showedsample_image').src = src;
}

function checkUsername(username) {
	if (username == "") return;
	//chk = window.open('check_username.php?username='+username+'', 'checkusername','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=275,height=80');
	//chk.moveTo(500, 500);
	document.forms["regForm"].action = "index.php?checkusername=1";
	document.forms["regForm"].submit();
}

function openWindow(pageToOpen) {
	window.open(pageToOpen, 'page','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=500');
}

function openWindowContact(pageToOpen) {
	window.open(pageToOpen, 'page','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=833,height=574');
}

function openWindowWH(pageToOpen, width, height) {
	window.open(pageToOpen, 'page','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width='+width+',height='+height+'');
}

function openTellAFriend(url) {
	window.open('../adcomlive/share/index.php?link=' + escape(url), 'page','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=500');
}

function showTellAFriend(url) {
	$('tell-a-friend-form').style.display = 'block';
	//window.open('../adcomlive/share/index.php?link=' + escape(url), 'page','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=550,height=500');
}

function tellAFriend(name, from_email, from_id, to_name, to_email, comment, url) {
	var data = 'q=tellafriend&name=' + name + '&from_email=' + from_email + '&from_id=' + from_id + '&to_name=' + to_name + '&to_email=' + to_email + '&comment=' + comment + '&url=' + escape(url);
	$('tell-a-friend-form').innerHTML = '<span class="header">Sending mail...</span>';
	var myAjax = new Ajax.Updater("tell-a-friend-form", "ajaxFeed.php",
	{method: 'post',
	parameters: data,
	encoding: 'UTF-8',
	onFailure: tellAFriendError,
	onSuccess: tellAFriendSuccess});
}

function tellAFriendError() {
	$('tell-a-friend-form').innerHTML = 'Something went wrong. Please try again.';
}

function tellAFriendSuccess() {
	$('tell-a-friend-form').innerHTML = 'Mail was successfully sent!';
}

// returns true if at least one category is checked
function checkCategories(suffix) {
	for (var i = 1; i <= categories; i++) {
		if (document.getElementById(suffix + i + "").checked == true) {
			return true;
		}
	}
	return false;
}

function validateForm() {
	alert(document.forms["regForm"].username.value);
	return;
	if (document.all.firstname.value == "") {
		alert("Please fill in first name.");
		return false;
	}
	if (document.all.lastname.value == "") {
		alert("Please fill in first last name.");
		return false;
	}
	
	if (document.all.username.value == "") {
		alert("Please fill in user ID.");
		return false;
	}

	if (document.all.password.value == "") {
		alert("Please fill in password.");
		return false;
	}

	/*if (!checkCategories("category_search_")) {
		alert("Var vnlig och kryssa fr vilka typer av jobb du sker!");
		return false;
	}

	if (document.all.portfolio_desc_1.value == "") {
		alert("Var vnlig och beskriv arbetsprov nr 1!");
		return false;
	}

	if (!checkCategories("category_job1_")) {
		alert("Var vnlig och kryssa fr kategorier till arbetsprov 1!");
		return false;
	}

	if (document.all.portfolio_desc_2.value == "") {
		alert("Var vnlig och beskriv arbetsprov nr 2!");
		return false;
	}

	if (!checkCategories("category_job2_")) {
		alert("Var vnlig och kryssa fr kategorier till arbetsprov 2!");
		return false;
	}

	if (document.all.portfolio_desc_3.value == "") {
		alert("Var vnlig och beskriv arbetsprov nr 3!");
		return false;
	}

	if (!checkCategories("category_job3_")) {
		alert("Var vnlig och kryssa fr kategorier till arbetsprov 3!");
		return false;
	}
	*/
	if (document.all.email.value == "") {
		alert("Please fill in mail address");
		return false;
	}

	if (document.all.street.value == "") {
		alert("Var vnlig och fyll i gatuadress!");
		return false;
	}

	if (document.all.zipcode.value == "") {
		alert("Var vnlig och fyll i postnummer!");
		return false;
	}
	
	if (document.all.city.value == "") {
		alert("Var vnlig och fyll i ort!");
		return false;
	}

	/*if (document.all.orgNumber.value == "") {
		alert("Var vnlig och fyll i personnummer/orgnummer!");
		return false;
	}*/

	if (document.all.conditions.checked == false) {
		alert("Var vnlig ls och acceptera avtalet!");
		return false;
	}

	return true;
}

function validateAndSubmitForm() {
	if (document.forms["regForm"].firstname.value == "") {
		alert("Var vnlig och fyll i frnamn!");
		return;
	}
	if (document.forms["regForm"].lastname.value == "") {
		alert("Var vnlig och fyll i efternamn!");
		return;
	}
	
	if (document.forms["regForm"].city.value == "") {
		alert("Var vnlig och fyll i ort!");
		return;
	}

	if (document.forms["regForm"].email.value == "") {
		alert("Var vnlig och fyll i en mailadress!");
		return;
	}

	if (document.forms["regForm"].username.value == "") {
		alert("Var vnlig och fyll anvndarnamn!");
		return;
	}

	if (document.forms["regForm"].password.value == "") {
		alert("Var vnlig och fyll ett lsenord!");
		return;
	}
	
	if (document.forms["regForm"].conditions.checked == false) {
		alert("Var vnlig ls och acceptera avtalet!");
		return;
	}

	document.forms["regForm"].submit();
}
