/****************************************************************************
 * Filename: scripts.js
 *
 * @author Simon Jensen
 ****************************************************************************/

// check for valid numeric strings
function isNumeric(strString) {
    var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;
    
    if (strString.length == 0)
        return false;
    
    // test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++)
    {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1)
        {
            blnResult = false;
        }
    }
    return blnResult;
}

/***************************************************************************/

/* Check if an email if well formed: user@host.xxx */
function checkEmail(mail) {
	var apos = mail.indexOf("@");
	var dotpos = mail.lastIndexOf(".");
	
	if (apos<1||dotpos-apos<2) {
		return false;
	} else {
		return true
	}
}

/***************************************************************************/

/* Check newsletter signup form */
function check_newsletter(form) {
	var errors = "";
	var email = form.email.value;
	var name = form.name.value;
	var signup = 'true';
	var list = form.list.value;
	//$("input[@name='signup']:checked").val();
	
	if(name == "") {
		errors += "You need to tell us Your country.\n\n";
	}
	if(email == "") {
		errors += "You need to tell us Your e-mail.\n\n";
	} else {
		if(!checkEmail(email)) {
			errors += "Your e-mail needs to have the following format: bruger@host.xyz\n\n";
		}
	}
	
	if(errors == "") {
		$("#loading").show();
		$.post('/admin/themes/strongmind/form_newsletter.php', {
			newsletter: 'ajax',
			name: name,
			email: email,
			list: list,
			signup: signup
		},
		function(data) {
			$("#newsletter_response").html(data);
			$("#loading").hide();
		});
	} else {
		alert(errors);
	}
}

/***************************************************************************/
 
/* Check subscribtion form */
function sendMail(form) {
	var errors = "";
	var email = form.email.value;
	var navn = form.navn.value;
	var virksomhed = form.virksomhed.value;
	var telefon = form.telefon.value;
	var besked = form.besked.value;
	$("#loading").show();
	if(navn == "") {
		errors += "You need to tell us Your country.\n\n";
	}
	if(email == "") {
		errors += "You need to tell us Your e-mail.\n\n";
	} else {
		if(!checkEmail(email)) {
			errors += "Your e-mail needs to have the following format: bruger@host.xyz\n\n";
		}
	}
	if(telefon == "") {
		errors += "Du skal oplyse dit telefon nr.\n\n";
	}
	
	if(errors == "") {
		$.post('/admin/themes/strongmind/form_contact.php', {
			contact: 'ajax',
			navn: navn,
			email: email,
			telefon: telefon,
			virksomhed: virksomhed,
			besked: besked
		},
		function(data) {
			if(data == "") {
				alert(data);
			} else {
				alert(data);
			}
		});
	} else {
		$("#loading").hide();
		alert(errors);
	}
}

/***************************************************************************/

$(document).ready(function() {
	var rotate_placeholder_2 = setInterval(function() {
		$.post("/admin/themes/strongmind/ajax_foredragsholdere.php", {
			extra_id: $("#extra_id").html()
		},
		function(data) {
			if(data.err == "0") {
				$("#extra_id").html(data.id);
				
				$("#box_title_l").fadeOut("fast", function() {
					$("#box_title_l").html(data.title).fadeIn("fast");
				});
				
				$("#box_content_l").fadeOut("fast", function() {
					$("#box_content_l").html(data.content).fadeIn("fast");
				});
			}
		},"json");
	}, 15000);
	
	var rotate_placeholder_3 = setInterval(function() {
		$.post("/admin/themes/strongmind/ajax_referencer.php", {
			extra_id: $("#extra_id_r").html()
		},
		function(data) {
			if(data.err == "0") {
				$("#extra_id_r").html(data.id);
				
				$("#box_content_r").fadeOut("fast", function() {
					$("#box_content_r").html(data.content).fadeIn("fast");
				});
			}
		},"json");
	}, 15000);
});

/***************************************************************************/
