/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
//window.alert('validateInput.js');
var dtCh= "/";
var minYear=2008;
var maxYear=2100;

function quoteXml (xmlStr) {
var result = new EditableString(xmlStr);
result = result.replaceAll("<", "&lt;");
result = result.replaceAll(">", "&gt;");
result = result.replaceAll("\"", "&quot;");
result = result.replaceAll("'", "&apos;");
return result.data;
}

//+-------------------------------------------------------------
//| unquoteXml removes the quote marks, which is useful when
//| the returned stuff wants to be parsed as xml.
//+-------------------------------------------------------------
function unquoteXml (xmlStr) {
var result = new EditableString(xmlStr);
result = result.replaceAll("&lt;", "<");
result = result.replaceAll("&gt;", ">");
result = result.replaceAll("&quot;", "\"");
result = result.replaceAll("&apos;", "'");
return result.data;
}


//+-------------------------------------------------------------
//| Object type editableString is a string that can be edited with
//| a number of useful methods contained below.
//+-------------------------------------------------------------
function EditableString(str) {
this.data = str;
}

//+-------------------------------------------------------------
//| replaceAll replaces all source strings with destination strings,
//| returning a new EditableString containing the result.
//+-------------------------------------------------------------
EditableString.prototype.replaceAll = function (srcStr, dstStr) {
this.pat = new RegExp(srcStr,"g");
var newStr = this.data.replace (this.pat, dstStr);
return new EditableString(newStr);
}

//expand/collapse div
function doMenu(item) {
 obj=document.getElementById(item);
 col=document.getElementById("x" + item);
 if (obj.style.display=="none") {
  obj.style.display="block";
  col.innerHTML="[-] Collapse";
 }
 else {
  obj.style.display="none";
  col.innerHTML="[+] Expand";
 }
}
function stripIt(x){
x = x.toString().replace(/['"]/g,'');
};
function fixIt(x)
{
    x = x.replace(/\'/g, "&apos;");
    x = x.replace(/\"/g, "&quot;");
  return x;
}
function getCookie(name)
{
	var cookies = document.cookie;
	if (cookies.indexOf(name) != -1)
	{
		var startpos = cookies.indexOf(name)+name.length+1;
		var endpos = cookies.indexOf(";",startpos)-1;
		if (endpos == -2) endpos = cookies.length;
		return unescape(cookies.substring(startpos,endpos));
	}
	else
	{
		return false; // the cookie couldn't be found! it was never set before, or it expired.
	}
}
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		window.alert("Please enter a date in the format: mm/dd/yyyy For example 02/01/2015");
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		window.alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		window.alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		window.alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		window.alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateForm()
{
	var dt=document.frmInputs.weddingDate
	if (isDate(dt.value)==false)
  {
		dt.focus();
		return false;
	}
    //remove cookies, we are updating them
    delete10kTemplateCookies();
    return true;
    
 }
function DeleteCookie(name)
{
   SetCookie(name,"",-1);
}
 function delete10kTemplateCookies()
 {
    if (getCookie("TENK_LISTVALUES") != "")
    {
        DeleteCookie("TENK_LISTVALUES");
      }
    if (getCookie("TENK_LISTDATES") != "")
    {
        DeleteCookie("TENK_LISTDATES");
      }
 }
 function validateBudget()
 {
    //window.alert("validateBudget");
    x = document.frmUserInput.Total.value;
    if (isInteger(x)== true)
    {
        x = Math.round(parseInt(x) / 5) * 5;
        document.frmUserInput.Total.value = x;
    }
 }
 
  function validateInputs()
 {
    //window.alert("validateInputs");
    x = true;
    if (isInteger(document.frmUserInput.Total.value)== true)
    {
    //do nothing
    }
    else
    {
      window.alert("Invalid character for total money in your budget (no decimals accepted).");
      x = false;
      document.frmUserInput.Total.value = "";
    }
    if (isInteger(document.frmUserInput.Occupants.value)== true)
    {
      //do nothing
    }
    else
    {
      window.alert("Invalid character for number of Guests (no decimals accepted).");
      document.frmUserInput.Occupants.value = "";
      x = false;
    }
    return x;
 }
//window.alert('finished loading validateInput.js');
