$(function () {
	$.unobtrusivelib();
	prettyPrint();
	$("div.homepage").carousel({
        autoSlide: true,
        autoSlideInterval: 4000,
        delayAutoSlide: 0,
        loop: true}
    );
	$("div.pola").carousel({
        autoSlide: true,
        autoSlideInterval: 3000,
        delayAutoSlide: 3000,
        loop: true,
        effect: "fade"}
    );
    $('.lightbox_close').click(function () {
        $('.addthis_toolbox').fadeOut('fast');
        return false;
    });
    $('.callback').click(function () {
        $('.addthis_toolbox').fadeIn('fast');
        return false;
    });
});

function echeck(str) {
    var at = "@",
        dot = ".",
        lat = str.indexOf(at),
        lstr = str.length,
        ldot = str.indexOf(dot);
	if (str.indexOf(at) == -1) {
	   alert("Email error : You forgot the '@'");
	   return false;
    }
	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
	   alert("Email error : your address can't start with '@'");
	   return false;
	}
	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr){
	    alert("Email error : You forgot the '.' in your address");
	    return false;
	}
    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Email error : You only need one @ in your address");
        return false;
    }
    if (str.substring(lat-1, lat) == dot || str.substring(lat+1, lat+2) == dot) {
        alert("Email error : You need the domain name between the '@' and the '.'");
        return false;
    }
    if (str.indexOf(dot, (lat+2)) == -1) {
        alert("Email error : The '@' must be placed before the '.'");
        return false;
    }
    if (str.indexOf(" ") != -1) {
        alert("Email error : Please do not use any spaces");
        return false;
    }
    return true;
}

function erase1() {
	if (document.enquiry.phone.value=="(+country code) phone number") {
		document.enquiry.phone.value = "";
		return false;
	}
}

/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 8;

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s) {
    var i,
        returnString = "";
    // Search through string's characters one by one. /If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") {
            returnString += c;
        }
    }
    return returnString;
}

function stripCharsInBag(s, bag) {
    var i,
        returnString = "";
    // Search through string's characters one by one. /If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) {
            returnString += c;
        }
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
    var bracket = 3,
        strPhone = trim(strPhone),
        s = stripCharsInBag(strPhone,validWorldPhoneChars),
        brchr = strPhone.indexOf("(");
    if (strPhone.indexOf("+") > 1) {
        return false;
    }
    if (strPhone.indexOf("-") != -1) {
        bracket = bracket + 1;
    }
    if (strPhone.indexOf("(") != -1 && strPhone.indexOf("(") > bracket) {
        return false;
    }
    if (strPhone.indexOf("(") != -1 && strPhone.charAt(brchr+2) != ")") {
        return false;
    }
    if (strPhone.indexOf("(") == -1 && strPhone.indexOf(")") != -1) {
        return false;
    }
    return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// VALID BOOKING FORM
function validation(){
	var emailID=document.enquiry.email,
        Phone=document.enquiry.phone;

	if (document.enquiry.name.value == "") {
		alert('Please input your name');
		document.enquiry.name.focus();
		document.enquiry.name.select();
		return false;
	}
	if ((emailID.value == null)||(emailID.value == "")) {
		alert('we need your email address');
		emailID.focus();
		emailID.select();
		return false;
	}
	if (echeck(emailID.value) == false) {
		emailID.focus();
		emailID.select();
		return false;
	}
	/*if(document.enquiry.phone.value=="")
		{
		alert('Please input your phone number');
		document.enquiry.phone.focus()
		document.enquiry.phone.select()
		return false;
		}*/

	if ((Phone.value == null)||(Phone.value == "") ) {
		alert("Please Enter your Phone Number");
		Phone.focus();
		return false;
	}
	if (checkInternationalPhone(Phone.value)==false) {
		alert("Please Enter your phone number in the following format: +44 (0) 203 004 8240");
		Phone.value = "";
		Phone.focus();
		return false;
	}

	if (document.enquiry.message.value=="") {
		alert('Please input your message');
		document.enquiry.message.focus();
		document.enquiry.message.select();
		return false;
	}
}
