// Load popup
function fn_popup_EmailForm(popup_email_id, popup_email_title, url)
{
	//popup_email_id = 'ilyas.kazi@tiez.nl';	//overwite for debugging
	var url_check = "";
	//url_check = url;							//remove comment to Debug

	// create html for popup:
	var popup_email_html = "<div id='popup_email_html' class='contForm_popup_wrap' style='display:block;'><!-- contForm_popup_wrap START here -->"+
		url_check +
		"<form name='frmEmailPopUp' id='frmEmailPopUp' action='' method='post' onsubmit=''>"+
			"<input type='hidden' name='email_for_client' id='email_for_client' value='y' />"+
			"<input type='hidden' name='client_email_address' id='client_email_address' value='" + popup_email_id + "' />"+
		"<table width='460' border='0' align='right' cellpadding='0' cellspacing='0'>"+
			"<tr><td colspan='3' class='text_form'>E-mail will be send to: <strong>" + popup_email_title + "</strong></td></tr>"+
			"<tr><td colspan='3' height='11'>&nbsp;</td></tr>"+
			"<tr><td class='text_form'>Your name:</td>"+
				"<td align='left'><input name='Name' id='Name' type='text' value='' size='40' /></td>"+
				"<td align='left'>&nbsp;</td></tr>"+
			"<tr><td width='170' class='text_form'>Company:</td>"+
				"<td width='260' align='left'><input name='Company' id='Company' type='text' size='40' value='' /></td>"+
				"<td width='30' align='left'>&nbsp;</td></tr>"+
			"<tr><td class='text_form'>E-mail address:</td>"+
				"<td align='left'><input name='Emailadress' id='Emailadress' type='text' size='40' value='' /></td>"+
				"<td align='left'>&nbsp;</td></tr>"+
			"<tr><td class='text_form'>Telephone number:</td>"+
				"<td align='left'><input name='phoneno' id='phoneno' type='text' size='40' /></td>"+
				"<td align='left'>&nbsp;</td></tr>"+
			"<tr><td class='text_form'>select country:</td>"+
				"<td align='left'>" + select_country + "</td>"+
				"<td align='left'>&nbsp;</td></tr>"+
			"<tr><td valign='top' class='text_form'>Your message:</td>"+
				"<td align='left'><textarea name='Comment' id='Comment' style='width:272px; height:82px'></textarea></td>"+
				"<td align='left' valign='top'>&nbsp;</td></tr>"+
			"<tr><td valign='top' class='text_form'>&nbsp;</td>"+
				"<td align='left' class='text_form'>please enter the security code as shown in the image below.</td>"+
				"<td align='left' valign='top'>&nbsp;</td></tr>"+
			"<tr><td class='text_form'>Security code:</td>"+
				"<td align='left'><input type='text' name='txtSecureCode' id='txtSecureCode' size='20' onkeypress='javascript: checkSubmit(event,\""+ url +"\")' />"+
				"<label id='err_securecode' style='padding-left:10px'>&nbsp;</label></td>"+
				"<td align='left'>&nbsp;</td></tr>"+
			"<tr><td class='text_form'>&nbsp;</td>"+
				"<td align='left'><label><div style='margin-left:0px'><img src='/CaptchaSecurityImages.php?width=100&height=32&characters=5' align='left'/></div></label></td>"+
				"<td align='left'></td></tr>"+
			"<tr><td class='text_form'>&nbsp;</td>"+
				"<td align='left'>"+
					"<a href='#' id='email_send' class='button_orange_arrow' onclick='javascript: validate_and_submit(\""+ url +"\");'><span>Send</span></a>"+
				"</td><td align='left'>&nbsp;</td></tr>"+
		"</table></form></div><!-- contForm_popup_wrap END here -->"+
		"<div id='popup_email_html_loading' class='wraptocenter_outer'>"+
			"<div class='wraptocenter_inner'>"+
				"<div class='wraptocenter'>"+
					"<img src='/styles/images/preload.gif' /><br />"+
					"<p>Please wait...</p>"+
				"</div>"+
			"</div>"+
		"</div>";
	
	// Show popup:
	TINY.box.show({html:popup_email_html,width:460,height:460})
}

