
function xmlhttpPostSendEmail(strURL,qstr) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
			//alert(self.xmlHttpReq.responseText);
			//alert(self.xmlHttpReq.readyState);
        }
    }
    self.xmlHttpReq.send(qstr);
}



function updatepage(str){
	switch(str){
	case "-1":
		alert('Send e-mail fail...');
		break;
	case "0":
		alert('Thank you for your comment.');
		document.frm_contact.reset();
		break;
	default :
		alert(str);
	}
	//alert(str);
}


function CyJS_Utils_IsEmailValid(checkThisEmail)
	{
		var myEMailIsValid = true;
		var myAtSymbolAt = checkThisEmail.indexOf('@');
		var myLastDotAt = checkThisEmail.lastIndexOf('.');
		var mySpaceAt = checkThisEmail.indexOf(' ');
		var myLength = checkThisEmail.length;


		// at least one @ must be present and not before position 2
		// @yellow.com : NOT valid
		// x@yellow.com : VALID

		if (myAtSymbolAt < 1 ) 
		 {myEMailIsValid = false;}


		// at least one . (dot) afer the @ is required
		// x@yellow : NOT valid
		// x.y@yellow : NOT valid
		// x@yellow.org : VALID

		if (myLastDotAt < myAtSymbolAt) 
		 {myEMailIsValid = false;}

		// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
		// x.y@yellow. : NOT valid
		// x.y@yellow.a : NOT valid
		// x.y@yellow.ca : VALID

		if (myLength - myLastDotAt <= 2) 
		 {myEMailIsValid = false;}


		// no empty space " " is permitted (one may trim the email)
		// x.y@yell ow.com : NOT valid

		if (mySpaceAt != -1) 
		 {myEMailIsValid = false;}

		return myEMailIsValid;
	}

function checkSubmit(){
		//alert("Test");
		var fname = document.frm_contact.fname;
		var lname = document.frm_contact.lname;
		var company = document.frm_contact.company;
		var email = document.frm_contact.email;
		if(fname.value == ""){
			alert('Please fill information');
			fname.focus();
			return false;
		}
		if(lname.value == ""){
			alert('Please fill information');
			lname.focus();
			return false;
		}
		if(company.value == ""){
			alert('Please fill information');
			company.focus();
			return false;
		}
		if(!CyJS_Utils_IsEmailValid(email.value)){
			alert('Please fill information');
			email.focus();
			return false;
		}
		sendmail();
}


function replacestring(str){
	var s = '';
	for(var i = 0 ; i < str.length; i++){
		var c = str.charAt(i);
		switch(c){
			case '&' :
				c = '%26';
				break;
			case '=' :
				c = '%3D';
				break;
			case '?' :
				c = '%3F';
				break;
			case '/' :
				c = '%2F';
				break;
		}
		s = s + c;
		
	}
	return s;
}

// Radio Button Validation
// copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005
// you may copy this function but please keep the copyright notice with it
function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}
function sendmail(){
	//var toppic = document.frm_contact.toppic.;
	var fname = document.frm_contact.fname.value;
	var lname = document.frm_contact.lname.value;
	var company = document.frm_contact.company.value;
	var email = document.frm_contact.email.value;
	var toppic = valButton(document.frm_contact.toppic);
	var comment = document.frm_contact.comment.value;
	var street = document.frm_contact.street.value;
	var postcode = document.frm_contact.postcode.value;
	var city = document.frm_contact.city.value;
	var country = document.frm_contact.country.value;
	var tel = document.frm_contact.tel.value;
	var fax = document.frm_contact.fax.value;

	var qstr = 'toppic=' + toppic +'&fname=' + replacestring(fname) + '&lname=' + replacestring(lname) +'&company=' + company + '&comment=' + replacestring(comment) + '&email=' + email + '&street=' + replacestring(street)+ '&postcode=' + replacestring(postcode) + '&city=' + replacestring(city)+ '&country=' + replacestring(country)+ '&tel=' + tel+ '&fax=' + fax;
	//alert(qstr);
	//xmlhttpPostSendEmail("apps/sendmail_contact.php",qstr);
	xmlhttpPostSendEmail("pagesendmail.php",qstr);
}
