/**
* Javascript file containing common methods to validate email, calculate age
**/

/**
 * Open a javascript popup window
 */
function openWin(url, windowName, width, height, resizeable) {
  window.open(url,windowName,'width='+width+',height='+height+',directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,resizable='+resizeable);
 }

/**
* function to validate email
**/
function isValidEmail(emailStr){
	if(!emailStr.match(new RegExp("^([a-zA-Z0-9_]|\\-|\\.)+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}$"))){
	  //alert("Please enter valid email");
	  return false;
	}
	else{
	  return true;
	}
}

/**
* function to calculate age from date of birth
**/
function getAge(DOB) {
  var bDate = DOB.split('/');
  //bDateArr = (''+bDate).split(' ');
  cDate = new Date;
  //cDateArr = (''+cDate).split(" ");
  //age = cDateArr[3] - bDateArr[3];
  if (navigator.appName == "Microsoft Internet Explorer")
  {
    age = cDate.getYear() - bDate[2];
  }
  else
  {
    birthDate = new Date(bDate[2],bDate[0]-1,bDate[1]);
    age = cDate.getYear() - birthDate.getYear();
  }
  return age;
}

/**
* function to check date format
**/
function checkDateFormat(dob) {
  var date_format = '([0-9]{1,2})/([0-9]{1,2})/([0-9]{4})';
	dateRegExp = new RegExp(date_format);
	if(!dateRegExp.test(dob)){
		return false;
	}
	else{
	  return true;
	}
}

/**
* function to hide pop ups
**/
function hide_popup(element_id)
{
  if (element_id == '')
  {
    return null;
  }
  document.getElementById(element_id).style.display = 'none';
}

/**
* function to get cookie values
**/
function getCookie(cookieName)
{
  if (document.cookie.length>0)
    {
    cookieStart=document.cookie.indexOf(cookieName + "=");
    if (cookieStart!=-1)
      {
      cookieStart=cookieStart + cookieName.length+1;
      cookieEnd=document.cookie.indexOf(";",cookieStart);
      if (cookieEnd==-1) cookieEnd=document.cookie.length;
      return unescape(document.cookie.substring(cookieStart,cookieEnd));
      }
    }
  return "";
}

/**
* function to get highlight the stars
**/
function highlight_stars(star_count, suffix)
{
  for (var i=1; i <= 5; i++)
  {
    if (i <= star_count)
    {
      document.getElementById("star_"+i+suffix).src = '/images/star_gold.png';
    }
    else
    {
      document.getElementById("star_"+i+suffix).src = '/images/star_gray_big.png';
    }
  }
}

/**
* function to get freeze the stars
**/
function freeze_stars(star_count, suffix)
{
  for (var i=1; i <= 5; i++)
  {
    if (i <= star_count)
    {
      document.getElementById("star_"+i+suffix).src = '/images/star_gold.png';
    }
    else
    {
      document.getElementById("star_"+i+suffix).src = '/images/star_gray_big.png';
    }
    document.getElementById("star_"+i+suffix).onmouseout = function ()
                                                            {
                                                              return false;
                                                            }
    document.getElementById("star_"+i+suffix).onmouseover = function ()
                                                            {
                                                              return false;
                                                            }
  }
}

/**
* function to dehighlight the stars
**/
function dehighlight_stars(suffix)
{
  for (var i=1; i <= 5; i++)
  {
      document.getElementById("star_"+i+suffix).src = '/images/star_gray_big.png';
  }
}

function display_star_rating_count(suffix)
{
  for (var the_key in ratings)
  {
    var the_value = ratings[the_key];
    if (the_key.indexOf("_avgRating") != -1)
    {
      var key_splitted = the_key.split("_");
      var ep_id = key_splitted[0];
      for (var i=1; i <= 5; i++)
      {
        if (i <= the_value)
        {
          document.getElementById("star_"+i+"_display_"+suffix).src = '/images/star_white.png';
        }
        else
        {
          document.getElementById("star_"+i+"_display_"+suffix).src = '/images/star_gray.png';
        }
      }
    }
    else
    {
      document.getElementById("ratings_count_"+suffix).innerHTML = the_value;
    }
  }
}

