function mailIt(url,loadimg)
{
	//alert(base);
	document.getElementById('ajax_form').style.backgroundImage ='url('+base+'templates/mitash/images/contact-bg_done.jpg)';
	//document.getElementById('ajax_form').style.backgroundImage = 'url(templates/mitash/images/contact-bg_done.jpg)';
	document.getElementById("ajax_contact").style.display='none';
	document.getElementById("status").style.display='block';
	document.getElementById("status_msg").innerHTML = '<img src="'+loadimg+'" />';
	/*config*/
	var XHRequest = false;

	if(window.XMLHttpRequest)
	{
		XHRequest = new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		XHRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if(XHRequest)
	{
		//XHRequest.open("GET", url+"?to=" + document.getElementById('to').value + "&from=" + document.getElementById('from').value + "&subject=" + document.getElementById('subject').value + "&body=" + document.getElementById('body').value);
		XHRequest.open("POST", url);
		XHRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

		XHRequest.onreadystatechange = function(){
			if(XHRequest.readyState < 4)
			{
				document.getElementById("status_msg").innerHTML = "<img src='"+loadimg+"' /> Sending ...";
			}
			else if(XHRequest.readyState == 4 && XHRequest.status == 200)
			{
				var tempstr=XHRequest.responseText.split(" ### ");
				document.getElementById("status_msg").innerHTML = tempstr[0];
				document.getElementById("status_msg").innerHTML += "<br clear=\"both\" /><br clear=\"both\" /><a href=\"#form\" onclick=\"hide('status'); show('ajax_contact');\" > < Back</a>";
				document.getElementById('ajax_contact').style.backgroundImage ='url('+base+'templates/mitash/images/contact-bg.jpg)';
				//alert(document.getElementById('ajax_conact').style.backgroundImage);
				//alert(tempstr[1]);
				if(tempstr[1]=='sent' || tempstr[1]=='mailnotsent')
				{
					document.getElementById('ajax_form').style.backgroundImage ='url('+base+'templates/mitash/images/contact-bg_done.jpg)';
					//alert(document.getElementById('ajax_contact').style.backgroundImage);
					//resetter();
				}
				if(tempstr[1]=='sent')
				{
					//alert('coming here');
					resetter();
				}
				
				delete XHRequest;
				XHRequest = null;
			}
		}
		
		//XHRequest.send(null);
		
		XHRequest.send("captchastring=" + document.getElementById('captchastring').value +"&toaddress=" + document.getElementById('toaddress').value + "&name=" + document.getElementById('name').value + "&url=" + document.getElementById('url').value + "&from="+ document.getElementById('from').value + "&subject=" + document.getElementById('subject').value + "&phone=" + document.getElementById('phone').value + "&body=" + document.getElementById('body').value + "&submit=" + document.getElementById('submit').value + "&successful_msg=" + document.getElementById('successful_msg').value + "&failed_msg=" + document.getElementById('failed_msg').value + "&varifycode_error=" + document.getElementById('varifycode_error').value);
	}
}

function clicktextbox (resource, filler)
{
	if (resource.value == filler)
	{
		resource.value = '';
	}
}

function blurtextbox (resource, filler) {
	if (resource.value == '')
	{
		resource.value = filler;
	}
}

function resetter() {
	con_form = document.ajax_contact.elements;
	for (i=0;i<con_form.length;i++) {
		if ((con_form[i].type == 'text') || (con_form[i].type =='textarea'))
		{con_form[i].value = '';}
				
		if (con_form[i].type == ('img'))
		{
			con_form[i].src = base+'modules/mod_contactform/contactform/captcha.php?.png';
			//alert (con_form[i].src);
		}
			//alert(con_form[i].type);
	}
	
	document.getElementById('captchaimg').src ='';
	//alert(document.getElementById('captchaimg').src);
	document.getElementById('captchaimg').src =  base+'modules/mod_contactform/contactform/captcha.php?.png';
	//alert(document.getElementById('captchaimg').src);
}

function trim(str){
	return str.replace(/^\s+|\s+$/g,'');
}
	
function isEmail(str)
{
	var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
	return regex.test(str);
}

function isAlpha(sname)
{
	var alphaExp = /^[a-zA-Z]+$/;
	if(sname.match(alphaExp))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isNum(sphone)
{
	var numericExpression = /^[0-9]+$/;
	if(sphone.match(numericExpression))
	{
		return true;
	}
	else
	{
		return false;
	}
}

function errorbox(id,msg)
{
	errbox = document.getElementById(id);
	errbox.innerHTML ="";
	errbox.innerHTML = msg;
	errbox.style.display = "block";
}

function hideerrorbox(id)
{
	errbox = document.getElementById(id);
	errbox.innerHTML ="";
	errbox.style.display ="none";
}

function show(elm)
{
	div = document.getElementById(elm);
	//alert(elm);
	if(elm=='ajax_contact')
	{
		document.getElementById('ajax_form').style.backgroundImage ='url('+base+'templates/mitash/images/contact-bg.jpg)';
		//alert(document.getElementById('ajax_form').style.backgroundImage);
		//resetter();
	}
	div.style.display = 'block';
	//alert(elm + div.style.display);
}
function hide(elm)
{
	div = document.getElementById(elm);
	div.style.display = 'none';
	//alert(elm + div.style.display);
}

function checkForm(){

	// get the form field data
	var cname = document.ajax_contact.name.value;
	var cemail = document.ajax_contact.from.value;
	var cphone = document.ajax_contact.phone.value;
	var captcha = document.ajax_contact.captchastring.value;

	//alert("CNAME:"+cname);
	//alert(cname);
	 
	 //alert("Captcha:"+captcha); 
	if (trim(cname) == 'Your Name' || trim(cname) == '')
	{
	   // alert('Please enter Your Name');
		errorbox("lblerror","Please enter Your Name");
		document.ajax_contact.name.focus();
		return false;
	}
	if (p=(isAlpha(trim(cname)))==false){
		errorbox("lblerror","Only alphabets allowed");
	   // alert('Only alphabets allowed');
		document.ajax_contact.name.focus();
		return false;	
	}
	if (trim(cemail) == 'E-mail' || trim(cemail) == '')
	{
		errorbox("lblerror","Please enter your email");
		//alert('Please enter your email');
		document.ajax_contact.from.focus();
		return false;
	}
	else if(!isEmail(trim(cemail))){
		errorbox("lblerror","Email address is not valid");
		//alert('Email address is not valid');
		document.ajax_contact.from.focus();
		return false;
	} 
	else if(trim(cphone) == 'Phone' || trim(cphone) == ''){
		errorbox("lblerror","Please enter your Phone Number");
		//alert('Please enter your Phone Number');
		document.ajax_contact.phone.focus();
		return false;	
	} else if(t=(isNum(trim(cphone)))==false){
		errorbox("lblerror","Phone Number not valid");
		//alert('Phone Number not valid');
		document.ajax_contact.phone.focus();
		return false;			
	} else if (trim(captcha) == ''){
		errorbox("lblerror","Please enter the validation code");
		//alert('Please enter the validation letter code');
		document.ajax_contact.captchastring.focus();
		return false;
	}
	else{
		//alert(loadingimg);
		mailIt(url, loadingimg);
		return false;
	}
}
function checkForm2(){
           //alert("drtr");
	// get the form field data
	//alert(document.contact_us2.name.value);
	var cname = document.contact_us2.name.value;
	var cemail = document.contact_us2.email.value;
	var cphone = document.contact_us2.phone.value;
	var captcha = document.contact_us2.captchastring2.value;
	if (trim(cname) == '-Name-' || trim(cname) == '')
	{
	   alert('Please enter Your Name');
		//errorbox("lblerror","Please enter Your Name");
		document.contact_us2.name.focus();
		return false;
	}
	if (p=(isAlpha(trim(cname)))==false){
		//errorbox("lblerror","Only alphabets allowed");
	    alert('Only alphabets allowed');
		document.contact_us2.name.focus();
		return false;	
	}
	if (trim(cemail) == '-E-mail-' || trim(cemail) == '')
	{
		//errorbox("lblerror","Please enter your email");
		alert('Please enter your email');
		document.contact_us2.email.focus();
		return false;
	}
	else if(!isEmail(trim(cemail))){
		//errorbox("lblerror","Email address is not valid");
		alert('Email address is not valid');
		document.contact_us2.email.focus();
		return false;
	} 
	else if(trim(cphone) == '-phone-' || trim(cphone) == ''){
		//errorbox("lblerror","Please enter your Phone Number");
		alert('Please enter your Phone Number');
		document.contact_us2.phone.focus();
		return false;	
	} else if(t=(isNum(trim(cphone)))==false){
		//errorbox("lblerror","Phone Number not valid");
		alert('Phone Number not valid');
		document.contact_us2.phone.focus();
		return false;			
	} else if (trim(captcha) == ''){
		//errorbox("lblerror","Please enter the validation code");
		alert('Please enter the validation letter code');
		document.contact_us2.captchastring2.focus();
		return false;
	}
		else{
		//alert(loadingimg);
		mailIt(url, loadingimg);
		return false;
	}
}
///////////for contact us left menu
function checkForm3(){
           //alert("drtr");
	// get the form field data
	//alert(document.contact_us2.name.value);
	var cemail = document.contact_us.emailid.value;
	if (trim(cemail) == 'Your mail id' || trim(cemail) == '')
	{
		//errorbox("lblerror","Please enter your email");
		alert('Please enter your email');
		document.contact_us.emailid.focus();
		return false;
	}
	else if(!isEmail(trim(cemail))){
		//errorbox("lblerror","Email address is not valid");
		alert('Email address is not valid');
		document.contact_us.emailid.focus();
		return false;
	} 
	
		else{
		//alert(loadingimg);
		mailIt(url, loadingimg);
		return false;
	}
}
