function qsDecode(qs) {
	var HEXCHARS="0123456789ABCDEFabcdef"; 
	var strText="";
	var i= 0;
	while (i<qs.length) {
		var ch = qs.charAt(i);
		if (ch == "+") {
			strText += " ";
			i++;
		} else if (ch == "%") {
			if (i < (qs.length-2) && HEXCHARS.indexOf(qs.charAt(i+1)) != -1 && HEXCHARS.indexOf(qs.charAt(i+2)) != -1 ) {
				strText += unescape( qs.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + qs.substr(i) );
				strText += "%[ERROR]";
				i++;
			}
		} else {
			strText += ch;
			i++;
		}
	}
	return strText;
}

function qs(q) {
	strFQS = window.location.search.substring(1);
	arrFQS = strFQS.split("&");
	for (i=0;i<arrFQS.length;i++){
		arrCur = arrFQS[i].split("=");
		if (arrCur[0]==q){
			return qsDecode(arrCur[1]);
		}
	}
	return "";
}

function getBrowser(){
	var agent =  navigator.userAgent.toLowerCase();
	if (agent.indexOf('msie') != -1) {
		return "ie";
	} else { 
		return "other";
	}
}

function resizeViewport(targetWidth, targetHeight) {
	var targetWidth = parseInt(targetWidth);
	var targetHeight = parseInt(targetHeight);
	var resizeCounter = 0;
	var browser = getBrowser();
	if (browser == "ie") {
		window.resizeTo(targetWidth+10, targetHeight+80);   
	} else {
		window.resizeTo(targetWidth+8, targetHeight+85);
	}   		
	return true;
}

function bandwidthSuggestion(speed){
	if (speed >= 300000) {
		bandwidthSuggestion = "300";
	}
	else {
		bandwidthSuggestion = "150";
	}
	return bandwidthSuggestion;
}

function view(){
	if (Ext.getCmp('searchId').value) {
		playerControls('stop');
		clearTimeout();
		window.open('index.php?id='+Ext.getCmp('searchId').value, '_self');
	} else {
		alert('Invalid Selection\n\nPlease choose a value from the drop down list.')
	}
}

function send(btn){
	if (qs('id') != "") {
		Ext.getCmp('emailToName').validate() 
		Ext.getCmp('emailToEmail').validate()
		Ext.getCmp('emailFromName').validate() 
		Ext.getCmp('emailFromEmail').validate()
		
		if (Ext.getCmp('emailToName').isValid() && Ext.getCmp('emailToEmail').isValid() && Ext.getCmp('emailFromName').isValid() && Ext.getCmp('emailFromEmail').isValid()) {
			//Remove onclick from send button
			sendOnClickText = btn.onclick;
			Ext.getCmp('emailForm').getForm().submit({ 
				waitMsg: 'Sending Email...', 
				url: 'email.php',
				reset: true,
				success: function(form, action){ 
					btn.onclick = sendOnClickText;
					alert('Success\n\nYour email has been sent successfully.'); 
				}, 
				failure: function(form, action){ 
					btn.onclick = sendOnClickText;
					alert('Failure\n\nYour email has not been able to be sent.'); 
				}
			})
		}
	} else {
		alert('Nothing to send!\n\nPlease select a video before sending.'); 
	}
}
