// JScript File

var ids = new Array("tTitle","tFirstname","tSurname","tHouse","tPostcode","tAddress1","tCity","tCounty","tDate","tTime","tAdults","tContact","tEmail")
var errors = new Array("Title","First name","Surname","House no","Postcode","Address1","Town/city","County","Date of visit","Preferred time","No of adults","Contact no","Email address")
  
function validateParkVisits(parkId)
{
  
  var xmlString="";

  var msg="";
  for(i=0;i<ids.length;i++)
  {
    if(document.getElementById(ids[i]).value=="")
        msg = msg + errors[i] + '\n'
        
    xmlString = xmlString+'<'+ids[i]+'>'+document.getElementById(ids[i]).value.replace(/&/g,"&amp;").replace(/'/g,"''").replace(/</g,"''") + '</'+ids[i]+'>';        
  }  

  var today= new Date();
    
  if (!isDate(document.getElementById('tDate').value) && document.getElementById('tDate').value !="")
          msg = msg + "Valid date of visit e.g. dd-mm-yyyy" + '\n'

  if (!Emailcheck(document.getElementById('tEmail').value) && document.getElementById('tEmail').value !="")
          msg = msg + "Valid email address" + '\n'

  if ((document.getElementById('tEmail').value != document.getElementById('tEmailCon').value) && (document.getElementById('tEmail').value !=""))
          msg = msg + "Email address and confirm email does not match" + '\n'

 /* Using form values, create a new date object
 using the setFullYear function */
 
 if(isDate(document.getElementById('tDate').value) )
 { 
    selectedDate = stringIntoDate(document.getElementById('tDate').value)
    todaysDate = new Date();
    
    if(selectedDate < todaysDate)
        msg = msg + "Visit date must be in future" + '\n'
  }          
    
  
        
  xmlString = xmlString + '<tAddress2>' +   document.getElementById('tAddress2').value.replace(/&/g,"&amp;").replace(/'/g,"''").replace(/</g,"''")  + '</tAddress2>';
  xmlString = xmlString + '<tChildren>' +   document.getElementById('tChildren').value.replace(/&/g,"&amp;").replace(/'/g,"''").replace(/</g,"''")  + '</tChildren>';
  xmlString = xmlString + '<tComments>' +   document.getElementById('tComments').value.replace(/&/g,"&amp;").replace(/'/g,"''").replace(/</g,"''")  + '</tComments>';
  xmlString = xmlString + '<supplierId>' +  parkId  + '</supplierId>';
  
  xmlString = '<request>' + xmlString + '</request>'     
 
    
    
  if(msg!="")
    alert("Please review the following before you proceed;\n\n" + msg)
  else
  {
                        // create XML for HTTP POST
	        var oDomDoc = Sarissa.getDomDocument();
	        oDomDoc = (new DOMParser()).parseFromString(xmlString, "text/xml");
                        
            //alert(xmlString);
        	// debug-target: popup XML request for debugging
    
    
    //alert("http://"+window.location.hostname+"/resource/scripts/ajax/InsertParkVisits.aspx")
       
	        // enable following line
//    	    var oDomResultDoc = xmlhttp.responseXML;

			$('#emailLink').toggleClass('open');
			$('#emailForm').slideToggle('slow');
				
            clearForm();
            document.getElementById('emailhasbeensentid').innerHTML='<img src="../../resource/images/small-tick.gif" style="float:left;" /> Email sent...';
            document.getElementById('emailtextid').innerHTML='One of our friendly sales managers will be in touch as soon as posible to confirm the details of your visit.';

        	// create XMLHTTPRequest object and POST xml
    	    var xmlhttp = new XMLHttpRequest();
    	    xmlhttp.open("POST", "http://"+window.location.hostname+"/resource/scripts/ajax/InsertParkVisits.aspx", false);
    	    xmlhttp.send(oDomDoc);
    	    						
            
        	// debug-target display xml response result
        	//alert("XML HTTP Response: "+Sarissa.serialize(oDomResultDoc));
        	//document.getElementById('arrangeVisit').style.display='none';
  }
    
  function clearForm()
  {
  for(i=0;i<ids.length;i++)
  {document.getElementById(ids[i]).value="";}

  document.getElementById('tAddress2').value="";
  document.getElementById('tChildren').value="";
  document.getElementById('tComments').value="";
  document.getElementById('tEmailCon').value="";
  
  }  
    
}
