﻿
//Called by a hide modal routine.
//This function maintains the sign image url, if it exists.
function maintainSignImage() {
	//if we're on the myprofile page, maintain the sign image url.
	var bProfilePage = isMyCornerProfilePage();
	if (bProfilePage) { switchZodiac(); }	
	else { return; }
}

var strFolderPath = '/mycorner/images/signs/';

//This object holds the sign information necessary
//to change the sign.
var signdata = { 
	signs : [
		{	'Sign'	: 'Aquarius',
			'URL'		: strFolderPath + 'sign01.gif' },
		{	'Sign'	: 'Pisces',
			'URL'		: strFolderPath + 'sign02.gif' },
		{	'Sign'	: 'Aries',
			'URL'		: strFolderPath + 'sign03.gif' },
		{	'Sign'	: 'Taurus',
			'URL'		: strFolderPath + 'sign04.gif' },
		{	'Sign'	: 'Gemini',
			'URL'		: strFolderPath + 'sign05.gif' },
		{	'Sign'	: 'Cancer',
			'URL'		: strFolderPath + 'sign06.gif' },
		{	'Sign'	: 'Leo',
			'URL'		: strFolderPath + 'sign07.gif' },
		{	'Sign'	: 'Virgo',
			'URL'		: strFolderPath + 'sign08.gif' },
		{	'Sign'	: 'Libra',
			'URL'		: strFolderPath + 'sign09.gif' },
		{	'Sign'	: 'Scorpio',
			'URL'		: strFolderPath + 'sign10.gif' },
		{	'Sign'	: 'Sagittarius',
			'URL'		: strFolderPath + 'sign11.gif' },
		{	'Sign'	: 'Capricorn',
			'URL'		: strFolderPath + 'sign12.gif' },
		{	'Sign'	: '', //default
			'URL'		: strFolderPath + 'sign00.gif' }
	],
	outputSignData : function(id) {
		id -=1;
		alert('sign: ' + this.signs[id].Sign + ', \n and id:' + this.signs[id].ID + ', \n and url: ' + this.signs[id].URL);
	}
}


//Gets the zid, given the selected month and day.
function getZodiacSignIDByBirthDay(intBMonth, intBDay) {

	switch (intBMonth) {
		case 1:									//January
			if (1 <= intBDay && intBDay <= 20)		return 12;	//Capricorn 
			if (21 <= intBDay && intBDay <= 31)		return 1;	//Aquarius
			break;
		case 2:									//February
			if (1 <= intBDay && intBDay <= 19)		return 1;	//Aquarius 
			if (20 <= intBDay && intBDay <= 29)		return 2;	//Pieces
			break;
		case 3:									//March
			if (1 <= intBDay && intBDay <= 20)		return 2;	//Pieces 
			if (21 <= intBDay && intBDay <= 31)		return 3;	//Aries
			break;
		case 4:									//April
			if (1 <= intBDay && intBDay <= 20)		return 3;	//Aries 
			if (21 <= intBDay && intBDay <= 30)		return 4;	//Taurus
			break;		
		case 5:									//May
			if (1 <= intBDay && intBDay <= 21)		return 4;	//Taurus 
			if (22 <= intBDay && intBDay <= 30)		return 5;	//Gemini
			break;	
		case 6:									//June
			if (1 <= intBDay && intBDay <= 21)		return 5;	//Gemini 
			if (22 <= intBDay && intBDay <= 30)		return 6;	//Cancer
			break;		
		case 7:									//July
			if (1 <= intBDay && intBDay <= 23)		return 6;	//Cancer 
			if (24 <= intBDay && intBDay <= 31)		return 7;	//Leo
			break;		
		case 8:									//August
			if (1 <= intBDay && intBDay <= 23)		return 7;	//Leo 
			if (24 <= intBDay && intBDay <= 31)		return 8;	//Virgo
			break;		
		case 9:									//September
			if (1 <= intBDay && intBDay <= 23)		return 8;	//Virgo 
			if (24 <= intBDay && intBDay <= 30)		return 9;	//Libra
			break;		
		case 10:									//October
			if (1 <= intBDay && intBDay <= 23)		return 9;	//Libra 
			if (24 <= intBDay && intBDay <= 31)		return 10;	//Scorpio
			break;		
		case 11:									//November
			if (1 <= intBDay && intBDay <= 22)		return 10;	//Scorpio 
			if (23 <= intBDay && intBDay <= 30)		return 11;	//Sagittarius
			break;		
		case 12:									//December
			if (1 <= intBDay && intBDay <= 21)		return 11;	//Sagittarius 
			if (22 <= intBDay && intBDay <= 31)		return 12;	//Capricorn
			break;
	}
}

