//Code piece to add click function to HTMLElements in firefox browser
//Do this only for firefox
try
{
	new ActiveXObject('Msxml2.XMLHTTP')
} 
catch(e) {
	HTMLElement.prototype.click = function() {
	var evt = this.ownerDocument.createEvent('MouseEvents');
	evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
	this.dispatchEvent(evt);
	}
}

function show_askdetails(placeholder, productId, imageholder)
{
	var status = document.getElementById(placeholder).innerHTML;
	//Check the collapse status of the element
	if (""==status)
	{
		//Generate the form and place it in
		document.getElementById(placeholder).innerHTML = getFormHtml(placeholder, productId);
		//Change the image to collapse
		if (imageholder)
		{
			document.getElementById(imageholder).setAttribute('src', '/wp-content/plugins/wp-shopping-cart/images/collapse.gif');
			document.getElementById(imageholder).setAttribute('alt', 'Collapse');
		}
	}
	else
	{
		document.getElementById(placeholder).innerHTML = "";
		//Set expand image
		if (imageholder)
		{
			document.getElementById(imageholder).setAttribute('src', '/wp-content/plugins/wp-shopping-cart/images/expand.gif');
			document.getElementById(imageholder).setAttribute('alt', 'Expand');
		}
	}
	//Clear the validation message if any,
	try{
		var msgPh = productId + "_QUERYFORMMESSAGE_PH";
		document.getElementById(msgPh).innerHTML ="";		
	}catch (exce){}
}

function getFormHtml(placeholder, productId)
{
  //Add the hidden form which sends query 
  var formCode = "";

	//Placeholder for validation messages
	formCode += "<p><div id='" + productId + "_QUERYFORMMESSAGE_PH'></div></p>";
	//Here comes the FORM
	formCode += "<form action=\"javascript:sendQuery('" + placeholder + "'," + productId + ");\" name='commentform'  id='" + productId + "_QUERYFORM'>";
	formCode += "<table><tr>";
	formCode += "	<td><label for='author'><strong>Name <font color='#FF0000'>*</font></strong></label></td>";
	formCode += "	<td><input class='text_input' type='text' name='name' id='" + productId+"_author' value='' tabindex='1' /></td>";
	formCode += "</tr><tr>";
	formCode += "	<td><label for='email'><strong>Mail <font color='#FF0000'>*</font></strong></label></td>";
	formCode += "	<td><input class='text_input' type='text' name='email' id='" + productId+"_email' value='' tabindex='2' /></td>";
	formCode += "</tr><tr>";
	formCode += "	<td><label for='url'><strong>Website</strong></label></td>";
	formCode += "	<td><input class='text_input' type='text' name='url' id='" + productId+"_homepage' value='' tabindex='3' /></td>";
	formCode += "</tr><tr>";
	formCode += "	<td><label for='url'><strong>Subject <font color='#FF0000'>*</font></strong></label></td>";
	formCode += "	<td><input class='text_input' type='text' name='subject' id='" + productId+"_subject' value='' tabindex='4' /></td>";
	formCode += "</tr></table>";
	formCode += "	<p/><p><textarea class='text_input text_area' name='query' id='" + productId+"_query' rows='7' tabindex='5' cols='24'></textarea></p>";
	formCode += "	<p>";
	formCode += "	<input name='send' class='form_submit' type='button' id='submit' tabindex='6' value='Send' onclick=\"javascript:sendQuery('" + placeholder + "'," + productId + ");\"/>";
	formCode += "	</p>";
	formCode += "</form>";
	
	return formCode;
}

var queryResponse=function(text, xml, http_request)
{
if (http_request.status == 200) {
	var response = text;
	//Check for the response tag
	if (response.indexOf('response',0)!=-1)
	{
		var productId ="";
		try
		{
			//XML parsing... may fail sometimes
			var xmlDoc = xml;
			productId = xmlDoc.getElementsByTagName('product').item(0).firstChild.nodeValue;
			//Collapse the form and hide it......
			document.getElementById('product_' + productId + '_QUERYFORM_PH').innerHTML = "";
			document.getElementById('product_' + productId + '_ASKDETAILICON').setAttribute('src', '/wp-content/plugins/wp-shopping-cart/images/expand.gif');
		}
		catch (exp)
		{
			//string parsing ... highly unsafe
			var s = response.indexOf('<product>',0);
			var e = response.indexOf('</product>',s);
			if (s!=-1 && e!=-1)
				productId = response.substring(s+9, e);	
			//Collapse the form and hide it......
			document.getElementById('product_' + productId + '_QUERYFORM_PH').innerHTML = "";
			document.getElementById('product_' + productId + '_ASKDETAILICON').setAttribute('src', '/wp-content/plugins/wp-shopping-cart/images/expand.gif');
		}		
	}
} else {					
	document.getElementById('product_' + productId + '_QUERYFORM_PH').innerHTML = "There was a problem sending your query. Please try again.";
}
return true;
}

