function check_field(form)
{
    var complete = true;
    with(form)
    {
        if(username.value.length < 6 || username.value.length > 16)
        {
            complete = false;
            username.style.backgroundColor="#FF6A6A";
            alert("Please fill out a proper name .\n");
	    username.focus();
     
        }
        else
            username.style.backgroundColor="#FFFFFF";
		
		if(name.value.length < 6 || name.value.length > 50)
	    {
	        complete = false;
	        name.style.backgroundColor="#FF6A6A";
	        alert("Your name should be between 6 and 30 characters .\n");
		name.focus();
        
	    }
	    else
	        name.style.backgroundColor="#FFFFFF";
        
        if(password.value.length < 6 || password.value.length > 16 || password.value =="123456" || password.value =="654321" || password.value =="abc123" || password.value =="abcd123" || password.value =="1234abcd" || password.value =="123abc" || password.value =="111111" || password.value =="qwerty" || password.value =="000000" || password.value =="abc1234")
        {
            complete = false;
            password.style.backgroundColor="#FF6A6A";
			alert("Please fill out your password properly.\n");
			password.focus();
        }
        else
            password.style.backgroundColor="#FFFFFF";
    
		
		
		
        if(contact.value.length <10 || contact.value.length > 14)
        {
            complete = false;
            contact.style.backgroundColor="#FF6A6A";
	    alert("Please fill out your contact no properly.\n");
	    contact.focus();
        }

        else if (checkPhone(contact.value))
            contact.style.backgroundColor="#FFFFFF";
		
	else 
	{
		complete = false;
		contact.style.backgroundColor="#FF6A6A";
		alert("Please fill out your contact no properly.\n");
		contact.focus();
	}
       
        if(email.value.length < 10)
        {
            complete = false;
            email.style.backgroundColor="#FF6A6A";
	    alert("Please fill out your email properly.\n");
	    email.focus();
        }
		
	else if(echeck(email.value))
	{
		email.style.backgroundColor="#FFFFFF";
	}
		
	else
	{
		complete = false;
		email.style.backgroundColor="#FF6A6A";
		alert("Please fill out your email properly.\n");
		email.focus();
		
	}
	
	if ($(".checkbox input:checked").val() != "Yes") {
		complete = false;
		alert("Please read the Terms and conditions.\n");
	}
	
    }
    return complete;
}


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	


// 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 = 10;

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;
    var 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;
    var 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 checkPhone(strPhone)
{
	var bracket=3
	strPhone=trim(strPhone)
	
	if(strPhone.indexOf("+")>1) return false;
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1;
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false;
													   
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false;
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false;
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}


function ValidateForm(){
	var Phone=document.frmSample.txtPhone
	
	if ((Phone.value==null)||(Phone.value=="")){
		alert("Please Enter your Phone Number")
		Phone.focus()
		return false
	}
	if (checkInternationalPhone(Phone.value)==false){
		alert("Please Enter a Valid Phone Number")
		Phone.value=""
		Phone.focus()
		return false
	}
	return true
 }


function overlay(El) {
	Shadow = $("#shadow");
	Overlay = $("#overlay");
	Shadow.css({
		height: $(document).height(),
		width: $(document).width()
	}).show();
	Overlay.css({
		left: getMiddleWidth()
	}).show();
	El.show("500");
	$('html,body').animate({scrollTop: "120"}, 300);
}

function getMiddleWidth() {
	Wwidth = $(window).width();
	Ewidth = 430;
	
	return (Wwidth/2)-(Ewidth/2);
}

function overlayHide() {
	$("#overlay").hide();
	$("#overlay > div").hide();
}

function toggleLoading() {
	$("#loading").toggle();
}

function changeActiveSteps(El) {
	$("#setup .steps li").removeClass("active");
	El.addClass("active");
}

function slideSwitch(index) {	
	index = typeof(index) != 'undefined' ? index : -1;
	
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    if (index != -1) {
		var $next = $("#slideshow img").eq(index);
	} else {
		var $next =  $active.next().length ? $active.next()
	        : $('#slideshow img:first');
	}

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
	
	// Change nav icon
	var $active_nav = $('#slideshow_nav li a.active');

    if ( $active_nav.length == 0 ) $active_nav = $('#slideshow_nav li a:last');

    if (index != -1) {
		var $next_nav = $("#slideshow_nav li").eq(index).find("a");
	} else {
		var $next_nav =  $active_nav.parent().next().find("a").length ? $active_nav.parent().next().find("a")
	        : $('#slideshow_nav li a:first');
	}

    $active_nav.removeClass('active');
	$next_nav.addClass('active');
}

