/* 
---Javascript for page homepage.htm of website Icon Holidays
---Date: 23/June/2008
---Designed by: Nhieu Cong Tran
*/

//create the request
var xHRObject = false;
if (window.XMLHttpRequest) { xHRObject = new XMLHttpRequest(); }
else if (window.ActiveXObject)
{ xHRObject = new ActiveXObject("Microsoft.XMLHTTP"); }

//funciton submit()
function submit()
{
	//Validation
	//Getting all the fields
	var slt_title = document.getElementById('slt_title').value;
	var txt_firstName = document.getElementById('txt_firstName').value;
	var txt_surName = document.getElementById('txt_surName').value;
	var txt_Email = document.getElementById('txt_Email').value;
	var chk_Add2MailList = document.getElementById('chk_Add2MailList');
	var txt_Request = document.getElementById('txt_Request').value;
	
	//Getting span error
	var span_error = document.getElementById('error');
	//Initial span_error with nothing
	span_error.innerHTML = '';
	span_error.style.cssText = '';
	
	var error_code = new Array();
	var error_request = new Array();
	var email_temp = emailCheck(txt_Email);
	
	//FirstName
	if (txt_firstName == '') error_code.push('Your First Name');
			
	//SurName
	if (txt_surName == '') error_code.push('Your Surname');
	
	//Email
	if (txt_Email == '') error_code.push('Your Email Address');
	
	else if (email_temp != '') error_code.push('Email Address causes an error at: '+ email_temp);
	
	if (error_code.length>0) 
	{
		span_error.style.cssText = 'border:3px dotted #D1E3F9; background-color:#E9F0FA; font-size:110%';
      	span_error.innerHTML += 'Please enable us to respond to your request by completing the following details:<br/>';
      	//span_error.innerHTML += '<span style="background-color:#D1E3F9; padding:24px; border-left:3px solid #616EA4; border-right:3px solid #616EA4; border-top:3px solid #616EA4;">';
      	for (var i=0;i<error_code.length;i++)
      	{
	      	span_error.innerHTML += '&bull;&nbsp;<span style="font-style:italic;color:red">'+error_code[i]+'</span><br/>';
      	}
	}
	//Check whether at least 1 in 4 options have been chosen
	else if (txt_Request=='')
		{
			span_error.style.cssText = 'border:3px dotted #D1E3F9; background-color:#E9F0FA; font-size:120%; font-style:italic';
	      	span_error.innerHTML += 'Sorry! We do not understand your request.<br/>Please type in your request and then click on submit. Thanks!<br/>';
		}
	//submit the form
	else
	{
		
		//send the request
		var bodyofrequest = "title=" + encodeURIComponent(slt_title) + "&fName=" + encodeURIComponent(txt_firstName);
		bodyofrequest += "&sName=" + encodeURIComponent(txt_surName) + "&email=" + encodeURIComponent(txt_Email);
		
		if (txt_Request!='') bodyofrequest += "&u_request=" + encodeURIComponent(txt_Request);
		else bodyofrequest += "&u_request=None";
		
		if (chk_Add2MailList.checked==true) bodyofrequest += "&add2MailList=Yes";
		else bodyofrequest += "&add2MailList=None";
		
       	
       	xHRObject.open("POST", "response_ContactUs.aspx", true);
		xHRObject.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xHRObject.onreadystatechange = getData;
       	xHRObject.send(bodyofrequest); 
       	
		document.getElementById('submit').innerHTML='Please wait...';
       	
       	document.getElementById('submit').onclick = '';
       	
       	document.getElementById('submit').title = "Please wait...";
       	
       	document.getElementById('submit').style.cssText="font-size: 18px;border:1px solid #919194;color:#636C74;";
       	
		//while(xHRObject.readyState != 4)
		//getData();
	}
}

