/**
 * AJAX Processing Script to Generate Options for Drop Downs
 * @author ctyler
 */
function selectDevice(){
	var deviceID = $F('device');
	new Ajax.Updater('brand', '/community/contest_processing.php?id=' + deviceID + '&action=selectDevice', {
		method: 'get'
	});
}
function selectBrand(){
	var deviceID = $F('device');
	var brandID = $F('brand');
	
	new Ajax.Updater('model', '/community/contest_processing.php?device='+deviceID+'&make='+brandID+'&action=selectBrand', {
     method: 'get'
    });
}
function selectBrand_SKU(){
	var deviceID = $F('device');
	var brandID = $F('brand');
	
	new Ajax.Updater('model', '/community/contest_processing.php?device='+deviceID+'&make='+brandID+'&action=selectBrand_SKU', {
     method: 'get'
    });
}

function selectDevice2(){
	var deviceID = $F('device');
	if(deviceID == 'other'){
		return;
	}
	new Ajax.Updater('brand', '/community/contest_processing.php?id=' + deviceID + '&action=selectDevice2', {
		method: 'get'
	});
}
function selectBrand2(){
	var deviceID = $F('device');
	if(deviceID == 'other'){
		return;
	}
	var brandID = $F('brand').replace('&', '%26');
	
	new Ajax.Updater('model', '/community/contest_processing.php?device='+deviceID+'&make='+brandID+'&action=selectBrand2', {
     method: 'get'
    });
}

/**
 * @author dhill
 * 
 * Javascript to control the 'your top ten' page
 */

count = 0;
k = 0;

function addToListFree(){
	
	var error = false;
	var device;
	var brand;
	var model;
	
	if(count == 10){
		alert('You can only add up to 10 apps. Remove one to add another');
		return false;
	}
	
	
	if($('device').value == ''){
		alert('You Must Select a Device');
		error = true;
	}
	else if($('brand').value == ''){
		alert('You Must Select a Brand');
		error = true;
	}
	else if($('model').value == ''){
		alert('You Must Select a Model')
		error = true;
	}
	
	if(error == true){
		return;
	}
	
	if($('device').value == 'other'){
		if($('textDevice').value == ''){
			alert('You Must Select a Device');
			error = true;
		}
		else{
			device = $('textDevice').value;
		}
	}
	else{
		device = $('device').value;
	}
	
	if($('brand').value == 'other'){
		if($('textBrand').value == ''){
			alert('You Must Select a Brand');
			error = true;
		}
		else{
			brand = $('textBrand').value;
		}
	}
	else{
		brand = $('brand').value;
	}
	
	if($('model').value == 'other'){
		if($('textModel').value == ''){
			alert('You Must Select a Model');
			error = true;
		}
		else{
			model = $('textModel').value;
		}
	}
	else{
		model = $('model').value;
	}
	
	if(error == true){
		return;
	}
	
	count++;
	
	if(count != 0){
		$('free_list').show();
		$('freeShowText').hide();
		if($('freeClear')){
			$('freeClear').remove();
		}
	}
	
	
	var list = $('free');
	var hidden = new Element('input', {
		'id' : 'free'+k,
		'type' : 'hidden',
		'value' : device+'_'+brand+'_'+model+k
	});
	var div = new Element('div', {
		'id' : 'free_list'+k
	}).update('<div style="float: left; width: 300px;">'+brand+' '+model+'</div><div style="float: left; padding-bottom: 5px;"><input type="image" src="../images/other/error_form.png" style="font-size: 10px;" onClick="javascript:removeFromListFree('+k+')"></div>');
	$('free_list').appendChild(div);
	var clear = new Element('div',{
		'id' : 'freeClear',
		'style' : 'clear:both'
	});
	$('free_list').appendChild(clear);
	list.appendChild(hidden);
	k++;
	//$('free_app').value = '';
}


function removeFromListFree(id){

	$('free'+id).remove();
	$('free_list'+id).remove();
	count--;
	
	if (count == 0) {
		$('freeShowText').show();
		$('free_list').hide();
	}
	
}

function submitList(){
	var free_val = new Array();
	
	if ($('email').value == '') {
		alert('Please Enter Your Email Address');
		return false;
	}
	else {
		var email = $('email').value;
	}
	
	if(k == 0){
		alert('No Devices selected');
		return false;
	}
	var i = 0;
	while(k >= 0){
		if($('free'+k) != null) {
			free_val[i] = $('free' + k).value;
			i++
		}
		k--;
	}
	var freeVals = free_val.toJSON();
	new Ajax.Request('/community/contest_processing.php',{
		method: 'post',
		parameters: {
			vals:	freeVals,
			email:	email,
			action:	'submitList'
		},
		onComplete: function(x){
			if(x.responseText == 'badEmail'){
				alert('Invalid Email Address');
				return;
			}
			$('theStuff').update(x.responseText);
		}
	});
}


function showButton(){
	if($('model').value == '' || $('model').value == 'failson'){
		$('goButton').disabled=true;
	}
	else{
		$('goButton').disabled=false;
	}
}

function sendToUrl(){
	new Ajax.Request('/community/contest_processing.php',{
		method: 'get',
		parameters: {
			action: 'getUrl',
			item:	$('model').value
		},
		onComplete: function(transport){
			window.location = transport.responseText;
			//alert(transport.responseText);
		}
	});
}

function sendToUrl_SKU(){
	new Ajax.Request('/community/contest_processing.php',{
		method: 'get',
		parameters: {
			action: 'getUrl_SKU',
			item:	$('model').value
		},
		onComplete: function(transport){
			window.location = transport.responseText;
			//alert(transport.responseText);
		}
	});
}
maxL=50;
var bName = navigator.appName;
function taLimit(taObj) {
	if (taObj.value.length>=maxL){
		taObj.value = taObj.value.substr(0,50);
	} 
	return true;
}

function taCount(taObj,Cnt) { 
	objCnt=createObject(Cnt);
	objVal=taObj.value;
	if (objVal.length>maxL) objVal=objVal.substring(0,maxL);
	if (objCnt) {
		if(bName == "Netscape"){	
			objCnt.textContent=maxL-objVal.length;}
		else{objCnt.innerText=maxL-objVal.length;}
	}
	return true;
}
function createObject(objId) {
	if (document.getElementById) return document.getElementById(objId);
	else if (document.layers) return eval("document." + objId);
	else if (document.all) return eval("document.all." + objId);
	else return eval("document." + objId);
}

function showText(id){
    	if($(id).value == 'other'){
			if (id == 'device') {
				$('textDevice_layer').show();
				
				$('brand').update('<option value="">Select Brand</option><option value="other" SELECTED>Other</option>');
				$('textBrand_layer').show();
				
				$('model').update('<option value="">Select Model</option><option value="other" SELECTED>Other</option>');
				$('textModel_layer').show();
			}
			else if(id == 'brand'){
				$('textBrand_layer').show();
				
				$('model').update('<option value="">Select Model</option><option value="other" SELECTED>Other</option>');
				$('textModel_layer').show();
			}
			else if(id == 'model'){
				$('textModel_layer').show();
			}
    	}
    else{
        $('textDevice_layer').hide();
		$('textBrand_layer').hide();
		$('textModel_layer').hide();
    }
}

function clearValue(textId){
    var text = $(textId).value;
    if (text == 'Please Enter a Device' || text == 'Please Enter a Brand' || text == 'Please Enter a Model') {
        $(textId).value = '';
    }
}