// Is Enter Key Pressed then...
function checkSubmit(e, url)
{    
	if(e && e.keyCode == 13)
	{
		validate_and_submit(url);
	}
}
    
// Validate and submit popup email form
function validate_and_submit(url)
{
	if (document.frmEmailPopUp.Name.value=='')
	{
		alert("Please enter your name");
		document.frmEmailPopUp.Name.focus();
	}
	else if(document.frmEmailPopUp.Emailadress.value=='')
	{
		alert("Please enter your Email Address.");
		document.frmEmailPopUp.Emailadress.focus();
	}
	else if(echeck(document.frmEmailPopUp.Emailadress.value)==false)
	{
		document.frmEmailPopUp.Emailadress.focus();
	}
	else if(document.frmEmailPopUp.phoneno.value=='')
	{
		alert("Please enter your Telephone number.");
		document.frmEmailPopUp.phoneno.focus();
	}
	/*else if(document.frmEmailPopUp.language.value=='select')
	{
		alert("Please Select your Prefered communication language.");
		document.frmEmailPopUp.language.focus();
	}*/
	else if(document.frmEmailPopUp.country.value=='select')
	{
		alert("Please select your country.");
		document.frmEmailPopUp.country.focus();
	}
	else if(document.frmEmailPopUp.Comment.value=='')
	{
		alert("Please enter your question/suggestion/comment");
		document.frmEmailPopUp.Comment.focus();
	}
	else if(document.frmEmailPopUp.txtSecureCode.value=='')
	{
		alert("Please enter Security Code, which is showing on the image below");
		document.frmEmailPopUp.txtSecureCode.focus();
	}
	else
	{    
		verify_secure_code(url);
	}
	return false;
}


// Verify Secure Code through ajax post
function verify_secure_code(url)
{
	document.getElementById("popup_email_html").style.display = 'none';				// hide html form
	document.getElementById("popup_email_html_loading").style.display = 'table';	// show loading indicator
	
	var xmlhttp;
	var params		= "verify=securecode&txtSecureCode=" + document.frmEmailPopUp.txtSecureCode.value;
	var hostname	= "http://" + window.location.hostname;	// get hostname
	url				= hostname + url;						// create absolute url
	//alert(url);

	if (window.XMLHttpRequest)
	{   // code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{   // code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			if (xmlhttp.responseText == "TRUE")
			{	// Submit form if security code is valid:
				document.frmEmailPopUp.submit();
			}
			else
			{	// Alert for invalid security code:
				document.getElementById("popup_email_html_loading").style.display = 'none';	// hide loading indicator
				document.getElementById("popup_email_html").style.display = 'block';		// show html form
				document.getElementById("err_securecode").style.color="#FF0000";			// highlight message
				document.getElementById("err_securecode").innerHTML=xmlhttp.responseText;	// show error message
				document.getElementById("txtSecureCode").className = "highlight_error";		// highlight text box
				document.frmEmailPopUp.txtSecureCode.focus();
			}
		}
	}

	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.setRequestHeader("Content-length", params.length);
	//xmlhttp.setRequestHeader("Connection", "close");
	xmlhttp.send(params);
}




// Validate email address
function echeck(str) 
{
	var at  ="@";
	var dot =".";
	var lat =str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1)
	{
		alert("Invalid E-mail ID");
		return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert("Invalid E-mail ID");
		return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert("Invalid E-mail ID");
		return false;
	}
	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert("Invalid E-mail ID");
		return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert("Invalid E-mail ID");
		return false;
	}
	if (str.indexOf(dot,(lat+2))==-1)
	{
		alert("Invalid E-mail ID");
		return false;
	}
	if (str.indexOf(" ")!=-1)
	{
		alert("Invalid E-mail ID");
		return false;
	}
	return true;
}
