$(document).ready(function () {
	$('#shareSendButton').live('click',checkForm);
});

function openWin(theURL,winName,features) {
  window.open(theURL,winName,features);
}

	    function checkForm()
	    {
    var status= false;
		var illegalChars= /[\(\)\<\>\;\:\\\"\[\]]/;
		var illegalFileChars= /[\(\)\<\>\;\"\[\]]/;
		var emailFilter=/^[^\.@](\.?[^\.\s@])*@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,7}$/;
		var mutli_invi = document.getElementsByName("invitationtype[]");

		for(i=0;i<mutli_invi.length;i++)
		{
		    if(mutli_invi[i].checked==true){
			status=true;
			break;
		    }
		}
		
		//Checks  name is not empty or using illegal characters//
		//////////////////////////////////////////////////////////////
		if (document.getElementById("name").value == "")
		{
		    alert ("You didn't enter your name.\n");
		    return false;
		}

		else if (document.getElementById("name").value.match(illegalChars))
		{
		    alert ("Your  name contains illegal characters.\n");
		    return false;
		}

		//Checks for valid email address//
		/////////////////////////////////
		if (document.getElementById("email").value == "")
		{
		    alert ("You didn't enter an email address.\n");
		    return false;
		}
		else if (!(emailFilter.test(document.getElementById("email").value)))
		{
		    alert ("Please enter a valid email address.\n");
		    return false;
		}
		else
		{
		    if (document.getElementById("email").value.match(illegalChars))
		    {
			alert ("The email address contains illegal characters.\n");
			return false;
		    }
		}
		//Checks zip is not empty or using illegal characters//
		/////////////////////////////////////////////////////////////
		if (document.getElementById("zip").value == "")
		{
		    alert ("You didn't enter your zip code.\n");
		    return false;
		}

		else if (document.getElementById("zip").value.match(illegalChars))
		{
		    alert ("Your zip code contains illegal characters.\n");
		    return false;
		}

		if(document.getElementById("regioncheck").value == "yes"){
		    if (document.getElementById("region_select").value == "")
		    {
			alert ("Please select your region.\n");
			return false;
		    }
		}
		//Checks agree to terms and conditions//
		///////////////////////////////////////
		if (document.getElementById("termsBox").checked != true)
		{
		    alert ("You must agree to the terms and conditions.\n");
		    return false;
		}

                if (document.getElementById("recipient_list").value.length == 0 || status == true)
                {
                    //////////////////////////////////////////////////////////////
                    var mutli_rname = document.getElementsByName("rname[]");

                    for(i=0;i<mutli_rname.length;i++)
                    {
                        if (mutli_rname[i].value == "")
                        {
                            alert ("You didn't enter your Recipient name.\n");
                            mutli_rname[i].focus();
                            return false;
                        }
                        else if (mutli_rname[i].value.match(illegalChars))
                        {
                            alert ("Your recipient name contains illegal characters.\n");
                            mutli_rname[i].focus();
                            return false;
                        }

                    }
                    //// validate recipient email id's

                    var mutli_remail = document.getElementsByName("remail[]");

                    for(i=0;i<mutli_remail.length;i++)
                    {

                        if (mutli_remail[i].value == "")
                        {
                            alert ("Please enter a recipient email address.\n");
                            mutli_remail[i].focus();
                            return false;
                        }
                        else if (!(emailFilter.test(mutli_remail[i].value)))
                        {
                            alert ("Please enter a valid recipient email address.\n");
                            mutli_remail[i].focus();
                            return false;
                        }
                        else
                        {
                            if (mutli_remail[i].value.match(illegalChars))
                            {
                                alert ("The recipient email address contains illegal characters.\n");
                                mutli_remail[i].focus();
                                return false;
                            }
                        }
                    }                    
                }
					//var url = 'http://sunshine.visitflorida.com/share/send.php?'+$('#shareForm').serialize();
					var url = '/share/send.php';
					var params = $('#shareForm').serialize();
					console.log('post: '+url+'?'+params);

         $.post(url,params,function(resp){
           if(resp.indexOf('invite_id') > 0)
           {
             var inviteId = resp.replace(/^.+invite_id=([^&]+).*/i,'$1');
             window.location.href='/share/invite-confirmation.php?invite_id='+inviteId;
           }
           else
           {
             alert(resp);
           }
                
						//$('#shareSubmitButton').click();

         });
         return true;
	    }
	    /// function add recipients
	    function addElement() {
		var ni = document.getElementById('RecipFields');
		var numi = document.getElementById('theValue');
		var num = (document.getElementById('theValue').value -1)+ 2;
		//alert(num)
		if(num < 5){
		    // cheking the max recients count
		    numi.value = num;
		    //alert(numi.value)
		    //var newdiv = document.createElement('div');
		    var divIdName = 'my'+num+'Div';

		    document.getElementsByName("remail[]")[num].value='';
		    document.getElementsByName("rname[]")[num].value='';
		    document.getElementById(divIdName).style.display='block';
		}else{
		    alert("Sorry, only five recipients per invitation. Please send another when you're finished to invite more than five people");
		}
	    }
	    //function to remove recipients
	    function removeElement() {
		var ni = document.getElementById('RecipFields');
		var numi = document.getElementById('theValue');
		var numd = document.getElementById('theValue').value;

		if(numd >= 1){
		    // cheking the max recients count
		    numd = (numi.value);
		    numi.value=numd-1;
		    var divIdName = 'my'+numd+'Div';

		    document.getElementsByName("remail[]")[numd].value='z@z.com';
		    document.getElementsByName("rname[]")[numd].value='z';
		    document.getElementById(divIdName).style.display='none';
		}

	    }

	    // function to select single check box

	    function check(str){
		var mutli_invi = document.getElementsByName("invitationtype[]");
		for(i=0;i<mutli_invi.length;i++)
		{
		    if(i!=str){
			mutli_invi[i].checked=false
		    }else{
			mutli_invi[i].checked=true
		    }
		}
	    }
	    // change the region value
	    function change(str){
		document.getElementById("region").value=str
	    }

	    var xmlhttp

	    function showHint(str)
	    {

		xmlhttp=GetXmlHttpObject();
		if (xmlhttp==null)
		{
		    alert ("Your browser does not support XMLHTTP!");
		    return;
		}
		var url="dealscalculation.php";
		url=url+"?zipcode="+str;
		url=url+"&sid="+Math.random();
		xmlhttp.onreadystatechange=stateChanged;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	    }

	    function stateChanged()
	    {
		displayStaticMessage('<img src="images/ajax-loader.gif">Processing the data',false);
		if (xmlhttp.readyState==4)
		{
		    if(xmlhttp.responseText){
			//document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
			document.getElementById("regionshow").style.display='block';
			document.getElementById("regioncheck").value='yes'
		    }else{
			document.getElementById("region").value='none'
			document.getElementById("regionshow").style.display='none';
			document.getElementById("regioncheck").value='no'
			//document.getElementById("txtHint").innerHTML='<input type="HIDDEN" name="region" id="region" value="none">'
		    }

		    messageObj.close();
		}
	    }

	    function GetXmlHttpObject()
	    {
		if (window.XMLHttpRequest)
		{
		    // code for IE7+, Firefox, Chrome, Opera, Safari
		    return new XMLHttpRequest();
		}
		if (window.ActiveXObject)
		{
		    // code for IE6, IE5
		    return new ActiveXObject("Microsoft.XMLHTTP");
		}
		return null;
	    }
	    
	    



