﻿// Get X,Y Coords of Objects

function findXPos(obj) {
	var curleft = 0;

	if (obj.offsetParent) {
	do {
			curleft += obj.offsetLeft;

		} while (obj = obj.offsetParent);
		
	}
	return curleft;
}

function findYPos(obj) {
	var curtop = 0;

	if (obj.offsetParent) {
	do {
			curtop += obj.offsetTop;

		} while (obj = obj.offsetParent);
		
	}
	return curtop;
}


//Cookies
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function toggleClass(element, class1, class2) {
	if($(element).className == class1)
	    $(element).className = class2;
	else
	    $(element).className = class1;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

function convertIllegalChars(myString) {
    return myString.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;').replace(/\'/g, "&#39;").replace(/\"/g, "&#34;");
}

function getTime () {
    var now = new Date();
    var hours = now.getHours();
    var minutes = now.getMinutes();
    var seconds = now.getSeconds()
    var timeValue = "" + ((hours >12) ? hours -12 :hours)
    if (timeValue == "0") timeValue = 12;
    timeValue += ((minutes < 10) ? ":0" : ":") + minutes
    //timeValue += ((seconds < 10) ? ":0" : ":") + seconds
    timeValue += (hours >= 12) ? " PM" : " AM"
    return timeValue;
}

function getDate() {
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
    year+=1900
    var day=mydate.getDay()
    var month=mydate.getMonth()
    var daym=mydate.getDate()
    if (daym<10)
    daym="0"+daym
    var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
    var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")
    //dayarray[day]+", "+
    return montharray[month]+" "+daym+", "+year;
}


 function CenterOnScreen(obj, xWidth, yHeight) 
 {  
    var scrolledX, scrolledY;
    
    if( self.pageYOffset ) { 
        scrolledX = self.pageXOffset; 
        scrolledY = self.pageYOffset; 
    } else if( document.documentElement && document.documentElement.scrollTop ) { 
        scrolledX = document.documentElement.scrollLeft; 
        scrolledY = document.documentElement.scrollTop; 
    } else if( document.body ) { 
        scrolledX = document.body.scrollLeft; 
        scrolledY = document.body.scrollTop; 
    } 

    // Next, determine the coordinates of the center of browser's window 
    var centerX, centerY; 
    
    if( self.innerHeight ) { 
        centerX = self.innerWidth; 
        centerY = self.innerHeight; 
    } else if( document.documentElement && document.documentElement.clientHeight ) { 
        centerX = document.documentElement.clientWidth; 
        centerY = document.documentElement.clientHeight; 
    } else if( document.body ) { 
        centerX = document.body.clientWidth; 
        centerY = document.body.clientHeight; 
    } 

    // Xwidth is the width of the div, Yheight is the height of the 
    // div passed as arguments to the function: 
    var leftOffset = scrolledX + (centerX - xWidth) / 2; 
    var topOffset = scrolledY + (centerY - yHeight) / 2; 
    
    // The initial width and height of the div can be set in the 
    // style sheet with display:none; divid is passed as an argument to // the function 
    var o = document.getElementById(obj); 
    var r = o.style; 
    r.position='absolute'; 
    r.top = topOffset + 'px'; 
    r.left = leftOffset + 'px'; 
    //r.display = "block"; 


 }


// Ajax Saving Functions
function saveValue(textbox)
{
    if ((event.which ? event.which : event.keyCode) == 13)
    {
        $(textbox).blur();
    }
}

function toggleValue(type, textbox, label, hidValueType, hidValue, editbutton)
{
    if($(label).style.display == 'none')
        $(label).style.display='block';
    else
        $(label).style.display='none';

    if($(textbox).style.display == 'none')
    {
        $(textbox).style.display = 'block';
        $(editbutton).style.display = 'none';
        $(textbox).focus();
    }
    else
    {
        $(textbox).style.display = 'none';
        $(editbutton).style.display = 'block';
        
        //Don't save if no change made
        if(label.substring(0,11) == "description" || label.substring(0,8) == "keywords" || label.substring(0,4) == "name")
        {
            if(trim($(label).innerHTML) == trim($(textbox).value))
            {
                return;
            }
        }
        _saveValue(type, hidValueType, hidValue, $(textbox).value);
    }
    
    if(label == "about_me")
    {
        $("aboutme_text_desc").style.display = $(textbox).style.display;
        $("aboutme_save").style.display = $(textbox).style.display;
    }
}

function _saveValue(type, hidValueType, hidValue, value)
{
    $(hidValueType).value = type;
    $(hidValue).value = value;
    __doPostBack(hidValueType,'');
}

function ShowObject(obj)
{
    if($(obj) != null)
        $(obj).style.display = 'block';
} 
function HideObject(obj)
{
    if($(obj) != null)
        $(obj).style.display = 'none';
} 


function ToggleObject(obj)
{
    if($(obj) != null)
    {
        if($(obj).style.display == 'none')
            $(obj).style.display = 'block';
        else
            $(obj).style.display = 'none';
    }
} 

function ToggleAboutMe()
{
    if($('aboutme_tab') != null)
    {
        if($('aboutme_tab').style.display == 'none')
        {
            $('aboutme_collapsed').style.display = 'block';
            $('aboutme_expanded').style.display = 'none';
        }
        else
        {
            $('aboutme_collapsed').style.display = 'none';
            $('aboutme_expanded').style.display = 'block';
            
            location.href = "#aboutme";
        }
    }
} 


function ShowLogin()
{
    document.location.href = "/Registration.aspx";
}


var colors = new Array("#000066","#003399","#0000FF","#0066CC","#3399FF","#99CCFF","#990033","#CC0066","#FF0066","#FF3333","#FF0000");

// Rating Functions
function GetRatingFromMousePos(e, ratingTextBox)
{
    var xPos = getMouseX(e) - findXPos($('rating_rail'));
    var rating = ((xPos-12) / 45).toFixed(2);
    
    $('rating_handle_current').style.left = xPos + 'px';
    $('rating_bar').style.width = xPos > 520 ? 520 : xPos + 'px';
    $('rating_bar').style.background = colors[parseInt(rating) > 10 ? 10 : parseInt(rating)];
    
    //45 Pixels per number
    if(rating > 10)
        rating = 10.00;
    if(rating < 0)
        rating = 0.00.toFixed(2);
        
    //Set Graphical Border for Rating
    SetRatingBorder(rating);
        
    $(ratingTextBox).value = rating;
}

// Rating Functions
function SetPreviousRating(HidRatingValue)
{
    if($('rating_bar') != null)
    {
        var previousRating = parseFloat($(HidRatingValue).value);

        $('rating_bar').style.width = previousRating <= 0 ? 0 : (previousRating == 10 ? 520 :((previousRating * 45) + 12)) + 'px';
        
        if(previousRating >= 0)
            $('rating_bar').style.background = colors[parseInt(previousRating) > 10 ? 10 : parseInt(previousRating)];
        
        //Set Graphical Border for Rating
        SetRatingBorder(previousRating);
    }
}


// Main function to retrieve mouse x-y pos.s
function getMouseX(e) {
    // Detect if the browser is IE or not.
    // If it is not IE, we assume that the browser is NS.
  var IE = document.all?true:false

    // If NS -- that is, !IE -- then set up for mouse capture
  if (!IE) document.captureEvents(Event.MOUSEMOVE)

  var tempX = 0;
  
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  
  return tempX;
}

//Rating Functions

function SaveRating(id, HidRatingSave, HidRatingValue, TxtRatingValue)
{
    if(parseInt(id) > 0)
    {
        $(HidRatingValue).value = $(TxtRatingValue).value;
        $(HidRatingSave).value='1';
        __doPostBack(HidRatingSave,'');
        
        var ratingX = (parseFloat($(HidRatingValue).value) * 45) + 12;
        
        //Set Graphical Border for Rating
        SetRatingBorder(parseFloat($(HidRatingValue).value));
    
        //45 pixels per rating box
        //+12px for 0 selection offset
        if (parseFloat($(HidRatingValue).value) == 0)
            $('rating_handle_previous').style.left = '0px';
        else if(parseFloat($(HidRatingValue).value) == 10)
            $('rating_handle_previous').style.left = '520px';
        else
            $('rating_handle_previous').style.left = ratingX + 'px';
    }
    else
    {
        ShowLogin();
    }
}

function SetRatingBorder(ratingValue)
{

    var pic = new Image();

    if(ratingValue < 6)
        pic.src = '/_images/spacer.gif';
    else if(ratingValue >= 6 && ratingValue < 8)
        pic.src = '/_images/_rating/smoke.gif';
    else if(ratingValue >= 8 && ratingValue < 9.5)
        pic.src = '/_images/_rating/flame.gif';
    else
        pic.src = '/_images/_rating/flame_big.gif';



    $('rating_border').src = pic.src;
}

function LoadImage(mID, uString, src)
{
    $('profile_photo').innerHTML = '<a href="/ViewPhoto.aspx?id=' + mID + '&phid=' + uString + '"><img src="' + src + '" style="border: solid 2px #484848;" border=0></a>';
}

/*
function CharacterCounter(counter, field, lengthNeeded)
{
    if(lengthNeeded - $(field).value.length < 0)
        $(counter).value = 0;
    else
        $(counter).value = lengthNeeded - $(field).value.length;
}*/

function CharacterCounter(counter, field)
{
        $(counter).value = $(field).value.length;
}

function ValidateLengthAndCheck(t, i, l)
{
    if($(t).value.length >= l)
        $(i).style.display = 'block';
    else
        $(i).style.display = 'none';
}

function ValidateRegistrationBirthDate(d, m, y, i)
{
    if($(d).value != '' && $(m).value != '' && $(y).value != '')
        $(i).style.display = 'block';
    else
        $(i).style.display = 'none';
}