﻿// Grey out page
function greyoutContent(vis) {

  var cdark = document.getElementById('greyoutcontent');

  if (vis) {
    //set the shader to cover the entire page and make it visible.
    cdark.style.opacity = 0.7;                      
    cdark.style.MozOpacity = 0.7;                   
    cdark.style.filter = 'alpha(opacity=' + 70 + ')'; 
    cdark.className = 'greyout';		
  } else {
     cdark.className = 'hidden';
  }
}

/*
    AJAX Method Failure
*/
function ajaxfailure(ex, ctx, methodName) { 
    if (ex.get_message() == "Session Timeout" || ex.get_message() == "Authentication failed.") {
        window.location.href = "/signin.aspx?to=true";
        return false;
    }
    if (!(ex.get_message().startswith("The server method '") && ex.get_message().endswith("' failed."))) {
        alert(ex.get_message()); 
    }
    // get_stackTrace(), get_message(), 
    // get_statusCode(), get_timedOut() 

    // show lists and tools and hide add form
    /*
    switch (methodName) {
        case "Signin_User":
            enableform(document.getElementById("signinform"));
    }
    */
    // Hide any passed loader animations
    if (ctx) {
        if (ctx.greyoutcontent == "true") {
            greyoutContent(false);
        }
    }
} 

// Ends With
if (!String.prototype.endswith) {
  String.prototype.endswith = function(suffix) {
    var startPos = this.length - suffix.length;
    if (startPos < 0) {
      return false;
    }
    return (this.lastIndexOf(suffix, startPos) == startPos);
  };
}
// Starts With
if (!String.prototype.startswith) {
  String.prototype.startswith = function(prefix) {
    return (this.indexOf(prefix) == 0);
  };
}

/*
    Validations
*/
function ValidateItem(ftype, fvalue)
{
    switch(ftype)
    {
	    case "integer":
	        var objRegExp = /(^-?\d\d*$)/;
            //check for integer characters
            return objRegExp.test(fvalue);
	    case "decimal":
            var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
            //check for numeric characters
            return objRegExp.test(fvalue);
	    case "currency":
            var objRegExp = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
            //check for numeric characters
            return objRegExp.test(fvalue);
	    case "email":
            var objRegExp = /^.+@[^\.].*\.[a-z]{2,}$/;
            //check for format
            return objRegExp.test(fvalue);
	    case "date":
            var objRegExp;
            var dValue = fvalue;
            if (dValue.indexOf(" ") > -1) {
                // Date and time format
                objRegExp = /^\d{1,2}(\-|\/)\d{1,2}\1\d{4}\s\d{1,2}\:\d{1,2}$/;
            }
            else {
                // date format
                objRegExp = /^\d{1,2}(\-|\/)\d{1,2}\1\d{4}$/;
            }
            //check to see if in correct format
            if (!objRegExp.test(dValue)) {
                return false;
	        }
            else {
                if (dValue.indexOf(" ") > -1) {
                    dValue = dValue.substr(0, dValue.indexOf(" "));
                }

                var strSeparator = dValue.substring(2,3) //find date separator
                var arrayDate = dValue.split(strSeparator); //split date into month, day, year
                //create a lookup for months not equal to Feb.
                var arrayLookup = {'01':31, '03':31, '04':30, '05':31, '06':30, '07':31, '08':31, '09':30, '1':31, '3':31, '4':30, '5':31, '6':30, '7':31, '8':31, '9':30, '10':31, '11':30, '12':31}
                // Get month day and year
                var intDay = parseInt(arrayDate[0],10); 
                var intMonth = parseInt(arrayDate[1],10);
                var intYear = parseInt(arrayDate[2]);

                //check if month value and day value agree
                if(arrayLookup[intMonth] != null) {
                    if(intDay <= arrayLookup[intMonth] && intDay != 0) {
                        //found in lookup table, good date
                        return true;
                    }
                }

                //check for February
                if (intMonth == 2) { 
                    if (intDay > 0 && intDay < 29) {
                        // Good date
                        return true;
                    }
                    else if (intDay == 29) {
                        if ((intYear % 4 == 0) && (intYear % 100 != 0) || (intYear % 400 == 0)) {
                            // year div by 4 and ((not div by 100) or div by 400) ->ok
                            // Good date
                        return true;
                        }   
                    }
                }
            }  
            //any other values, bad date
            return false;
    }
} 


// Newsletter Unsubscribe
function unsubscribe() {
    if (document.getElementById("txtunsubscribeemail").value == "") {
       return false;
    }
    if (!ValidateItem("email", document.getElementById("txtunsubscribeemail").value)) {
        alert("Invalid e-mail address");
       return false;
    }
    PageMethods.Newslstter_Unsubscribe(document.getElementById("txtunsubscribeemail").value, unsubscribesuccess, ajaxfailure);
}

function unsubscribesuccess(value, ctx, methodName) { 
    document.getElementById("unsubscribeform").className = "hidden";
    document.getElementById("unsubscribeconfirm").className = "";
}