function makePOSTRequest(url, parameters, msgPlaceHolder) {
	
	try{
		ajax.post(url, queryResponse,parameters);
	}
	catch(exp)
	{
		document.getElementById(msgPlaceHolder).innerHTML = "There was a problem posting your query. Please try again.";
	}
}

function sendQuery(placeholder, productId) {

	var author = document.getElementById(productId+"_author").value;
	var email = document.getElementById(productId+"_email").value;
	var homepage = document.getElementById(productId+"_homepage").value;
	var subject = document.getElementById(productId+"_subject").value;
	var query = document.getElementById(productId+"_query").value;

	if (validateQuery(productId)==false)
	{
		return;
	}
	

  var poststr = "prodid=" + productId +
				"&author=" + encodeURI( author ) +
				"&email=" + encodeURI( email ) +
				"&homepage=" + encodeURI( homepage ) +
				"&subject=" + encodeURI( subject ) +
				"&query=" + encodeURI( query ) ;                                       
  //Show status in the placeholder element
  document.getElementById(placeholder).innerHTML =  "<img id='register_loading_img' src='/wp-content/plugins/wp-shopping-cart/images/loading.gif' alt='Sending...' title=''/>";

  makePOSTRequest('/wp-content/plugins/wp-shopping-cart/sendquery.php', poststr, placeholder);
}

function validateQuery(productId)
{
	var msgPh = productId + "_QUERYFORMMESSAGE_PH";

	var author = document.getElementById(productId+"_author").value;
	var email = document.getElementById(productId+"_email").value;
	var homepage = document.getElementById(productId+"_homepage").value;
	var subject = document.getElementById(productId+"_subject").value;
	var query = document.getElementById(productId+"_query").value;

	if (productId==0)
	{
		//This line commented to place this on "Contact Us" page
		//return;
	}
	if (author.length==0)
	{
		document.getElementById(msgPh).innerHTML = "<font color='#FF0000'>Kindly enter your Name</font>";
		return false;
	}
	if (emailCheck(email, msgPh)==false)
	{
		document.getElementById(productId+"_email").value="";
		document.getElementById(productId+"_email").focus();
    	document.getElementById(msgPh).innerHTML = "<font color='#FF0000'>Invalid E-mail ID</font>";
		return false;
	}
	if (subject.length==0)
	{
		document.getElementById(msgPh).innerHTML = "<font color='#FF0000'>Please enter the subject of your query</font>";
		return false;
	}
	if (query.length==0)
	{
		document.getElementById(msgPh).innerHTML = "<font color='#FF0000'>Please enter your query</font>";
		return false;
	}
	document.getElementById(msgPh).innerHTML ="";
	return true;
}

/**
 * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 * http://smartwebby.com/DHTML/email_validation.asp
 */

function emailCheck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false;
		 }

 		 return true;				
	}

//Code piece to show heavy media (video, flash content) only on user's request
//This is to minimize the unnecessary traffic
/*
Usage

<div id='chatlink' onclick='javascript:collapseMedia("chat");return false;' class='PlusBox_icon'><img id='chatimage' src='http://www.google.com/mb/plus_sm.gif'/></div>
<div id='chat' style='display:none' code='&lt;div style=&quot;width:210px&quot;&gt;&lt;style&gt;.mcrmeebo { display: block; background:url(&apos;%26quot;http:/widget.meebo.com/r.gif%26quot;&apos;) no-repeat top right; } .mcrmeebo:hover { background:url(&apos;%26quot;http:/widget.meebo.com/ro.gif%26quot;&apos;) no-repeat top right; } &lt;/style&gt;&lt;embed src=&quot;http://widget.meebo.com/mcr.swf?id=PfwduCPGLg&quot; type=&quot;application/x-shockwave-flash&quot; width=&quot;210&quot; height=&quot;350&quot; /&gt;&lt;a href=&quot;http://www.meebo.com/rooms&quot; class=&quot;mcrmeebo&quot;&gt;&lt;img alt=&quot;http://www.meebo.com/rooms&quot; src=&quot;http://widget.meebo.com/b.gif&quot; width=&quot;210&quot; height=&quot;45&quot; style=&quot;border:0px&quot;/&gt;&lt;/a&gt;&lt;/div&gt;'></div>

Content code should be put in the placeholder's attribute code. Code shouln't contain HTML special characters
Name the image's id as <placeholder's id>image

*/
function collapseMedia(element)
{
	if (document.getElementById(element).style.display=="none")
	{
		if (document.getElementById(element).innerHTML=='')
			document.getElementById(element).innerHTML = unescape(document.getElementById(element).getAttribute('code'));
		document.getElementById(element).style.display="block";
		document.getElementById(element+'image').src="http://www.google.com/mb/minus_sm.gif";
	}
	else
	{
		document.getElementById(element).style.display="none";   
		document.getElementById(element+'image').src="http://www.google.com/mb/plus_sm.gif";
	}		
}