/**
* function to check the length of textarea
**/
function check_length(field, max_limit)
{
  var oTextarea;
  oTextarea = document.getElementById(field);
  if (oTextarea.value.length > max_limit)
  {
   if(confirm("Your comment exceeds the max length of "+max_limit+" characters. \nPress Ok to allow the system to trim your comments or Cancel to edit"))
   {
    oTextarea.value = oTextarea.value.substring(0,max_limit);
   }
   else
   {
     oTextarea.focus();
     return "error";
   }
  }
  return oTextarea.value;
}

function check_form(event){
  var block_registration = getCookie('COOKIE_BLOCK_REG');
  if(block_registration == "true")
  {
    document.getElementById("error_signin").innerHTML = "Sorry. We are unable to process your registration at this time.";
    document.getElementById("error_signin").style.display = '';
    return false;
  }
  else
  {
    document.getElementById("error_signin").innerHTML = "";
    document.getElementById("error_signin").style.display = 'none';
  }
  var validated = 1;
  var err_msg = "";

  if (document.SignUp.Username.value == '' || document.SignUp.Username.value == 'User Name'){
    validated = 0;
    //err_msg += "* Required: User Name\n";
    document.getElementById("error_Username").style.display = '';
    document.getElementById("Username").style.border = '1px solid red';
  }
  else {
    document.getElementById("error_Username").style.display = 'none';
    document.getElementById("Username").style.border = '';
  }
  if (document.SignUp.Password.value == ''){
    validated = 0;
    //err_msg += "* Required: Password\n";
    document.getElementById("error_textPassword").style.display = '';
    document.getElementById("textPassword").style.border = '1px solid red';
    document.getElementById("error_typePassword").style.display = '';
    document.getElementById("typePassword").style.border = '1px solid red';
  }
  else {
    document.getElementById("error_textPassword").style.display = 'none';
    document.getElementById("textPassword").style.border = '';
    document.getElementById("error_typePassword").style.display = 'none';
    document.getElementById("typePassword").style.border = '';
  }
  if (document.SignUp.Email.value == '' || document.SignUp.Email.value == 'Email Address'){
    validated = 0;
    //err_msg += "* Required: Email Address\n";
    document.getElementById("error_Email").style.display = '';
    document.getElementById("Email").style.border = '1px solid red';
  }
  else {
    document.getElementById("error_Email").style.display = 'none';
    document.getElementById("Email").style.border = '';
  }
  if (document.SignUp.confirm_email.value == '' || document.SignUp.confirm_email.value == 'Confirm Email'){
    validated = 0;
    //err_msg += "* Required: Confirm Email Address\n";
    document.getElementById("error_confirm_email").style.display = '';
    document.getElementById("confirm_email").style.border = '1px solid red';
  }
  else {
    document.getElementById("error_confirm_email").style.display = 'none';
    document.getElementById("confirm_email").style.border = '';
  }
  if (document.SignUp.dobMonth.value == '0' || document.SignUp.dobDay.value == '0' || document.SignUp.dobYear.value == '0'){
    validated = 0;
    //err_msg += "! Please select all date fields for DOB\n";
    document.getElementById("error_dob").innerHTML = "Please select all date fields for DOB";
    document.getElementById("error_dob").style.display = '';
    document.getElementById("dobMonth").style.border = '1px solid red';
    document.getElementById("dobDay").style.border = '1px solid red';
    document.getElementById("dobYear").style.border = '1px solid red';
  }
  else {
    document.getElementById("error_dob").style.display = 'none';
    document.getElementById("dobMonth").style.border = '';
    document.getElementById("dobDay").style.border = '';
    document.getElementById("dobYear").style.border = '';
  }
  if (document.SignUp.Email.value != '' && document.SignUp.Email.value != 'Email Address'){
	  if (!isValidEmail(document.SignUp.Email.value)){
		validated = 0;
		//err_msg += "! Please enter valid email\n";
    document.getElementById("error_Email").innerHTML = "Please enter valid email";
    document.getElementById("error_Email").style.display = '';
    document.getElementById("Email").style.border = '1px solid red';
		//return false;
	  }
	  else {
  	  document.getElementById("error_Email").style.display = 'none';
    document.getElementById("Email").style.border = '';
	  }
  }
  if (document.SignUp.Email.value != '' && document.SignUp.confirm_email.value != '' && document.SignUp.Email.value != 'Email Address' && document.SignUp.confirm_email.value != 'Confirm Email'){
	if (document.SignUp.Email.value != document.SignUp.confirm_email.value)
	{
		validated = 0;
		//err_msg += "! E-mail and Confirm E-mail Address does not match\n";
    document.getElementById("error_confirm_email").innerHTML = "E-mail and Confirm E-mail Address does not match";
    document.getElementById("error_confirm_email").style.display = '';
    document.getElementById("confirm_email").style.border = '1px solid red';
	}
	else
	{
  	document.getElementById("error_confirm_email").style.display = 'none';
    document.getElementById("confirm_email").style.border = '';
	}
  }
  if (document.SignUp.dobMonth.value != '0' && document.SignUp.dobDay.value != '0' && document.SignUp.dobYear.value != '0') {
	  document.SignUp.DOB.value = document.SignUp.dobMonth.value + '/' + document.SignUp.dobDay.value + '/' + document.SignUp.dobYear.value;
    /*if (getAge(document.SignUp.DOB.value) < 13){
      validated = 0;
      document.cookie = "COOKIE_BLOCK_REG=true; path=/";
      document.getElementById("error_signin").innerHTML = "Sorry. We are unable to process your registration at this time.";
      document.getElementById("error_signin").style.display = '';
      //document.getElementById("dobMonth").style.border = '1px solid red';
      //document.getElementById("dobDay").style.border = '1px solid red';
      //document.getElementById("dobYear").style.border = '1px solid red';
      //return false;
    }
    else {
      document.getElementById("error_signin").style.display = 'none';
      document.getElementById("error_signin").innerHTML = '';
      //document.getElementById("dobMonth").style.border = '';
      //document.getElementById("dobDay").style.border = '';
      //document.getElementById("dobYear").style.border = '';
    }*/
  }
  if (!validated) {
    //alert(err_msg);
    return false;
  }
  signup('SignUp', '', event);
}