//Gets the zodiac id so we can query the signdata object.
function getZidByDate() {
	//get the selected indices of the day and month ddls now.
	var intMonthIndex = 0, intDayIndex = 0, zid = 0;
	var arrSelects = document.getElementsByTagName('select');
	
	intMonthIndex = getMonthSelectedIndex();
	intDayIndex = getDaySelectedIndex();
	
	//see if we have both a month and a day selected...
	if (intMonthIndex > 0 && intDayIndex > 0) {
		//alert('intMonthIndex: ' + intMonthIndex + ', and intDayIndex: ' + intDayIndex);
		zid = getZodiacSignIDByBirthDay(intMonthIndex, intDayIndex);
	}
	else {
		zid = 0;
	}
	//alert(zid);
	return zid;
}

//Gets a reference to the zodiac HTML element 
function getZodiacSelect(strDefaultOptionValue) {
	var arrSelects = document.getElementsByTagName('select');
	for (var i = 0; i < arrSelects.length; i++) {
		var select = arrSelects[i];
		for (var j = 0; j < select.childNodes.length; j++) {
			var val = select.childNodes[j].value;
			if (val == strDefaultOptionValue) {	
				//We found our select. Return it.
				return select;
			}
		}
	}
}

//Checks validity of user's birthdate 
//(including Feb 29 on leap year) on
//the myProfile page.
function checkDOB() {

	var bValid = true;
	var errorpar = document.getElementById('parInvalidDOB');
	var msg = 'Please select a valid birthdate.';
	var mo = document.getElementById('ctl00_Column4Main_ddlDobMo');
	var day = document.getElementById('ctl00_Column4Main_ddlDobDay');
	var yr = document.getElementById('ctl00_Column4Main_ddlDobYr');
	if (mo.selectedIndex > 0 && day.selectedIndex > 0 && yr.selectedIndex > 0) {
		//Extract date from selects.
		var d = mo.selectedIndex + '/' + day.value + '/' + yr.value;
		if (isDate(d)) {
			//Valid DOB.
			toggleDOBError(errorpar, msg, false);
			bValid = true;
		}
		else {
			//Invalid DOB.
			toggleDOBError(errorpar, msg, true);
			bValid = false;
		}
	}
	else {
		//Invalid DOB. User must complete M, D, Y.
		toggleDOBError(errorpar, msg, true);
		bValid = false;
	}
	//alert('checkDOB: bValid: ' + bValid);
	return bValid;
}

//Switches the DOB error on or off.
function toggleDOBError(elem, msg, bOn) {
	//Notify span (could be displaying a msg already).
	var notify = document.getElementById('ctl00_Column4Main_lblStatus');
	if (bOn) {
		//Switch on error.
		notify.innerHTML = ''; //Hide notify msg.
		elem.innerHTML = msg;
		elem.style.color = '#ff0000';
		elem.style.display = 'block';
	}
	else {
		//Switch off error.
		elem.innerHTML = '';
		elem.style.color = '#ff0000';
		elem.style.display = 'none';
	}
}

function getMonthSelectedIndex() {
	return getZodiacSelect('DOB_MONTH').selectedIndex;
}

function getDaySelectedIndex() {
	return getZodiacSelect('DOB_DAY').selectedIndex;
}

function AddItem(Text,Value) {
	// Create an Option object                
	var opt = document.createElement("option");

	// Add an Option object to Drop Down/List Box
	document.getElementById("DropDownList").options.add(opt);        
  
	// Assign text and value to Option object
	opt.text = Text;
	opt.value = Value;
}

//If a month is selected that renders the day invalid
//(such as going from Jan 31 to Feb 31), adjust the day
//to the next nearest valid value.
function adjustDayValue() {
	var m = getMonthSelectedIndex();
	var d = getDaySelectedIndex();
	var selectday = getZodiacSelect('DOB_DAY');
	adjustDaySelectedValue(m, d, selectday);
}


