/**
 * @author Vishwanath M S
 * @project SILICON EMBSYS
 * @date 21 JANUARY 2011
 * @copyright 2011
 * Coding done by Vishwanath M S
 */

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function isAlphanew(xStr){
       var regEx = /^[a-zA-Z\.\-\s]+$/;
       return xStr.match(regEx);
     }

 function createXMLHttpRequest( ) {
    var request = false;

    /* Does this browser support the XMLHttpRequest object? */
    if (window.XMLHttpRequest) {
        if (typeof XMLHttpRequest != 'undefined')
            /* Try to create a new XMLHttpRequest object */
            try {
                request = new XMLHttpRequest( );
            } catch (e) {
                request = false;
            }
    /* Does this browser support ActiveX objects? */
    } else if (window.ActiveXObject) {
        /* Try to create a new ActiveX XMLHTTP object */
        try {
            request = new ActiveXObject('Msxml2.XMLHTTP');
        } catch(e) {
            try {
                request = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {
                request = false;
            }
        }
    }
    return request;
}
         $(document).ready(function(){
	         $('#livechatclick').click(function(){

	 document.sechatform.reset();
	$('errresponse').fadeOut('slow');

	document.getElementById('vname').style.borderColor='#DFDFDF';
	document.getElementById('vname').style.color='grey';
	document.getElementById('vemail').style.borderColor='#DFDFDF';
	document.getElementById('vemail').style.color='grey';
	document.getElementById('vphone').style.borderColor='#DFDFDF';
	document.getElementById('vphone').style.color='grey';
	document.getElementById('errresponse').innerHTML='';

	         $('#floatdiv').fadeOut('slow',function(){
	         	     $('#engineering').fadeIn('slow');
	         	     $('#engineering2').fadeIn('slow');
			 });
			 });

			 $('#crossclose').click(function(){
	         	     $('#engineering').fadeOut('slow');
	         	     $('#engineering2').fadeOut('slow', function(){
	         	     	$('#floatdiv').fadeIn('slow');
					 });
			 });

			 $('#crossclose1').click(function(){
	         	     $('#engineering3').fadeOut('slow');
	         	     $('#engineering4').fadeOut('slow', function(){
	         	     	$('#floatdiv').fadeIn('slow');
					 });
			 });

			 $('#cancelclose').click(function(){
	         	     $('#engineering').fadeOut('slow');
	         	     $('#engineering2').fadeOut('slow', function(){
	         	     	$('#floatdiv').fadeIn('slow');
					 });
			 });

	$('#submitchat').click(function(){
	var name = document.getElementById('vname');
	var email = document.getElementById('vemail');
	var phone = document.getElementById('vphone');
	var company = document.getElementById('vcompany');
	var userquery = document.getElementById('vquery');

	$('errresponse').fadeOut('slow');

	document.getElementById('vname').style.borderColor='#DFDFDF';
	document.getElementById('vname').style.color='grey';
	document.getElementById('vemail').style.borderColor='#DFDFDF';
	document.getElementById('vemail').style.color='grey';
	document.getElementById('vphone').style.borderColor='#DFDFDF';
	document.getElementById('vphone').style.color='grey';

	document.getElementById('errresponse').innerHTML='';

	if(trim(name.value)=='')
	{
		document.getElementById('vname').style.borderColor='red';
		document.getElementById('vname').style.color='red';
		document.getElementById('errresponse').style.color='red';
		document.getElementById('errresponse').innerHTML='<b>Invalid name.</b>';
		$('errresponse').fadeOut('slow');
		document.getElementById('vname').focus();
		return false;
	}
	else
	{
		if(!isAlphanew(trim(name.value)))
		{
		document.getElementById('vname').style.borderColor='red';
		document.getElementById('vname').style.color='red';
		document.getElementById('errresponse').style.color='red';
		document.getElementById('errresponse').innerHTML='<b>Invalid name.</b>';
		$('errresponse').fadeOut('slow');
		document.getElementById('vname').focus();
		return false;
		}
	}

	if(trim(email.value)=='')
	{
		document.getElementById('vemail').style.borderColor='red';
		document.getElementById('vemail').style.color='red';
		document.getElementById('errresponse').style.color='red';
		document.getElementById('errresponse').innerHTML='<b>Invalid email.</b>';
		$('errresponse').fadeOut('slow');
		document.getElementById('vemail').focus();
		return false;
	}
	else(trim(email.value)!='')
	{
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
			 if (!filter.test(email.value))
			 {
				document.getElementById('vemail').style.borderColor='red';
				document.getElementById('vemail').style.color='red';
				document.getElementById('errresponse').style.color='red';
				document.getElementById('errresponse').innerHTML='<b>Invalid email.</b>';
				$('errresponse').fadeOut('slow');
				document.getElementById('vemail').focus();
				return false;
			 }
	}

	if(trim(phone.value)=='')
	{
		document.getElementById('vphone').style.borderColor='red';
		document.getElementById('vphone').style.color='red';
		document.getElementById('errresponse').style.color='red';
		document.getElementById('errresponse').innerHTML='<b>Invalid Ph/Mob.</b>';
		$('errresponse').fadeOut('slow');
		document.getElementById('vphone').focus();
		return false;
	}
	else(trim(phone.value)!='')
	{
			var filter = /^[0-9\-\+]+$/;
			 if (!filter.test(phone.value))
			 {
				document.getElementById('vphone').style.borderColor='red';
				document.getElementById('vphone').style.color='red';
				document.getElementById('errresponse').style.color='red';
				document.getElementById('errresponse').innerHTML='<b>Invalid Ph/Mob.</b>';
				$('errresponse').fadeOut('slow');
				document.getElementById('vphone').focus();
				return false;
			 }
	}

	var param = $('#sechatform').serialize();

 	var p_request = createXMLHttpRequest();

 	p_URL = 'chatforminsert.php?'+param;
 	p_data = '';
 	p_method = 'POST';

 	if (p_request) {
        /* Is the posting method 'GET'? */
        if (p_method == 'GET')
            p_request.open('GET', p_URL + '?' + p_data, true);
        else
            p_request.open('POST', p_URL, true)
        p_request.onreadystatechange = function() {

    /* Is the /readyState/ 4? */
    if (p_request.readyState == 4) {
        /* Is the /status/ 200? */
        if (p_request.status == 200) {
            /* Grab the /responseText/ from the request (XMLHttpRequest) */
            var response = p_request.responseText;
			if(response=='success')
			{
				     $('#engineering').fadeOut('slow');
	         	     $('#engineering2').fadeOut('slow', function(){
	         	     $('#engineering3').fadeIn('slow');
	         	     $('#engineering4').fadeIn('slow');
	         	 	 document.getElementById('finalmsg').innerHTML = 'Thank you for the details. Currently the contact is offline, we shall get back within 24 hrs!';
					 });

			}
			else if(response=='duplicate')
			{
					$('#engineering3').fadeIn('slow');
	         	    $('#engineering4').fadeIn('slow');
	         	 	document.getElementById('finalmsg').innerHTML = 'Your request is in progress. we ll get back to u soon.';
			}
			else if(response=='error')
			{
					$('#engineering3').fadeIn('slow');
	         	    $('#engineering4').fadeIn('slow');
	         	    document.getElementById('finalmsg').innerHTML = 'Network error. Try after some time.';
			}

            // here is where the parsing would begin.
        } else
            alert('ERROR: There was a problem retrieving the data: \n' +
                p_request.statusText);
        p_request = null;
    }
};
		    p_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			p_request.setRequestHeader("Connection", "close");

        /* Is the posting method 'GET'? */
        if (p_method == 'GET')
            p_request.send();
        else
            p_request.send(p_data);
    }


});


});