function check_value_onblur(fieldId, fieldVal)
{
  if (document.getElementById(fieldId).value == '')
  {
    document.getElementById(fieldId).value = fieldVal;
  }
}

function check_value_onfocus(fieldId, fieldVal)
{
  if (document.getElementById(fieldId).value == fieldVal)
  {
    document.getElementById(fieldId).value = '';
    document.getElementById(fieldId).focus();
  }
}

function check_password_onfocus()
{
  document.getElementById("textPasswordtr").style.display = 'none';
  document.getElementById("typePasswordtr").style.display = '';
  document.getElementById("typePassword").focus();
}

function check_password_onblur()
{
  if (document.getElementById("typePassword").value == '')
  {
    document.getElementById("textPasswordtr").style.display = '';
    document.getElementById("typePasswordtr").style.display = 'none';
  }
}

function check_password_header_onfocus()
{
  document.getElementById("textPasswordHeader").style.display = 'none';
  document.getElementById("Password_header").style.display = '';
  document.getElementById("Password_header").focus();
}

function check_password_header_onblur()
{
  if (document.getElementById("Password_header").value == '')
  {
    document.getElementById("textPasswordHeader").style.display = '';
    document.getElementById("Password_header").style.display = 'none';
  }
}

function check_password_player_onfocus()
{
  document.getElementById("textPasswordPlayer").style.display = 'none';
  document.getElementById("Password_player").style.display = '';
  document.getElementById("Password_player").focus();
}

function check_password_player_onblur()
{
  if (document.getElementById("Password_player").value == '')
  {
    document.getElementById("textPasswordPlayer").style.display = '';
    document.getElementById("Password_player").style.display = 'none';
  }
}

function check_password_body_onfocus()
{
  document.getElementById("textPasswordBody").style.display = 'none';
  document.getElementById("Password_body").style.display = '';
  document.getElementById("Password_body").focus();
}

function check_password_body_onblur()
{
  if (document.getElementById("Password_body").value == '')
  {
    document.getElementById("textPasswordBody").style.display = '';
    document.getElementById("Password_body").style.display = 'none';
  }
}

