$(document).ready(function(){
    if($('.feature .switch').length>0){
        $('.feature .switch').cycle({
            fx: 'scrollUp',
            speed: 500,
            timeout: 8500
        });
    }
//popup map hide
$('div map area.close').click(function(){
	$(this).parent().parent().fadeOut();
	return false;
});
	//pop up
	/*$(window).bind("load",function(){
		if($('#popup').length>0){
			bVer=jQuery.browser.version;
			if(bVer!='6.0' && bVer!='7.0'){
				if(!readCookie('vPop')){
					$('.pMaskC')
					.animate({opacity:0},{duration:0,queue:false}).css({display:'block'})
					.children('.maskC').animate({opacity:.8},{duration:0,queue:false});
					$('.pMaskC').children('.popC').html($('#popup').html()).centreElm({aniSpeed:0,easing:'linear'});
					$('.pMaskC').animate({opacity:1},{duration:300,queue:false});
					
					$('.popC .closeW').click(function(){
						$('.pMaskC').animate({opacity:0},300,function(){
							$(this).css({display:'none'});
							createCookie('vPop','1');
						});
						return false;
					});
				}
			}
		}								   
	});*/

	
	//stnwa boy now button
	$('.stnwa_buynow').click(function(){
		 $.ajax({
		   type: "POST",
		   url: "media/php/process.php",
		   data: "func=stnwaBuy",
		   success:function(data){
			   //alert(data);
			   window.location=data;
			}
		 });
		return false;								  
	});
	
	if($('.testM').length>0){
		$('.testM').corner('5px');
	}

    $('#signupform input[type="text"], #signupform textarea, #contactregister input[type="text"], \n\
    #contactregister textarea').focus(function(){
       $(this).css({border:'1px solid #003E61',backgroundColor:'#F1F6F9'});
        thsObj=$(this);
        thsNme=thsObj.attr('name');
        thsVal=thsObj.val();
        if(ValidateInput(thsVal,thsNme)){thsObj.css({backgroundColor:'#B2FEB7'});}
        else{thsObj.css({backgroundColor:'#FEB8B8'});}
    }).blur(function(){
        //The Pelican Innaaa-norcap.org.uk$(this).css({border:'1px solid #97BBFD',backgroundColor:'#fff'});
    }).keyup(function(){
        thsObj=$(this);
        thsNme=thsObj.attr('name');
        thsVal=thsObj.val();
        if(ValidateInput(thsVal,thsNme)){thsObj.css({backgroundColor:'#B2FEB7'});}
        else{thsObj.css({backgroundColor:'#FEB8B8'});}
    });

    $('#signupform').submit(function(){
        return false;
    });

    $('#contactregister').submit(function(){
        hidVal1=$('#contactregister input[type="hidden"]').val();
        hidVal2=$('#contactregister input[type="hidden"]').val();

        if(hidVal1=="" && hidVal2==""){
            pId=$('#signupform select').eq(0).val();
            $('#contactregister #returntxt').text("Processing...");

            if(ValidateFormImputs()){
                 $.ajax({
                   type: "POST",
                   url: "media/php/crProcess.php",
                   data: "func=snd&pid="+pId+"&frmVals="+returnFormVas(),
                   success: function(data){
                       $('#signupform input[type="text"], #signupform textarea,#contactregister input[type="text"], #contactregister textarea').val("");
                       window.location=data;
                   }
                 })
            }else{
              $('#contactregister #returntxt')
              .text("Please ensure you have filled in all the mandatory fields correctly and agreed with the conditions of the form.");
            }
        }
        return false;
    })

});

//javascript functions
function returnFormVas(){
   expArr="";
   $('#signupform input[type="text"]').each(function(){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";});
   $('#signupform textarea').each(function(){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";});
   $('#signupform select').each(function(){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";});
   $('#signupform input[type="radio"]').each(function(){if(this.checked){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";}});

   $('#contactregister input[type="text"]').each(function(){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";});
   $('#contactregister textarea').each(function(){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";});
   $('#contactregister select').each(function(){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";});
   $('#contactregister input[type="radio"]').each(function(){if(this.checked){thsVal=$(this).val();expArr=expArr+thsVal+"{spl}";}});
   return expArr;
}
function ValidateFormImputs(){
    returnBool1=true;
    returnBool2=true;
    $('#signupform input[type="text"]').each(function(){
        thsVal=$(this).val();
        thsNme=$(this).attr('name');
        if(!ValidateInput(thsVal,thsNme)){returnBool1=false;}
    });
    $('#contactregister input[type="text"]').each(function(){
        thsVal=$(this).val();
        thsNme=$(this).attr('name');
        if(!ValidateInput(thsVal,thsNme)){returnBool2=false;}
    });
   if($('#contactregister input[type="checkbox"]')
   .attr('checked')){returnBool3=true}else{returnBool3=false;}
    if(returnBool1 && returnBool2 && returnBool3){return true;}
    else{return false;}
}

function ValidateInput(inputStr, valType){
    vType=['email','number','phone','input'];
    if(valType=="noval"){
        return true;
    }
    else{
        expBool=false;
        for(i=0;i<=vType.length-1;i++){
           if(valType==vType[i]){
               if(vType[i]=="email"){if(CheckEmail(inputStr)){expBool=true;}else{expBool=false;}}
               else if(vType[i]=="number"){if(isPhone(inputStr)){expBool=true;}else{expBool=false;}}
               else if(vType[i]=="phone"){if(isPhone(inputStr)){expBool=true;}else{expBool=false;}}
               else if(vType[i]=="input"){if(inputStr!=""){expBool=true;}else{expBool=false;}}
           }
        }
        return expBool;
    }
}

function CheckEmail(inputemail) {
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");
	if (AtPos == -1 || StopPos == -1){return false;}
	else{return true;}
}

function IsNumeric(strString){
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function isPhone(inpStr){
  pros1=inpStr.replace(" ","");
  if(pros1!="" && pros1.length>=11 && IsNumeric(pros1)){return true;}else{return false;}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}