// Site Services JS functions
var SSDetails = null;

// Pops up new window
function PopUp(url,w,h,l,t) 
{
	wlNewWin = window.open(url, '', 'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,left=' + l + ',top=' + t + ',height=' + h + ',width=' + w);
}

// Pops up new window
function PopUpNamed(url, w,h,l,t, winObj) 
{
  var wlWin
  if (winObj != null)
  {
    // if a window is open load in it
    if (!winObj.closed) {
      winObj.location = url;
      winObj.focus();
      return winObj;
    }
  }    
  wlWin = window.open(url, '', 'toolbar=no,location=no, address=no, titlebar=no,directories=no,status=no,menubar=no,scrollbars=1,resizable=1,left=' + l + ',top=' + t + ',height=' + h + ',width=' + w);
  if (wlWin != null)
        wlWin.focus();
  
  return wlWin;
}

// Auto tabs through text inputs
function MoveNextField(len, caller, next)
{
	if (event.keyCode > 48)
	    if (caller.value.length == len)
		    next.focus();
}

// Change the day values in a dropdown to reflect the number of days in a selected month
function ChangeDays(selectedName)
{
    var thisIndex = -1;
    var days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

    for(x = 0; x < document.forms[0].elements.length; x++)
    {
	    if(document.forms[0].elements[x].name == selectedName)
	    {
	        thisIndex = x;
		    daysIndex = x+1;
		    break;
	    }
    }

    thisMonth = document.forms[0].elements[thisIndex].selectedIndex;
    document.forms[0].elements[daysIndex].length = 1;

    for(i = 2; i <= days[thisMonth]; i++)
    {
        document.forms[0].elements[daysIndex].options[i-1] = new Option(i, i);
	}		
}

// Change the day values in a dropdown to reflect the number of days in a selected month
//Assuming date/month/year format
function ChangeDaysUK(selectedName)
{

    var thisIndex = -1;
    
    var days;
    
    if(IsLeapYear())
    {
        days = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    }
    else
    {
        days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    }
    

    for(x = 0; x < document.forms[0].elements.length; x++)
    {
	    if(document.forms[0].elements[x].name == selectedName)
	    {
	        thisIndex = x;
		    daysIndex = x-1;
		    break;
	    }
    }
    
    selDay = document.forms[0].elements[daysIndex].value;
    thisMonth = document.forms[0].elements[thisIndex].selectedIndex;
    document.forms[0].elements[daysIndex].length = 1;

    for(i = 2; i <= days[thisMonth]; i++)
    {
        document.forms[0].elements[daysIndex].options[i-1] = new Option(i, i);
	}		
	
	if(selDay > 1)
	{
	    if(selDay <= days[thisMonth])
	    {
	        document.forms[0].elements[daysIndex].selectedIndex = selDay - 1;
	    }
	    else
	    {
	        document.forms[0].elements[daysIndex].selectedIndex = 0;
	    }
	}
}


function OnLoadFocus(ElementId)
{
    window.onload = function()
    {
        document.getElementById(ElementId).focus();
    }
}

function ResetForm(formName)
{
    document.forms[formName].reset();
}

function DownloadCoupon(url, width, height, left, top, question, idx) {
		
	if (question != '') {
		
		if (confirm(question)) {
			WLNewWin = window.open('','WLNewWin',"toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);								
			eval('document.frm' + idx).submit();
			WLNewWin.focus();
		}		
	 }
	 
	else {
		
		WLNewWin = window.open('','WLNewWin',"toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=" + width + ",height=" + height + ",left=" + left + ",top=" + top);								
		eval('document.frm' + idx).submit();
		WLNewWin.focus();
	}
				
}

function IsLeapYear()
{ 
        var d = new Date();
        var thisYear = d.getFullYear();
        
        var yr= thisYear;
        var isLeap = false;
        
        if ((parseInt(yr)%4) == 0)
        { 
            if (parseInt(yr)%100 == 0)
            { 
                if (parseInt(yr)%400 == 0)
                { 
                    isLeap = true; 
                } 
            } 
            else
            { 
                isLeap = true; 
            } 
        } 
        
        return isLeap;
}

function DisplayAndRecordSurvey()
{
    jQuery.ajax({
        type: "POST",
        url: "/Benefits/LocalDiscount/Survey.rails", 
        data: "purpose=siteEvent"
    });

    PopUp('http://www.zoomerang.com/survey.zgi?p=WEB226F9U2YH82',700,600,0,0);
}
