//this function is use to remove unnecessary attributes that do not need in the e-mail body
/*function compare(attribute)
{

    //alert(attribute);
        
    if(attribute == "ps_session")
    {
	return 0;
    }
    else
    {
    
	return 1;
    }
    
	
}//end compare function*/

//see if a word exists in a string
function match(sentence,attrid)
{
    var inthere= sentence.match(attrid);
    
    if((sentence =='func') || (sentence == 'page') || (sentence == 'Order'))
    {
	return 0;
	//alert("CHII");
    }
    
    //alert(sentence);
    //alert(inthere);
    
    //if(inthere == null)
    //if(inthere == '')
    if(inthere)
    {
        return 1;
    }
    
    return 0;
}//end compare function

//get length of a sentence
function getlength(sentence)
{
    return sentence.length;
}

//get position of "=" symbol
function getFirst(sentence)
{
    return sentence.indexOf("=");

}


function sendEmail(Form, Address, Subject, CC, BCC, ReplyTo) {

    var emailBody;
    var emailTo;
    var emailSubject;
    var emailCC;
    var emailBCC;

    var emailURL; // The URL that will open the user's email client

    // temporary variables
    var elementCount;
    var elementIsHeader;

    var i;
    var j;
    var result;
    var stop=1;
    var include;
    var remove;
    var word="AttrID";
    var replace="";
    var len=0;
    var first=0;
    var last=0;
    var cust_details=0;
    
    var attrID;
    // Set some default values
    emailBody = "Product Details - ";
    emailTo = Address;
    //emailSubject = Subject;
    emailSubject = "Enquiry regarding ";
    emailCC = CC ? CC : '';
    emailBCC = BCC ? BCC : '';
    emailReplyTo = ReplyTo ? ReplyTo : '';
    
    for(i = 0; i < Form.elements.length; i++) {
	fe = Form.elements[i];
	elementIsHeader = 0;

	/* Check for special form elements whose names start with 'Mailto_'
	   These are used to set our headers from HTML form elements instead
	   of from the function parameters.
	 */
	
	switch(fe.name) {
	case "Mailto_To":
	    emailTo = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_Subject":
	    emailSubject = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_CC":
	    emailCC = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_BCC":
	    emailBCC = fe.value;
	    elementIsHeader = 1;
	    break;
	case "Mailto_ReplyTo":
	    emailReplyTo = fe.value;
	    elementIsHeader = 1;
	    break;
	}

	if(!elementIsHeader) {
	    switch(fe.type) {

	    case "hidden":
	    case "password":
	    case "text":
	    case "textarea":
		
		if(fe.value.length > 0) {
			
			    if(fe.name != 'product_id')
			    {	
				//work in here
				//alert(word);
				remove = match(fe.name,word);
				if(remove == 1)
				{
				    attrID = fe.value;
				}
				
				else(remove != 1)
				{
				include=match(fe.name,attrID);//find out if correct attribute
				if(include == 1)
				{
				    len=getlength(fe.name);
				    first = getFirst(fe.name);
				    //add a function here to get the fe.name and return its proper value
				    if(first == -1)
				    {
					emailBody += fe.name + ": " + fe.value +  " ";
				    }
				    else
				    {
					emailBody += "\n" + fe.name.substring(0,first) + ": " + fe.value;
					//emailBody += "\n" + fe.name + ": " + fe.value;
				    }	
				}//end if include==1
				
				}//end else remove!=1
			    }//if !=product_id
			    
			    if(fe.name == 'product_name'){
				//emailSubject += fe.name + ": " + fe.value + "; ";
				emailSubject += "Product Name" + ": " + fe.value +  " on your website";
			    }
			    
		}
		break;

	    case "select-multiple":
	    case "select-one":
		elementCount = 0;
		for(j = 0; j < fe.options.length; j++) {
		    if(fe.options[j].selected) {
			elementCount++;
			if(elementCount > 1) {
			    //emailBody +=  "  " + fe.options[j].value;
			} else {
			    remove = match(fe.name,word);
			    if(remove!=1)
			    {
				emailBody += fe.name + ": " + fe.options[j].value;//this is where display attribute chosen
			    }
			    attrID = fe.options[j].value;
			}
		    }
		}
		if(elementCount > 0) {
		    emailBody += "  ";
		}
		break;

	    case "radio":
	    case "checkbox":
		if(fe.checked) {
		    emailBody += fe.name + ": " + fe.value + "\n";
		}
		break;
		
	    case "button":
	    case "image":
	    case "submit":
	    case "file":
		/* We do not record the name or value of buttons.  Please 
		   note that if a button is named with one of the special
		   element names (Mailto_To, Mailto_Subject etc.) that it
		   will not be ignored.  This means that you can have 
		   several different buttons, and name them Mailto_Subject
		   with different values.  Whichever button the user 
		   clicks on will cause the subject to be set.
		*/
		break;

	    default :
		// If we have never heard of this type of input we record
		// the name, input type, and value.
		emailBody += fe.name + "(" + fe.type + "): " + fe.value + "\n";
	    }
	}
    }
	emailBody +="\n\nPlease type your enquiry below\n";

    emailURL = "mailto:" + escape(emailTo) + 
	"?subject=" + escape(emailSubject) +
	"&cc=" + escape(emailCC) +
	"&bcc=" + escape(emailBCC) +
	"&replyto=" + escape(emailReplyTo) +
	"&body=" + escape(emailBody);

    window.location = emailURL;

    return true;
}
