if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}
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 prepExternalLinks() {
   $('a.external_link').attr('target', '_blank');
}

function inputBoxes() {
   $("div.signupFieldWrapper input").addClass('text_input_rounded');
	$.each($("input[type='text']"), function() {
	   if($(this).attr('class') == "text_input_rounded" || $(this).attr('class') == "text_input_rounded_interest" || $(this).attr('class') == "text_input_rounded_zipcode") {
		   $(this).attr('value', humanize($(this).attr('id')))
		}
	});
	$("input[type='text']").focus(function() {
		if($(this).attr('value') == humanize($(this).attr('id'))) {
			$(this).attr('value', '');
		}
	});
	$("input[type='text']").blur(function() {
		if($(this).attr('value') == '' && ($(this).attr('class') == "text_input_rounded" || $(this).attr('class') == "text_input_rounded_interest" || $(this).attr('class') == "text_input_rounded_zipcode")) {
			$(this).attr('value', humanize($(this).attr('id')));
		}
	});
	$.each($("input[type='password']"), function() {
		$(this).attr('value', humanize($(this).attr('id')))
	});
	$("input[type='password']").focus(function() {
		if($(this).attr('value') == humanize($(this).attr('id'))) {
			$(this).attr('value', '');
		}
	});
	$("input[type='password']").blur(function() {
		if($(this).attr('value') == '') {
			$(this).attr('value', humanize($(this).attr('id')));
		}
	});
}

function humanize(str) {
   var pieces = str.split("_");
   if(pieces.length > 1) {
      var firstchar = pieces[1][0];
      var rest = pieces[1].substr(1, pieces[1].length);
      var final_str = firstchar.toUpperCase()+rest;
      if(final_str == "Captcha") {
         final_str = "Please enter the code in the image.";
      }
      return final_str;
   }
   return str;
}

function submit_form(form) {
   form.submit();
   return false;
}

function wizard() {
   $('input.text_input_rounded_interest').keyup(function(e) {
      if(e.keyCode == 13) {
         if($(this).attr('value') != "") {
            $('select#drop2').append('<option value="'+$(this).attr('value')+'">'+$(this).attr('value')+'</option>');
            $('div#testform').append('<input type="hidden" value="'+$(this).attr('value')+'" name="'+$(this).attr('value')+'">')
            $(this).attr('value', '');
         }
      }
      var topic = $(this).val();

      // normalize by removing leading whitespace and converting 
      // other whitespace to single spaces
      topic = topic.replace(/^\s+/, '');
      topic = topic.replace(/\s+/g, ' ');

      // does the topic begin and end with a double-quote?
      var quoted = (topic.length > 1 && topic[0] == '"' && topic[topic.length - 1] == '"');
      // remove all double quotes from the string to weed out ugly cases
      topic = topic.replace(/\"/g, '');
      if (quoted)
         // remove leading or following whitespace from the string now that quotes are gone
         topic = topic.replace(/^\s+|\s+$/g, '');
         
      var chosen = getChosenTopics();
      // put double quotes around each tag and separate them with commas
      $(chosen).each(function(i, t) {
         chosen[i] = '"' + t + '"';
      });
      chosen = chosen.join(' ');
      
      $('select#drop1').load(
         '/topics/autocompletewizard/',
         { 
            topic: topic,
            chosen: chosen
         },
         function(response, status, request) {
            if(status == 'success') {
            }
         });
            
   });
}

function getChosenTopics() {
   topics = []
   options_from_one = $('select#drop1').attr('options');
   $.each(options_from_one, function() {
      topics.push($(this).attr('value'));
   })
   return topics;
}

function transfer_right() {
   var options_from_one = $('select#drop1').attr('options');
   var options_from_two = $('select#drop2').attr('options');
   $.each(options_from_one, function() {
      if($(this).attr('selected') && ($(this).attr('value') != "")) {
         $('select#drop2').append('<option id="'+$(this).attr('value')+'" value="'+$(this).attr('value')+'">'+$(this).attr('value')+'</option>');
         $('div#testform').append('<input type="hidden" value="'+$(this).attr('value')+'" name="'+$(this).attr('value')+'">')
         $("select#drop1 option[value='"+$(this).attr('value')+"']").remove();
      }
   });
   return false;
}
function transfer_left() {
   var options_from_two = $('select#drop2').attr('options');
   $.each(options_from_two, function() {
      if($(this).attr('selected')) {
         $('select#drop1').append('<option value="'+$(this).attr('value')+'">'+$(this).attr('value')+'</option>');
         $('input[name="'+$(this).attr('value')+'"]').remove();
         $('select#drop2 option[value="'+$(this).attr('value')+'"]').remove();
      }
   });
   return false;
}

function notifications() {
   $('.notify').mouseover(function(e) {
      $(this).prev().fadeIn('fast');
   });
   $('.notify').mouseout(function(e) {
      $(this).prev().fadeOut('fast');
   });
   $('input#id_password').keyup(function(e) {
      if(validate_password($(this).val())) {
         $(this).next().next().attr('src', '/elements/img/green-check.jpg');
      } else {
         $(this).next().next().attr('src', '/elements/img/alert-sign.jpg');
      }
   });
   $('input#id_username').keyup(function(e) {
      if(validate_username($(this).val())) {
         $(this).next().next().attr('src', '/elements/img/green-check.jpg');
      } else {
         $(this).next().next().attr('src', '/elements/img/alert-sign.jpg');
      }
   });
}

function validate_password(val) {
   var re = /^(([a-zA-Z]+)([^a-zA-Z]+).*)|([^a-zA-z]+)([a-zA-Z]+).*$/;
   return re.test(val) && val.length >= 6;
}

function validate_username(val) {
   var re = /^[a-zA-Z]([a-zA-Z0-9.])*[a-zA-Z0-9]?$/;
   return re.test(val) && val.length <= 30 && val.length > 3;
}

$(document).ready(function() {
   prepExternalLinks();
	inputBoxes();
	wizard();
	notifications();
	var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
	var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

	if (jQuery.browser.msie && (ie55 || ie6)) {
	   $(document).pngFix();
	}
});
