/*
   updateFormField: Update a form item only
   This function is used by all DAP's to update form fields as preparation work for LookupDispatchAction
   The submit happends when user invokes an image, and hence should not be called as part of this javascript function
*/
function updateFormField(formNumber,fieldNumber,value) {
   document.forms[formNumber].elements[fieldNumber].value = value;
   // alert('setting field ' + document.forms[formNumber].elements[fieldNumber].name + ' to value ' + value);
   // document.forms[formNumber].submit();
}

/*
   setFormTarget: Update a form's target so results display in another window
*/
function setFormTarget(formNumber,windowName) {
	document.forms[formNumber].target=windowName;
}


/*
   updateFormFieldSubmit: Update a form item and submit the form
   This function is useful for filter/order logic where a link is clicked and the form should submit,
   passing the order or filter parameter to the action.
   Note: Should not be called as part of DAP processing for LookupDispatchAction. Will endup in double submissions...
*/
function updateFormFieldSubmit(formNumber,fieldNumber,value) {
   document.forms[formNumber].elements[fieldNumber].value = value;
   document.forms[formNumber].submit();
}


/* Update image source with a file path
   This function is useful for image file uploads, to display a preview of the uploaded file before it is
   sent to the server */
function setPreviewLogo(fileInputElement,imageName,serverPath,existingLogo,defaultImage) {
   var tempImage = eval("document." + imageName);
   if (fileInputElement.value != null && fileInputElement.value != "")
   {
      tempImage.src = fileInputElement.value;
   }
   else if (existingLogo != null && existingLogo != "")
   {
      tempImage.src = serverPath + "" + existingLogo;
   }
   else
   {
      tempImage.src = defaultImage;
   }
}

/* Offer detail popup
   This function provides a pre-formatted popup window for VRO Member details */
function vroDetail(url) {
	var curURL = window.location.href;
	curURL = curURL.toLowerCase();
	if (curURL.indexOf("bankofamerica") != -1 || curURL.indexOf("boa") != -1 || curURL.indexOf("bofa") != -1 ){
		// all bank of american pop up should be 625 x 345
		window.open(url, 'detail', "width=625,height=345,resizable,scrollbars");
	}
	else
	{
		window.open(url, 'detail', "width=455,height=500,resizable,scrollbars");
	} 
}


/* Generic popup window
   This function provides functionality to open a customizable child-window. */
function openWindow(url,windowname,width,height,params) {
   var parameters = "width=" + width + ",height=" + height + "," + params;
   window.open(url,windowname,parameters);
}

/* popup([url],[name],[width],[height]) */
function popUp(url,name,wd,ht)
{
   var params = "width=" + wd + ",height=" + ht + ",scrollbars";
   var popUpWindow = window.open(url,name,params);
}

function smartSubmit(formItem,params) {
   var theValue = formItem.value;
   if(theValue != "")
   {
      var place = theValue.indexOf(",");
      var contentId = theValue.substring("0", place);
      var contentName = theValue.substring(place+1);

      while(contentName.indexOf(" ") != -1)
      {
         var pos = contentName.indexOf(" ");
         contentName = contentName.substring("0",pos) + contentName.substring(pos+1);
      }
      location.href = contentName + "/vro_details.jsp?offerId=" + contentId + "&offerName=" + contentName + "&" + params;
   }
}

/* Show form details
   This function provides functionality to construct a url based on form parameters and redirect to the newly create url */
function showFormDetails(formName,elementName,analyticsTags,relativePath) {
   var selectedValue = eval("document." + formName + "." + elementName + ".value");
   if(selectedValue != "")
   {
      var id = selectedValue.substring(0,selectedValue.indexOf(","));
      var name = selectedValue.substring(selectedValue.indexOf(",")+1);
	  var dspName = "";

      var type = eval("document." + formName + "." + elementName + ".name");
      var nameParam = 'merchantName';
      var idParam = 'merchantId';

      if(type == "category") {
         nameParam = 'categoryName';
         idParam = 'categoryId';
      }
      else if(type == "location") {
         nameParam = 'locationName';
         idParam = 'locationId';
      }

      if(relativePath == null) {
         relativePath = "";
      }
		
		var curURL = location.href
	  if (curURL.indexOf("/detail/") == - 1)
	  {
	  	relativePath = relativePath + "detail/";
	  }
	  
	  if ( typeof merchant_ids == "undefined")
	  {}
	  else
	  {
	  		for(var i=1; i<merchant_ids.length; i++){
	  		if (merchant_ids[i] == selectedValue){
				dspName = "|" + merchant_names[i];
				break;
			}	 
	  	}
	  }
      var url = relativePath + name + "/vro_details.jsp?" + nameParam + "=" + name + "&" + idParam + "=" + id + "&" + analyticsTags + dspName;
      location.href = url;
   }
}




