var itemSendId;
function recommenditem(itemTypeId,itemTypeName,itemImage,itemAuthor,itemPrice,itemPriceUsd,shippingInfo,bindingInfo)
{
    document.getElementById("RecommendTable").style.display='block';
	document.getElementById("RecommendItem"+itemTypeId).style.display='block';
	var lSearchElement;
	var lSearch;
	var lEntityConfiguration;
	var lRequest;
	var lXMLResponse;
	var lRoot;
	itemid =itemTypeId;
	itemName =itemTypeName;	

	var lEntity="scscrecommenditem";
	gEntityConfig = eval("entityConfiguration." + lEntity);	
	lEntityConfiguration = gEntityConfig;
	gCurrentEntity = lEntityConfiguration.entity;
	gCurrentSearchTemplate = lEntityConfiguration.searchTemplate;
	gCurrentAddEditTemplate = lEntityConfiguration.addEditTemplate;
	gCurrentListTemplate = lEntityConfiguration.listTemplate;
	gCurrentViewTemplate = lEntityConfiguration.viewTemplate;
	gCurrentEntityIdField = lEntityConfiguration.entityIdField;
	gCurrentEntityListURL = lEntityConfiguration.URLForEntityList;
	gCurrentEntityPreEditURL = lEntityConfiguration.URLForEntityPreEdit;
	gCurrentEntitySaveURL = lEntityConfiguration.URLForEntitySave;
	gCurrentEntityPreURL = lEntityConfiguration.URLForPreFetch;
	gCurrentEntityDeleteURL = lEntityConfiguration.URLForEntityDelete;
	gPostAddEdit = lEntityConfiguration.postAddEdit;
	gPostSearch = lEntityConfiguration.postSearch;
	gPostEdit = lEntityConfiguration.postEdit;
	gPostList = lEntityConfiguration.postList;
	gClearAddEdit = true;
	//Check if the search element for the entity is available in Cache.
	//lSearchElement = gCache.get(pCurrentEntity + "SEARCH");
	lSearchElement = null;
	if (lSearchElement == null)
	{
		//if it is not available then fetch the template from the server.
		var lAJAXInteraction = new AJAXInteraction(gCurrentAddEditTemplate+'?itemTypeId='+itemTypeId+'&itemTypeName='+itemTypeName+'&itemImageSrc='+itemImage+'&itemAuthor='+itemAuthor +'&itemPrice='+itemPrice +'&itemPriceUsd='+itemPriceUsd+'&shippingInfo='+shippingInfo+'&bindingInfo='+bindingInfo, null, true);
		lRequest = lAJAXInteraction.doPost('LK=' + gLoginKey);
		lXMLResponse = lRequest.responseXML;
		lRoot = lXMLResponse.documentElement;
		lSearchElement = copyNodes(lRoot);

		//Add the template received to the cache and show the search area.
		//gCache.add(gCurrentEntity + "SEARCH", lSearchElement);
	}	
	showRecommendItem(lSearchElement,itemTypeId);
}
function showRecommendItem(pAddEditElement,itemTypeId)
{
	var lAddEdit;	
	clearDIV("RecommendItem"+itemTypeId);
	lAddEdit = document.getElementById("RecommendItem"+itemTypeId);	
	lAddEdit.appendChild(pAddEditElement);		
	clearForm("RecommendItem"+itemTypeId);
	clearExtras();	
}
function validaterequiredfield(itemId,orgfolder,itemTypeName,itemImage,itemAuthor,itemPrice,itemPriceUsd,ShippingInfo,biddingInfo)
{
	var pformname=eval("document.RecommendItem"+itemId);    
	var ToEmail =  pformname.recommenditemtoemailid;
	var personal =  pformname.recommenditempersonalmessage;
	var yourname =  pformname.recommenditemfromname;
	var yourmail =  pformname.recommenditemfromid;
	var ToEmailId = pformname.recommenditemtoemailid.value;
	var personalMessage = pformname.recommenditempersonalmessage.value;
	var yourName = pformname.recommenditemfromname.value;
	var yourEmailId = pformname.recommenditemfromid.value;
	var check;
	 if (!ToEmailId)
	 {
    	showMessage(MSG_TO_EMAIL_ID);	
		ToEmail.focus();
		return;
	}
	if (!ToEmailId)
	{
		//alert('hi12');
		showMessage(MSG_TO_EMAIL_ID);	
		ToEmail.focus();
		return;
	}
	else
	{	    
	 if (ValidateEmailId(itemId,orgfolder)==false)
	 {		
		ToEmail.focus();
		return;
	 }	 
	}
	//check = ValidateEmailId(itemId,orgfolder);
	if (!personalMessage)
	{
		//alert('hi');
		showMessage(MSG_PERSONAL_MESSAGE);
		personal.focus();
		return;
	}
	if (validateyourname(yourName)== false)
	{		
		//showMessage(MSG_YOUR_NAME);
		yourname.focus();
		return;
	}
		
	if (!yourEmailId)
	{
		//alert('hi12');
		showMessage(MSG_YOUR_EMAIL_ID);	
		yourmail.focus();
		return;
	}
	else
	{	    
	 if (youremailidcheck(yourEmailId)==false)
	 {		
		yourmail.focus();
		return false;
	 }	 
	}

	sendmail(itemId,orgfolder,itemTypeName,itemImage,itemAuthor,itemPrice,itemPriceUsd,ShippingInfo,biddingInfo);
}
   function validateyourname(yourname)
   {
	   if(!yourname)
	   { 
		   showMessage(MSG_YOUR_NAME);
		   return false;
	   }
         var splCharslist = '*|\":<>[]{}`\';()&$#%^';
    	 for (k = 0;k < yourname.length; k++)
		 {
		   if (splCharslist.indexOf(yourname.charAt(k)) != -1)
			{
			   showMessage(MSG_INVALID_YOUR_NAME);		   
               return false;
			}
		 }   
   }
   function ValidateEmailId(itemId,orgfolder)
	{
		
	 var pformname=eval("document.RecommendItem"+itemId);    
	 var emailID =  pformname.recommenditemtoemailid;
     var email=emailID.value.split(',');
	// var ToEmailId = pformname.recommenditemtoemailid.value;
    	if(email.length > 5)
		 {
			showMessage(MSG_MORE_THAN_TO_EMAIL_ID);	  
			return false;
		 }
	
	for(i=0; i < email.length ; i++ )
	{
   	if (emailidcheck(email[i])==false)
	{		   
		emailID.focus();
		return false;
	}
	}
	return;
 }
 
       function emailidcheck(srt) 
	   {		
		 var at="@";
		 var dot=".";
		 var lat=srt.indexOf(at);
		 var lstr=srt.length;
		 var ldot=srt.indexOf(dot);
         var splChars = '*|\":<>[]{}`\';()&$#%^';
    	 for (j = 0;j < srt.length; j++)
		 {
		   if (splChars.indexOf(srt.charAt(j)) != -1)
			{
			   showMessage(INVALID_EMAIL_ID);		   
               return false;
			}
		 }
		 if (srt.indexOf(at)==-1)
		 {    	   
		   showMessage(INVALID_EMAIL_ID);		   
		   return false;
		 }
		 if (srt.indexOf(at)==-1 || srt.indexOf(at)==0 || srt.indexOf(at)==lstr)
		 {     	   
		   showMessage(INVALID_EMAIL_ID);
		   return false;
		 }
		 if (srt.indexOf(dot)==-1 || srt.indexOf(dot)==0 || srt.indexOf(dot)==lstr)
		 {        	
		    showMessage(INVALID_EMAIL_ID);
		    return false;
		 }
		 if (srt.indexOf(at,(lat+1))!=-1)
	     {        	
		    showMessage(INVALID_EMAIL_ID);
		    return false;
		 }
		 if (srt.substring(lat-1,lat)==dot || srt.substring(lat+1,lat+2)==dot)
		 {
     	    showMessage(INVALID_EMAIL_ID);
		    return false;
		 }
		 if (srt.indexOf(dot,(lat+2))==-1)
		 {     	   
		    showMessage(INVALID_EMAIL_ID);
		    return false;
		 }
		 if (srt.indexOf(" ")!=-1)
	     {     	  
		    showMessage(INVALID_EMAIL_ID);
		    return false;
		 }
 			return true;
	} 
       function youremailidcheck(srt) 
	   {
		 var at="@";
		 var dot=".";
		 var lat=srt.indexOf(at);
		 var lstr=srt.length;
 		 var ldot=srt.indexOf(dot);
         var splChars1 = '*|\":<>[]{}`\';()&$#%^';
    	 for (j = 0;j < srt.length; j++)
		 {
		   if (splChars1.indexOf(srt.charAt(j)) != -1)
			{
			   showMessage(INVALID_YOUR_EMAIL_ID);		   
               return false;
			}
		 }
		 if (srt.indexOf(at)==-1)
		 {
    	   //alert(srt+'1');
		   showMessage(INVALID_YOUR_EMAIL_ID);		   
		   return false;
		 }
		 if (srt.indexOf(at)==-1 || srt.indexOf(at)==0 || srt.indexOf(at)==lstr)
		 {
     	   //alert(srt+'2');
		   showMessage(INVALID_YOUR_EMAIL_ID);
		   return false;
		 }
		 if (srt.indexOf(dot)==-1 || srt.indexOf(dot)==0 || srt.indexOf(dot)==lstr)
		 {
        	//alert(srt+'3');
		    showMessage(INVALID_YOUR_EMAIL_ID);
		    return false;
		 }
		 if (srt.indexOf(at,(lat+1))!=-1)
	     {
        	//alert(srt+'4');
		    showMessage(INVALID_YOUR_EMAIL_ID);
		    return false;
		 }
		 if (srt.substring(lat-1,lat)==dot || srt.substring(lat+1,lat+2)==dot)
		 {
     	   //alert(srt+'5');
		    showMessage(INVALID_YOUR_EMAIL_ID);
		    return false;
		 }
		 if (srt.indexOf(dot,(lat+2))==-1)
		 {
     	   //alert(srt+'6');
		    showMessage(INVALID_YOUR_EMAIL_ID);
		    return false;
		 }
		 if (srt.indexOf(" ")!=-1)
	     {
     	   //alert(srt+'7');
		    showMessage(INVALID_YOUR_EMAIL_ID);
		    return false;
		 }
 			return true;		
	} 
