//get URL parameter function from www.netlobo.com/url_query_string_javascript.html
function gup(name)
{
name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regexS = "[\\?&]"+name+"=([^&#]*)";
var regex = new RegExp( regexS );
var results = regex.exec( window.location.href );
if( results == null )
return "";
else
return results[1];
}

//Get cookie routine by Shelley Powers 
function get_cookie(Name) {
var search = Name + "="
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search)
// if cookie exists
if (offset != -1) { 
offset += search.length
// set index of beginning of value
end = document.cookie.indexOf(";", offset);
// set index of end of cookie value
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end))
}
}
return returnvalue;
}



//Grab user agent and make it lower in case things change
var uagent = navigator.userAgent.toLowerCase();

//Check if mobile user chose to break out to full PS version as passed in the URL parameter
var mHomePval = gup('mobileHome');
if (mHomePval == 'main') {
	document.cookie = "mobileHome=main";
}
var mHomeCval = get_cookie("mobileHome");

/* this function simply just detects if the user agent is for a mobile browser */
function isMobileUserAgent() { 
return  ((uagent.indexOf('webkit') != -1 && mHomeCval == 'main')  || 
			uagent.indexOf('iphone') != -1 || //Check for iPhone
			uagent.indexOf('ipad') != -1 || //Check for iPad
			//uagent.indexOf('ipod') != -1 || //Check for iPod
            uagent.indexOf('android') != -1); //Check for Android
   			//uagent.indexOf('blackberry') != -1 || //Check for BlackBerry
			//uagent.indexOf('windows ce') != -1 || //Check for Windows Mobile
			//uagent.indexOf('bolt') != -1);  //Check for the Bolt browser. Bolt reports as Windows (not Windows CE) on blackberry so must look for the browser itself.
			

}

function insertCss(resourcePath) { 
	var headID = document.getElementsByTagName("head")[0];         
	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = resourcePath;
	cssNode.media = 'screen';
	headID.appendChild(cssNode);
}

/* district level pref */
var districtpref = pref;
if( document.getElementById('mobilepages') != null) {
	districtpref=document.getElementById('mobilepages').value;	
}	

function isDistrictMobileWebEnabled() {
	return districtpref==1;
}

//check for webkit and make sure user has not requested main PS version.
// No webkit means no mobile version.
if (isDistrictMobileWebEnabled() && uagent.indexOf('webkit') != -1 && mHomeCval != 'main') { 
//user agent does have webkit and doesn't want main version so now detect if mobile device
if (uagent.indexOf('iphone') != -1) {window.location="mobile/home.html";} //Check for iPhone
//else if (uagent.indexOf('ipod') != -1) {window.location="mobile/home.html";} //Check for iPod
else if (uagent.indexOf('ipad') != -1) {window.location="mobile/home.html";} //Check for iPad
else if (uagent.indexOf('android') != -1) {window.location="mobile/home.html";} //Check for Android
//else if (uagent.indexOf('blackberry') != -1) {window.location="mobile/home.html";} //Check for BlackBerry
//else if (uagent.indexOf('windows ce') != -1) {window.location="mobile/home.html";} //Check for Windows Mobile
//else if (uagent.indexOf('palm') != -1) {window.location="mobile/home.html";} //Check for PalmOS
//else if ((uagent.indexOf('series60') != -1)&&(uagent.indexOf('symbian') != -1)) {window.location="mobile/home.html";} //Check for Symbian S60 Smartphones
//else if (uagent.indexOf('bolt') != -1) {window.location="mobile/home.html";} //Check for the Bolt browser. Bolt reports as Windows (not Windows CE) on blackberry so must look for the browser itself.
}
