$jq(function(){
	Feedback.init();
	$jq("#topIssues option:eq(0)").attr("selected", "selected");
});

var Feedback = {
	issues:[
        {
          issue:[]
        },
		{
		issue:[
			{type: "technical", desc:["Video Freezes/Pauses", "Choppy/Skipping Video", "Poor Quality", "Audio/Video not synched", "Video but no Audio", "Audio but no Video", "Video not loading"]},
			{type: "non technical", desc:["iPad Support", "Other issues (please specify)"]}
		]},
		{
		issue:[
			{type: "technical", desc:["Reports that I'm outside the US when I'm not"]},
			{type: "non technical", desc:["I'm outside the US and can't watch content"]}
		]},
		{
		issue:[
			{type: "technical", desc:["Ad stuck in video player", "Ad crashes or Locks up"]},
			{type: "non technical", desc:["Ad repeats too much", "Recommendations for Advertisers", "Other issues related to Ads"]}
		]},
		{
		issue:[
			{type: "non technical", desc:["Questions on this show", "Recommendations for show", "Comments on show"]}
		]},
		{
		issue:[
			{type: "non technical", desc:["Questions on this show", "Recommendation for video player features/ Enhancements", "Recommendations for show"]}
		]}
	],

	init: function(){
		$jq("#topIssues").change(function(){
			var subIssuesSelect = $jq("<select id=\"subIssues\" name=\"subIssues\"></select>");
			var topIssues = Feedback.issues[$jq(this).attr("selectedIndex") - 1];

			for(var topIssue in topIssues){
				var subIssues = topIssues[topIssue];
				for(var subIssue in subIssues){
					var issueDesc = subIssues[subIssue].desc;
					var issueType = subIssues[subIssue].type;
					for(var desc in issueDesc){
						$jq(subIssuesSelect).append("<option value=\"" + issueType + " : " + issueDesc[desc] + "\">" + issueDesc[desc]);
					}
				}
			}

			if($jq("#subIssues").length > 0){
				$jq("#subIssues").animate({height: '0px'}, 100, function(){
					$jq(this).remove();
					if($jq(subIssuesSelect).find("option").length > 0){
						$jq("#topIssues").after($jq(subIssuesSelect));
						$jq("#subIssues").animate({height: '20px'}, 100);
					}
				});
			}else{
				if($jq(subIssuesSelect).find("option").length > 0){
					$jq(this).after($jq(subIssuesSelect));
					$jq("#subIssues").animate({height: '20px'}, 100);
				}
			}
		});
	}
};

//to avoid the overhead of the SDK, I pulled the appropriate validate functions from it
function IsName(str){
    return str.match(/^[a-zA-Z0-9 -]+$/) != null;
}

function IsEmail(str){
    return str.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/) != null;
}

function OptionSelected(index){
    return (index != 0);
}

function IsEmpty(val){
    return (val != '' || val.search(/[a-zA-Z0-9]/) != -1);
}

function FormCheck(form){
    var check = {"name" : IsName, "EMAIL_SENDER" : IsEmail, "topIssues" : OptionSelected, "desc" : IsEmpty};
    var curVal;
    var success = true;

    for(var item in check){
        //grab the value and the corresponding label for the field we are checking
        if(item == 'topIssues')
            curVal = form[item].selectedIndex;
        else
            curVal = form[item].value;
        if(check[item](curVal)){
            document.getElementById('label_' + item).className = '';
        }
        else{
            document.getElementById('label_' + item).className = 'error';
            success = false;
        }
    }

    if(!success){
        document.getElementById('errorMsg').style.display = 'block';
        return false;
    }

    return true;
}

// CSS Browser Selector   v0.2.5
// Documentation:         http://rafael.adm.br/css_browser_selector
// License:               http://creativecommons.org/licenses/by/2.5/
// Author:                Rafael Lima (http://rafael.adm.br)
// Contributors:          http://rafael.adm.br/css_browser_selector#contributors
var css_browser_selector = function() {
	var
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=(!(/opera|webtv/i.test(ua))&&/msie (\d)/.test(ua))?('ie ie'+RegExp.$1):is('gecko/')? 'gecko':is('opera/9')?'opera opera9':/opera (\d)/.test(ua)?'opera opera'+RegExp.$1:is('konqueror')?'konqueror':is('applewebkit/')?'webkit safari':is('mozilla/')?'gecko':'',
		os=(is('x11')||is('linux'))?' linux':is('mac')?' mac':is('win')?' win':'';
	var c=b+os+' js';
	h.className += h.className?' '+c:c;
}();