/* Show small biz landing page form details

   This function provides functionality to construct a url based on form parameters and redirect to the newly create url
   This is a temp search function for new small biz landing page 3-19-07*/
   
function showSMBizFormDetails(formName,elementName,analyticsTags,relativePath) {
   var selectedValue = eval("document." + formName + "." + elementName + ".value");
   if(selectedValue != "")
   {
      var id = selectedValue.substring(0,selectedValue.indexOf(","));
      var name = selectedValue.substring(selectedValue.indexOf(",")+1);
	  var dspName = "";

      var type = eval("document." + formName + "." + elementName + ".name");
      var nameParam = 'merchantName';
      var idParam = 'merchantId';

      if(type == "category") {
         nameParam = 'categoryName';
         idParam = 'categoryId';
      }
      else if(type == "location") {
         nameParam = 'locationName';
         idParam = 'locationId';
      }

      if(relativePath == null) {
         relativePath = "";
      }
		
		var curURL = location.href
	  if (curURL.indexOf("/detail/") == - 1)
	  {
	  	relativePath = relativePath + "/business/small_business/" + "detail/";
	  }
	  
	  if ( typeof merchant_ids == "undefined")
	  {}
	  else
	  {
	  		for(var i=1; i<merchant_ids.length; i++){
	  		if (merchant_ids[i] == selectedValue){
				dspName = "|" + merchant_names[i];
				break;
			}	 
	  	}
	  }
      var url = relativePath + name + "/vro_details.jsp?" + nameParam + "=" + name + "&" + idParam + "=" + id + "&" + analyticsTags + dspName;
      location.href = url;
   }
}

  //for filling out discounts dropdowns on segment landing pages
  function fillDropDown(drp){
	  for(var i=1; i<merchant_ids.length; i++){
		  drp.options[i]=new Option(merchant_names[i], merchant_ids[i], false, false);
		}
	}
	
	//for showing the discount selected on a segment landing page.
	function showSegmentDiscount(formName,elementName){
	  showFormDetails('shopMerch','merchant','pageId='+pageId+'&it=il|'+document.location.href,relativePath);
	}

/* Show form details for Signature
   This function provides functionality to construct a url based on form parameters and redirect to the newly create url */
function showFormDetailsForSig(formName,elementName,analyticsTags,relativePath,sigFlag) {
   var selectedValue = eval("document." + formName + "." + elementName + ".value");
   if(selectedValue != "")
   {
      var id = selectedValue.substring(0,selectedValue.indexOf(","));
      var name = selectedValue.substring(selectedValue.indexOf(",")+1);
	  var dspName = "";

      var type = eval("document." + formName + "." + elementName + ".name");
      var nameParam = 'merchantName';
      var idParam = 'merchantId';

      if(type == "category") {
         nameParam = 'categoryName';
         idParam = 'categoryId';
      }
      else if(type == "location") {
         nameParam = 'locationName';
         idParam = 'locationId';
      }

      if(relativePath == null) {
         relativePath = "";
      }

		var curURL = location.href
	  if (curURL.indexOf("/detail/") == - 1)
	  {
	  	relativePath = relativePath + "detail/";
	  }

	  if ( typeof merchant_ids == "undefined")
	  {}
	  else
	  {
	  		for(var i=1; i<merchant_ids.length; i++){
	  		if (merchant_ids[i] == selectedValue){
				dspName = "|" + merchant_names[i];
				break;
			}
	  	}
	  }
      var url = '';

      if (id == "0") {
        url = relativePath + name + "/vro_details.jsp?" + nameParam + "=" + name + "&" + idParam + "=" + id + "&" + analyticsTags + dspName + "&signatureFlag=1";
      } else {
        url = relativePath + name + "/vro_details.jsp?" + nameParam + "=" + name + "&" + idParam + "=" + id + "&" + analyticsTags + dspName + "&signatureFlag=" + sigFlag;
      }
      location.href = url;
   }
}