//Used by myaccount and myprofile pages to adjust day and month
//dropdown values.
function adjustDaySelectedValue(m, d, selectday) {
	var maxday;
	switch (m) {
		case 2: //February
			maxday = 29;
			if (d > maxday) {
				selectday.selectedIndex = maxday;
				return;
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11: //30 day months
			maxday = 30;
			if (d > maxday) {
				selectday.selectedIndex = maxday;
				return;
			}
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:	//31 day months
			maxday = 31;
			if (d > maxday) {
				selectday.selectedIndex = maxday;
				return;
			}
			break;
	}
}

//Adjusts the days to the correct number,
//depending on which month is selected.
function switchDaysInMonth() {
	var m = getMonthSelectedIndex();
	var d = getDaySelectedIndex();
	if (m <= 0 || d <= 0) { return; } //We need a valid day and month.
	var select = getZodiacSelect('DOB_DAY');
	var lastday = select.options.length - 1;
	
	//Do the switch
	doDaysInMonthSwitch(m, select, lastday);
}

function doDaysInMonthSwitch(m, select, lastday) {
	switch (m) {
		case 2: //February
			if (lastday == '31') {
				select.remove(lastday); 
				select.remove(lastday-1); 
				return;
			}
			if (lastday == '30') {
				select.remove(lastday); 
				return;
			}
			if (lastday == '29') {
				return;//correct
			}
			break;
		case 4:
		case 6:
		case 9:
		case 11: //30 day months
			if (lastday == '31') {
				select.remove(lastday); 
				return;
			}
			if (lastday == '30') {
				return;//correct
			}
			if	(lastday == '29') {
				addOption(select,'30', '30'); 
				return;
			}
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:	//31 day months
			if (lastday == '31') {
				return;//correct
			}
			if (lastday == '30') {
				addOption(select,'31', '31'); 
				return;
			}
			if (lastday == '29') {
				addOption(select,'30', '30'); 
				addOption(select,'31', '31'); 
				return;
			}
			break;
	}
}

//Changes the zodiac sign depending on the day and month.
function switchZodiac() {
	var m = getMonthSelectedIndex();
	var d = getDaySelectedIndex();
	if (m <= 0 || d <= 0) { return; } //We need a valid day and month.
	var zid = getZodiacSignIDByBirthDay(m, d);
	//Decrement zip by 1 before we set sign properties.
	zid -= 1;
	var zsrc = signdata.signs[zid].URL;
	var ztext = signdata.signs[zid].Sign;
	document.getElementById('imgZodiacSignClient').src = zsrc;
	document.getElementById('txtZodiacSign').innerHTML = ztext;
}



function isMyCornerProfilePage() {
	var url = window.location.href.toLowerCase();
	if (url.indexOf('/users/myprofile.aspx') > -1) { return true; }
	else { return false; }
}

//Birthdate must be earlier than diagnosis date.
//If not, custom validation will run and the sign defaults back to sign00.gif.
//So we need to re-set the sign to the correct img path.
function checkForCustomDateErrors() {
	var h1 = document.getElementById('DateInvalidDOB');
	var h2 = document.getElementById('DateInvalidDD');
	var h3 = document.getElementById('DateInvalidITD');
	var bFlagged = false;
	if (!bFlagged) { if (h1) { bFlagged = isDateFlagged(h1); } }
	if (!bFlagged) { if (h2) { bFlagged = isDateFlagged(h2); } }
	if (!bFlagged) { if (h3) { bFlagged = isDateFlagged(h3); } }
	if (bFlagged) {
		switchZodiac();
	}
}

function isDateFlagged(objH) {
	return objH.value == '1';
}

function checkMyProfileData() {
	var bValidDOB = checkDOB();
	var bValidCity = checkProfileCity();
	//alert('returning: ' + (bValidDOB && bValidCity));
	if (!(bValidDOB && bValidCity)) {
		//If the profile form doesn't validate because of dob or city
		//scroll to the top of the parent div, so user knows what he needs
		//to fix.
		var topright = document.getElementById('mp-top-right');
		if (topright) { ScrollToElement(topright); }
	}
	return bValidDOB && bValidCity;
}

//Used to limit the number of personality checkboxes to 10.
function countPChecks(elem) {

	//Hide error div.
	var errordiv = document.getElementById('parExceededPersonalityCount');
	errordiv.style.display = 'none';
	
	//Set some values.
	var c = 0;	//check count
	var n = 10;	//check limit
	var mpp = document.getElementById('mp-personality');
	var inputs = mpp.getElementsByTagName('input');
	
	//Increment count for previously checked checkboxes.
	for (var i in inputs) {
		if (inputs[i].type == 'checkbox') {
			if (inputs[i].id.indexOf('PersonalityTraitsProfile') > -1) {
				var chk = inputs[i];
				if (chk.checked) { c += 1; }
			}
		}
	}
	
	//Eval count and flag if necessary.
	if (c > n) { 
		elem.checked = false;	//uncheck the last element.
		errordiv.innerHTML = 'Please select only up to ' + n + ' traits.';
		errordiv.style.color = '#ff0000';
		errordiv.style.display = 'block';
	}
}

//validation.js must also be included for the following function to run.
function checkProfileCity() {
	var bValid = true;
	var msg = 'Please specify valid value.';
	var errpar = document.getElementById('parCityInvalid');
	var container = document.getElementById('EditCityState');
	var val = document.getElementById('ctl00_Column4Main_txtCity').value;
	var bValidCity = isValidCity(val);
	if (!bValidCity) {
		//Set error
		container.style.display = 'block';
		errpar.style.display = 'block';
		errpar.innerHTML = msg;
		//Set flag
		bValid = false;
	}
	//alert('checkProfileCity: bValid: ' + bValid);
	return bValid;
}


//////////////////////////////////////////////
// Dom Ready Code
//////////////////////////////////////////////

$(document).ready(function(){
	var bProfilePage = isMyCornerProfilePage();
	if (bProfilePage) {
		checkForCustomDateErrors();
		//Show the disabled grid paging buttons in the friends-only profile.
		//Note: these buttons should never page. They are only a preview.
		//They are initially hidden so they don't show up in the friend profile.
		$('div#FriendProfileBottom div#FriendFriends div.disabledPagingButtons').css({ display:'block', visibility:'visible' });
	}	
});