function sendmail(itemTypeId,orgfolder,itemTypeName,itemImage,itemAuthor,itemPrice,itemPriceUsd,shippingInfo,bindingInfo)
{
    var pformname=eval("document.RecommendItem"+itemTypeId);  
	showOnlyEntity('scscrecommenditem');   	
	var lDisable = document.getElementById("SendMail");
	lDisable.disabled = "true";
	itemSendId = itemTypeId;
	submitForm(pformname,gCurrentEntitySaveURL+'?orgfolder='+orgfolder+'&itemId='+itemTypeId+'&itemTypeName='+itemTypeName+'&itemImageSrc='+itemImage+'&itemAuthor='+itemAuthor +'&itemPrice='+itemPrice +'&itemPriceUsd='+itemPriceUsd+'&shippingInfo='+shippingInfo+'&bindingInfo='+bindingInfo, "POST", handleSendMail, gLoginKey, null);
}
function handleSendMail(pXMLObject)
{
	var lXMLResponse;
	var lRoot;
	var lStatus;
	var lResult;
	var lStatus;
	var lResponse;
	var lCount;
	var lFocusField;
	var lCount;
	var lSuccess;
	lXMLResponse = pXMLObject.responseXML;
	lRoot = lXMLResponse.documentElement;
	lRoot = copyNodes(lRoot);
	lResult = lRoot.childNodes[0];	
	lSuccess = handleResultformail(lResult, "RecommendItem"+itemSendId);	
	clearForm("RecommendItem"+itemSendId);
	clearDIV("RecommendItem"+itemSendId);
		
	if (lSuccess)
	{
		//clearForm("RecommendItem"+itemSendId);
		lAddEdit = document.getElementById("RecommendItem"+itemSendId);
		if (lAddEdit.childNodes != null)
		{
			for (lCount = lAddEdit.childNodes.length - 1; lCount >= 0; lCount--)
			{
				lAddEdit.removeChild(lAddEdit.childNodes[lCount]);
			}
		}
		//clearDiv("RecommendItem"+itemSendId);
	}
}
function handleResultformail(pResult, pFormName)
{
	var lSuccess;
	var lStatus;
	var lMessage;
	var lParam;
	var lFocusField;
	var lCount;
	lSuccess = false;	
	if (pResult.attributes != null)
	{
		lStatus = null;
		lMessage = null;
		lStatus = pResult.attributes.getNamedItem("STATUS").value;		
		if (pResult.attributes.getNamedItem("MESSAGE") != null)
		{
			lMessage = pResult.attributes.getNamedItem("MESSAGE").value;
		}		
		
		if (lStatus != null)
		{
			lSuccess = (lStatus == "0");
			if (lMessage != null)
			{
				if(lStatus == "1")
				{
					//clearDiv("RecommendItem"+itemSendId);
				}
				if(lStatus == "2")
				{
					//clearDiv("RecommendItem"+itemSendId);
				}
				showMessage(lMessage);
				
			}
			
			if(lStatus != "2")
			{
				lFocusField = pResult.attributes.getNamedItem("FOCUS");
				if(lFocusField != null)
				{
					//eval("document." + pFormName + "." + lFocusField.value + ".focus()");
				}
			}
		}
	}	
	return lSuccess;
}
