/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		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){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateDate(dateField){
	//var dt=document.frmSample.txtDate
	if (isDate(dateField)==false){
		//dt.focus()
		return false
	}
    return true
 }
 
function setUserSelections(formObj)
{   	
    setCheckedValue(formObj.elements['CalendarType'], jCalendarType);
    formObj.InsideColor.value = jCalendarInnerColor;
    formObj.OutsideColor.value = jCalendarBorderColor;
    formObj.CalendarStyle.value = jCalendarStyle;
    setQuoteImage(formObj);
}

function MTRValidateForm_old(formObj)
{
    if (validateForm(formObj))

        if (ValidateDate(formObj.CustomerDateofSale.value))
            if(!preview_window())
				submitenabled(formObj);
        else
        {
            formObj.CustomerDateofSale.focus;
            submitenabled(formObj);
        }
	return false;
}

function MTRValidateUpdateForm(formObj)
{
    if (validateForm(formObj))
	 {
		submitenabled(formObj);
		//submitonce(formObj);
		//alert(formObj.Continue.disabled);
		//formObj.Continue.disabled = true;
		formObj.Continue.value = "Please wait...";
		return true;
	}

	return false;
}


function MTRValidateForm(formObj)
{
    if (validateForm(formObj))
    {
		submitenabled(formObj);
		//submitonce(formObj);
		//alert(formObj.Continue.disabled);
		//formObj.Continue.disabled = true;
		formObj.Continue.value = "Please wait...";
		return true;
	}

	return false;
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) 
{
	if (!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == null) 
	{
	    radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
    for(var i = 0; i<radioLength; i++) 
    {
        radioObj[i].checked = false;
        if(radioObj[i].value == newValue.toString()) 
        {
            radioObj[i].checked = true;
        }
    }
}

function repStr(inStr, frStr, toStr) 
{
    return inStr.split(frStr).join(toStr);
}

function AutoGenerateText(formObj)
{
    if (!ValidateDate(formObj.CustomerDateofSale.value))
    {
        formObj.CustomerDateofSale.focus;
    }
    else
    {
		var d = new Date(formObj.CustomerDateofSale.value);
		var dMonth, saleDate = "";

		if(d.getMonth() == 0)
			dMonth = "Jan"
		else if(d.getMonth() == 1)
			dMonth = "Feb"
		else if(d.getMonth() == 2)
			dMonth = "March"
		else if(d.getMonth() == 3)
			dMonth = "April"
		else if(d.getMonth() == 4)
			dMonth = "May"
		else if(d.getMonth() == 5)
			dMonth = "June"
		else if(d.getMonth() == 6)
			dMonth = "July"
		else if(d.getMonth() == 7)
			dMonth = "August"
		else if(d.getMonth() == 8)
			dMonth = "Sep"
		else if(d.getMonth() == 9)
			dMonth = "Oct"
		else if(d.getMonth() == 10)
			dMonth = "Nov"
		else
			dMonth = "Dec"

		if (d.getFullYear() < 2000)
		{
			d.setFullYear(d.getFullYear() + 100);
		}	 

		if (d.getDate())
		{
			saleDate = dMonth + " " + d.getDate() + ", " + d.getFullYear()
		}
		else
		{
			saleDate = ""
		}

		var temp = "";
		if(jCalendarLine1 != "")
		{
			temp = repStr(jCalendarLine1, "[FN]", formObj.CustomerFirstName.value)
			temp = repStr(temp, "[LN]", formObj.CustomerLastName.value)
			temp = repStr(temp, "[DS]", saleDate)
			temp = repStr(temp, "[MN]", jCustomerFullname)
			formObj.CalendarLine1.value = temp
		}
		if(jCalendarLine2 != "")
		{
			temp = repStr(jCalendarLine2, "[FN]", formObj.CustomerFirstName.value)
			temp = repStr(temp, "[LN]", formObj.CustomerLastName.value)
			temp = repStr(temp, "[DS]", saleDate)
			temp = repStr(temp, "[MN]", jCustomerFullname)
			formObj.CalendarLine2.value = temp
		}
		if(jCalendarLine3 != "")
		{
			temp = repStr(jCalendarLine3, "[FN]", formObj.CustomerFirstName.value)
			temp = repStr(temp, "[LN]", formObj.CustomerLastName.value)
			temp = repStr(temp, "[DS]", saleDate)
			temp = repStr(temp, "[MN]", jCustomerFullname)
			formObj.CalendarLine3.value = temp
		}	
		if(jCalendarLine4 != "")
		{
			temp = repStr(jCalendarLine4, "[FN]", formObj.CustomerFirstName.value)
			temp = repStr(temp, "[LN]", formObj.CustomerLastName.value)
			temp = repStr(temp, "[DS]", saleDate)
			temp = repStr(temp, "[MN]", jCustomerFullname)
			formObj.CalendarLine4.value = temp
		}			

		if(formObj.CalendarLine1.value == "")
		{
			formObj.CalendarLine1.value = "Congratulations" + formObj.CustomerFirstName.value;
			formObj.CalendarLine2.value = "on the purchase of your new home!";
			formObj.CalendarLine3.value = "Thank you  -  " + jCustomerFullname + "  -  " + saleDate; 
		}
    }
}


function checkExtension(ext) 
{
	ext = ext.substring(ext.length-3,ext.length);
	ext = ext.toLowerCase();
	if(ext != "jpg") 
	{
		alert("You selected a ." + ext + " file; please select a .jpg file instead!");
		return false; 
	}
	else
		return true; 
}

function getRadioButtonGroupValue(rGroupId)
{
	var group = document.getElementsByName(rGroupId);
	var result = "";
	for (i=0; i < group.length; i++)
	{
		if (group[i].checked == true)
		{
			result = group[i];
			break;
		}
	}
	if (result == "")
	{	
		return "";
	} 
	else
	{
		return result.value;
	}
}

function setQuoteImage(formObj)
{
	var CalendarType = getRadioButtonGroupValue("CalendarType");		
	var CalInsideColor, CalOutsideColor;

	if (formObj.CalendarStyle.value == "Flag")
	{
	    CalInsideColor = "";
	    CalOutsideColor = "";
	    formObj.InsideColor.disabled=true;
	    formObj.OutsideColor.disabled=true;
	}
	else
	{
	    CalInsideColor = formObj.InsideColor.value
	    CalOutsideColor = formObj.OutsideColor.value;
	    formObj.InsideColor.disabled=false;
	    formObj.OutsideColor.disabled=false;			
	    if (CalInsideColor == "")
	    {
			CalInsideColor = "Gold";
			CalOutsideColor = "Black";
			formObj.InsideColor.value = CalInsideColor;
			formObj.OutsideColor.value = CalOutsideColor;
		}
	}

	if(CalendarType == "Full Size Calendar")
	    document.anchors.item("zoomlink", 0).href = "javascript:void(window.open('zoom.aspx?img=images/options/" + CalendarType + "/zoom/" + formObj.CalendarStyle.value + CalOutsideColor + CalInsideColor + ".jpg" + "','player','toolbar=no,location=no,status=no,directories=no,menubar=no,scrolling=auto,scrollbars=auto,width=365,height=590,resizable=no'));"
	else
	    document.anchors.item("zoomlink", 0).href = "javascript:void(window.open('zoom.aspx?img=images/options/" + CalendarType + "/zoom/" + formObj.CalendarStyle.value + CalOutsideColor + CalInsideColor + ".jpg" + "','player','toolbar=no,location=no,status=no,directories=no,menubar=no,scrolling=auto,scrollbars=auto,width=365,height=590,resizable=no'));"

	document.getElementById("quotediv").innerHTML = "<IMG border='0' height='130' src='images/options/" + CalendarType + "/" + formObj.CalendarStyle.value + CalOutsideColor + CalInsideColor + ".jpg'>";
}
