/*
   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=655,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;
   }
}

//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 event details
   This function provides functionality to construct a url based on form parameters and redirect to the newly create url */
function showEventDetails(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 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 the path does not contain specialEvents, then the full path has to be set from events.jsp.
	  //This happens when the user is doing a "confirm select", as the outer containing page does not
	  //have the correct context for this call.
	  if (curURL.indexOf("/specialEvents/") == - 1)
	  {
	  	relativePath = relativePath + "/specialEvents/";
	  }
	  else relativePath = "";

      var url = relativePath + name + "/vro_eventsDetails.jsp?" + nameParam + "=" + name + "&" + idParam + "=" + id + "&" + analyticsTags;
      location.href = url;
  }
}
function showPopup(title, theText)
{
	var params = "width=455,height=455,scrollbars=yes";
	tipwin = window.open("","Terms_And_Conditions",params);
	var htmlText = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'><html xmlns='http://www.w3.org/1999/xhtml'><head><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>Visa USA | Personal | Visa Signature</title><style type=\'text/css\'>@import url('http://usa.visa.com/css/global.css');@import url('http://usa.visa.com/css/content.css');@import url('http://usa.visa.com/css/personal.css');</style></head><body><div id=\'content\'><div style=\'margin:10px 10px 10px 10px; width:400px\'><p><img src=\'http://usa.visa.com/img/logo_visa.gif\'></p><h3>" + title + " Offer Terms and Conditions</h3><br />" + theText + "</div></div></body></html>";

	tipwin.document.write(htmlText);

	if (document.images)
	{
		tipwin.focus()
	}
	
	tipwin.document.close();
}
/* Show USA event details
   This function provides functionality to construct a url based on form parameters and redirect to the details url */
function showUSAEventDetails(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 type = eval("document." + formName + "." + elementName + ".name");
      if(type == "category") {
         nameParam = 'categoryName';
         idParam = 'categoryId';
      }
      else if(type == "location") {
         nameParam = 'locationName';
         idParam = 'locationId';
      }

      if(relativePath == null) {
         relativePath = "";
      }
      var url = relativePath + "?" + nameParam + "=" + name + "&" + idParam + "=" + id + "&" + analyticsTags;
      location.href = url;
  }
}




