function check_form_forgot_password(fieldId, messageDiv){

  if (document.getElementById(fieldId).value == '' || document.getElementById(fieldId).value == 'Email Address')
  {
    document.getElementById(messageDiv).className = "error";
    document.getElementById(messageDiv).innerHTML = "Enter your Email Address or User Name to receive your password";
    Effect.Appear(messageDiv);
    return false;
  }
  forgot_password(fieldId, messageDiv);
}

function open_popup (popup_id, event)
{
  var x;
  var y;
  var coordinate;
  x = Event.pointerX(event);
  y = Event.pointerY(event);
  coordinate = eval("'"+x+","+y+"'");

  new Popup(popup_id,null,{position: coordinate});
  $(popup_id).popup.show();
}

function open_signin_popup (popup_id, event)
{
  document.getElementById("error_"+popup_id).className = "error";
  document.getElementById("error_"+popup_id).innerHTML = "";
  document.getElementById("error_"+popup_id).style.display = "none";
  var x;
  var y;
  var coordinate;
  x = Event.pointerX(event);
  y = Event.pointerY(event);
  x = x-250;
  y = y+10;
  coordinate = eval("'"+x+","+y+"'");
  if (navigator.appName == "Microsoft Internet Explorer")
  {
    document.getElementById(popup_id).style.width = "335px";
  }
  else
  {
    if (navigator.userAgent.indexOf("Safari") != -1)
    {
      document.getElementById(popup_id).style.width = "380px";
    }
    else
    {
      document.getElementById(popup_id).style.width = "320px";
    }
  }
  new Popup(popup_id,null,{position: coordinate});
  $(popup_id).popup.show();
}

function trigger_signin_popup (popup_id, popup_link_id)
{
  document.getElementById("error_"+popup_id).className = "error";
  document.getElementById("error_"+popup_id).innerHTML = "";
  document.getElementById("error_"+popup_id).style.display = "none";
  var x;
  var y;
  var pos = Position.cumulativeOffset(document.getElementById(popup_link_id));
  var coordinate;

  x = pos[0];
  y = pos[1];
  x = x-250;
  y = y+20;
  coordinate = eval("'"+x+","+y+"'");
  if (navigator.appName == "Microsoft Internet Explorer")
  {
    document.getElementById(popup_id).style.width = "335px";
  }
  else
  {
    if (navigator.userAgent.indexOf("Safari") != -1)
    {
      document.getElementById(popup_id).style.width = "380px";
    }
    else
    {
      document.getElementById(popup_id).style.width = "320px";
    }
  }
  new Popup(popup_id,null,{position: coordinate});
  $(popup_id).popup.show();
}

function open_signup_popup (popup_id)
{
	if (typeof ActiveUser == 'object') {
		ActiveUser.doSignUpClick();
	}
	else {
		if(typeof hidePlayer == 'function')
		{
			hidePlayer();
		}
		if (navigator.appName == "Microsoft Internet Explorer")
		{
			new Popup(popup_id,null,{modal:true,position:'center',opacity:0.8})
		}
		else
		{
			new Popup(popup_id,null,{modal:true,position:'center',opacity:0.8})
		}
		$(popup_id).popup.show();
	}
}

function open_report_popup (popup_id, event)
{
  var x;
  var y;
  var coordinate;
  x = Event.pointerX(event);
  y = Event.pointerY(event);
  x = x-370;
  y = y+10;
  coordinate = eval("'"+x+","+y+"'");
  document.getElementById("report_comment_id").value = popup_id;
  document.getElementById("error_ReasonForViolation").className = "error";
  document.getElementById("error_ReasonForViolation").innerHTML = '';
  document.getElementById("error_Reason").innerHTML = '';
  new Popup("report_comment",null,{modal:true,position: coordinate,opacity:0.2});
  $("report_comment").popup.show();
}

function toggle_class(objthis)
{
  var objid = document.getElementById(objthis.id);
  if (objid.className == "defaultText")
  {
    objid.className = "defaultTextBorder";
  }
  else if (objid.className == "defaultTextBorder")
  {
    objid.className = "defaultText";
  }
}