$(function() {
	// Register overlay
	$(".register_link").click(function() {
		overlay($("#overlay_register"));
		return false;
	});
	
	// Login overlay
	$("#login_link").click(function() {
		overlay($("#overlay_login"));
		return false;
	});
	
	// Close login window and open register window
	$("#register_mini_link").click(function() {
		$("#overlay_login").hide("500");
		$("#overlay_register").show("500");
		return false;
	});
	
	// Contact overlay
	$("#contact_link").click(function() {
		overlay($("#overlay_contact"));
		return false;
	});
	
	// Click on shadow to close
	$("#shadow").click(function() {
		$(this).hide();
		overlayHide();
	});
	
	// Change class to active inputs
	$("form input, form textarea").focus(function() {
		Parent = $(this).parent();
		Parent.addClass("active");
	}).blur(function() {
		Parent = $(this).parent();
		Parent.removeClass("active");
	});
	
	$("#setup .steps li a.title_image").click(function() {
		toggleLoading();
		
		This = $(this);
		Parent = This.parent();
		Cont = $("#setup_content");
		Cont.hide("200");
				
		Cont.load(This.attr("href")+" ol", {}, function() {
			changeActiveSteps(Parent);
			Cont.show("500");
			toggleLoading();
		})
		
		return false;
	});
	
	// Slideshow
	if( $("#slideshow").size() > 0) {
		slide_interval = setInterval( "slideSwitch()", 3500 );
		
		$('#slideshow_nav li a').click(function() {
			$this = $(this);
			$index = $("#slideshow_nav li").index($this.parent());
			slideSwitch($index);
			clearTimeout(slide_interval);
			return false;
		});
	}
	
	$("form input[type=reset]").click(function() {
		$(this).parent().parent().find("input").each(function() {
			This = $(this);
			
			if (This.attr("type") == "text" || This.attr("type") == "password") {
				This.css({
					backgroundColor: "#ffffff"
				});
			}
		});
		$(this).parent().parent().find("textarea").each(function() {
			$(this).css({
				backgroundColor: "#ffffff"
			});
		});
	});
	
	// Form validation
	$("#register_form").submit(function() {
		return check_field(this);
	});
	$("#login_form").submit(function() {
		complete = true;
		
		with (this) {
			if (username.value.length < 6) {
				complete = false;
				username.style.backgroundColor="#FF6A6A";
		    	alert("The username should be longer than 6 characters.\n");
		    	username.focus();
			} else if(username.value.length > 16) {
				complete = false;
				username.style.backgroundColor="#FF6A6A";
		    	alert("The username should be shorter than 16 characters.\n");
		    	username.focus();
			}
			if (password.value.length < 6) {
				complete = false;
				password.style.backgroundColor="#FF6A6A";
		    	alert("The password should be longer than 6 characters.\n");
		    	password.focus();
			} else if(password.value.length > 16) {
				complete = false;
				password.style.backgroundColor="#FF6A6A";
		    	alert("The password should be shorter than 16 characters.\n");
		    	password.focus();
			}
			
			return complete;
		}
	});	
	$("#contact_form").submit(function() {
		complete = true;
		
		with (this) {
			if(email.value.length < 10) {
	            complete = false;
	            email.style.backgroundColor="#FF6A6A";
		    	alert("Please fill out your email properly.\n");
		    	email.focus();
			} else if(echeck(email.value)) {
				email.style.backgroundColor="#FFFFFF";
			} else {
				complete = false;
				email.style.backgroundColor="#FF6A6A";
				alert("Please fill out your email properly.\n");
				email.focus();		
			}
			
			if (message.value.length < 10) {
				complete = false;
	            message.style.backgroundColor="#FF6A6A";
		    	alert("Please fill out a longer message.\n");
		    	message.focus();
			} else {
				message.style.backgroundColor="#FFFFFF";
			}
		}
		
		return complete;
	});
	
	// News archive
	$("#archive_link").click(function() {
		This = $(this);
		Href = This.attr("href");
		This.replaceWith('<img src="images/loading.gif" alt="Loading..." id="loading_gif" />');
		$.get(Href, {}, function(data, txt) {
			$("#loading_gif").hide();
			$("#news_list").append(data);
		})
		return false;
	});
	
	// Rounded Corners
	$("#setup .steps li").corner("4px");
});
