// JavaScript Document
var base = "http://www.castrike.com";
function iconText(icon) {
	if(icon=="fb") {
		$(".fb").addClass('icon_highlight');
		$(".twt").removeClass('icon_highlight');
		$(".icon_text").html("find me on Facebook!");
	} else if(icon=="twt") {
		$(".twt").addClass('icon_highlight');
		$(".fb").removeClass('icon_highlight');
		$(".icon_text").html("follow me on Twitter!");
	}
}

function submitContact() {
		var name = $("#name").val();
		if(name.length == 0) { errorInForm("please enter a valid name"); return; }
		var email = $("#email").val();
		if(email.length == 0 || !valemail(email)) {  errorInForm("please enter a valid email"); return; }
		var phone = $("#phonenumber").val();
		if(phone!=null || phone.length > 0 && phone.length<12 ) { 
			phone = phone.replace(/[-]/g,"");
			if(isNaN(phone)) { errorInForm("please enter a valid phone number"); return; }
		}
		var subject = $("#subject").val();
		if(subject.length == 0) { errorInForm("please enter a valid subject"); return; }
		var message = $("#message").val();
		if(message.length == 0) { errorInForm("please enter a valid message"); return; }
		if(confirm("sending!")) posInForm("email was sent sent"); 
		else errorInForm("email could not be sent"); 
		
}

function loadImg(i) {
	var main = $("#main_pic").attr("src");
	main = main.substring(main.lastIndexOf("/")+1);
	var current = $("#pic_"+i).attr("src");
	current = current.substring(current.lastIndexOf("/")+3);
	//alert("MAIN: "+main+" CURRENT: "+current+" BASE: "+base);
	$("#main_pic").attr("src",base+"/proj_images/"+current);
	$("#pic_"+i).attr("src",base+"/proj_images/th"+main);
	
}

function login() {
	var user = $("#un").val();
	if(user.length == 0) { errorInForm("please enter a valid username"); return; }
	var pass = $("#pw").val();
	if(pass.length == 0) { errorInForm("please enter a valid password"); return; }
	if(confirm("logining!")) posInForm("user authenticated"); 
	else errorInForm("login could not be processed"); 
}

function errorInForm(message) {
	$("#response").addClass("bad");
	$("#response").text(message);
	setTimeout( function() { $("#response").html("&nbsp;"); $("#response").removeClass("bad"); }, 5000 );
}

function posInForm(message) {
	$("#response").addClass("good");
	$("#response").text(message);
	setTimeout( function() { $("#response").html("&nbsp;"); $("#response").removeClass("good"); }, 5000 );	
}


function valemail(em) {
	if(em.indexOf('@')>0 && em.indexOf('@')<em.length-1 ) {
		em = em.substr(em.indexOf('@')+1);
		if(em.lastIndexOf('.')>0) return true;
	}
	return false;
}