//function getData when receiving the response from the server
function getData()
{
    if (xHRObject.readyState == 4 && xHRObject.status == 200)
    {
        var serverText = xHRObject.responseText;
        //alert(serverText);

        if(serverText.indexOf('|') != -1 ) 
	    {
            var element = serverText.split('|');
            //document.getElementById(element[0]).innerHTML = element[1];
        
        
            //Change Brochure Enquiry into Thank you
            document.getElementById('banner_ContactUs_1').src="images/BannerImages/ThankYou/Header_01.jpg";
			document.getElementById('banner_ContactUs_2').src="images/BannerImages/ThankYou/Header_02.jpg";
			document.getElementById('banner_ContactUs_3').src="images/BannerImages/ThankYou/Header_03.jpg";
			document.getElementById('banner_ContactUs_5').src="images/BannerImages/ThankYou/Header_05.jpg";
            
            //Clear the content of div_left
            document.getElementById('div_left').innerHTML = '';
		    //document.getElementById('div_left').style.cssText = '';
    		
		    //clear span_error
		    document.getElementById('error').innerHTML = '';
		    document.getElementById('error').style.cssText = '';
		    
		    //var screen_x = screen.width* 70/100;
		    
		    document.getElementById('div_right').style.cssText="color: #999999;font-family: Arial, Helvetica, sans-serif;overflow:auto;font-size:13px;";
		    document.getElementById('div_right').align="left";
            
		    document.getElementById('div_right').innerHTML = '<strong>THANK YOU</strong><br/><br/>';

		    document.getElementById('div_right').innerHTML += 'Thank you for contacting Icon Holidays.<br/><br/>';

		    document.getElementById('div_right').innerHTML += '<strong>' + element[0] + '</strong><br/><br/>';
    		
		    //alert(element[1]);
			//alert(element[2].indexOf('Printed'));
			//alert(element[3]);
			//alert(element[4]);
			
			//Request only
			if (element[1] == "Yes") document.getElementById('div_right').innerHTML += 'We appreciate hearing from you.<br/> We will reply as soon as possible.<br/>';
		    
			//document.getElementById('div_right').innerHTML += 'We appreciate your request for further information and the brochures requested will shortly be forwarded to you.<br/><br/>';

		    document.getElementById('div_right').innerHTML += '<br/>We look forward to assisting you with any travel arrangements you require.  Should you wish to discuss your ideas further with us, please do not hesitate to contact us.<br/><br/>';

		    document.getElementById('div_right').innerHTML += '<strong>ICON HOLIDAYS</strong><br/><strong>Telephone: 03 9810 7666</strong>&nbsp;<span style="font-variant:small-caps">(melbourne and metropolitan area only)</span><br/><strong>Freecall: 1300 853 953</strong>&nbsp;<span style="font-variant:small-caps">(all other areas)</span>';
		}
		else 
		    {
		        document.getElementById('div_right').innerHTML = serverText;
		        document.getElementById('div_right').innerHTML += "<br/>Please <a href='IH_brochureRequest.html'>click here</a> to complete your request again."
		    }

    }
}

//function to cut all spaces unneeded in a string
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

//function emailCheck
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD. 1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format. It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters. We don't want to allow special characters in the address.
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a
username or domainname. It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes). E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names. E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

	return "Email address seems incorrect (check @ and .'s)";
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
	
	return "The username contains invalid characters.";
}
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {

	return "The domain name contains invalid characters.";
}
}

// See if "user" is valid

if (user.match(userPat)==null) {

// user is not valid

	return "The username doesn't seem to be valid.";
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
	
	return "Destination IP address is invalid!";
}
}
return "";
}

// Domain is symbolic name. Check if it's valid.

var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {

	return "The domain name does not seem to be valid.";
}
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 &&
domArr[domArr.length-1].search(knownDomsPat)==-1) {
	
	return "The address must end in a well-known domain or two letter " + "country.";
}

// Make sure there's a host name preceding the domain.

if (len<2) {

	return "This address is missing a hostname!";
}

// If we've gotten this far, everything's valid!
return "";
}

// End -->