function searchAndReplace(input, stringToFind, stringToInsert) {
    var output = "";
    var findLen = stringToFind.length;
    while (input.indexOf(stringToFind)>=0) {
        var currOffset = input.indexOf(stringToFind);
        output += input.substr(0, currOffset);
        output = output.substr(0, output.length);
        // delete the last char of output
        output = output += stringToInsert;
        input = input.substr((currOffset)+findLen, input.length-1);
    }
    output += input;
    return output;
}

function thisMovie(movieName) {
//alert('thisMovie being called ');
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName]
	}else {
		return document[movieName]
	}
}

function onPurchase() {
	top.window.location = 'http://'+piratesDomain+'/pirates/v3/register/finalization';
}

function popContest(url){
  window.open(url,"popContest","width=612,height=816,scrollbars=0,resizable=1");
}

function popUp(url, width, height){
  window.open(url,"popUp","width="+width+",height="+height+",scrollbars=0,resizable=0");
}

function reset_form(id){
  document.getElementById(id).reset()
}

function validate_field(id)
{
  //by camartin
  //check if an input field is null or blank

  if ( document.getElementById(id).value == null || document.getElementById(id).value == "" )
  {
    //alert(id + ' is blank')
    return false
  }
  else {
    //alert(id + ' is not blank')
    return true
  }
}


function validate_email(email)
{
  //simple email validation
  //returns true or false
  return (email.indexOf(".") > 2) && (email.indexOf("@") > 0)
}


function max_chars(max)
{
  //we need a better way to make this a reusable function
  var textField = document.upload_video_form.description

  if (max == null || max == '')
  {
    var max=35//max chars allowed
  }

  if (textField.value.length > max)
  {
    textField.value= textField.value.substring(0,max)
    textField.blur()
    //alert("You have reached the maximum characters allowed.")
  }
}


function upload_video_validation()
{
  //by camartin
  //check that all input fields are NOT blank or null
  //do a simple email validation
  //ungrey the Upload Submission button when the user check marks the terms

  submit_form = false
  no_blank_fields =  false
  valid_email = false
  terms = false
  valid_comments = false;

  textField = document.upload_video_form.comments;
  if (textField.value.length > 0) {
    valid_comments = true;
  }


/*
  //for video upload form
  if ( validate_field('file1') && validate_field('email') && validate_field('address') &&
  validate_field('city') && validate_field('state') && validate_field('zip') )
*/

/*
  2008-01-24 updated and removed validate_field('file1') for V-Day Contest 2008
*/
  if ( valid_comments && validate_field('email') && validate_field('address') &&
  validate_field('city') && validate_field('state') && validate_field('zip') )
  {
    //if all fields are NOT blank
    no_blank_fields = true
    //alert('no_blank_fields: ' + no_blank_fields);
  }


  eMail=document.getElementById("email")
  if (validate_email(eMail.value))
  {
    valid_email = true
  }
  else {
    //alert ('Invalid email')
    //eMail.focus()
  }

  if (document.upload_video_form.terms.checked == true )
  {
    terms = true
    //alert('terms: ' + terms);
  }

  if (no_blank_fields && valid_email && terms)
  {
    //alert('switch btn:');
    object = document.getElementById('upload_btn_id_off')
    object.style.display = 'none'

    object = document.getElementById('upload_btn_id_on')
    object.style.display = ''

    return true
  }

  //return submit_form
}


function upload_video_submit_form()
{
  //submit form is validation is true
  if ( upload_video_validation() )
  {
    document.forms['upload_video_form'].submit()
  }
}


function upload_video_reset_form()
{
  reset_form('upload_video_form')
/*
  object = document.getElementById('upload_btn_id_on')
  object.style.display = 'none'

  object = document.getElementById('upload_btn_id_off')
  object.style.display = ''
*/
}


function redirectToPiratesChannel() {

	var xmlHttp;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) { // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				//alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			//alert(xmlHttp.responseText);
			xmlDoc = createXMLParser(xmlHttp.responseText);
			if(xmlDoc.getElementsByTagName("status").length > 0) {
				statusValue = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;
				//alert("status="+statusValue);
				if(statusValue == "success") {
					if(xmlDoc.getElementsByTagName("url").length > 0) {
						url = xmlDoc.getElementsByTagName("url")[0].childNodes[0].nodeValue;
						if(url != undefined && url.indexOf("http:") || url.indexOf("https:")) {
							top.window.location = url;
						}
					}
				}
			}
		}
	}

	xmlHttp.open("GET","http://"+piratesDomain+"/pirates/v3/util/rpc/getPiratesChannel?cacheBuster=" + Math.random(), true);
	xmlHttp.send(null);

}


function checkLiveSupport() {

	var xmlHttp;

	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) { // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				//alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {

			//alert(xmlHttp.responseText);

			xmlDoc = createXMLParser(xmlHttp.responseText);

			result = xmlDoc.getElementsByTagName("result")[0].childNodes[0].nodeValue;

			if(result != null && result != 'undefined' && result == "success") {

				if(xmlDoc.getElementsByTagName("dayAndTime").length > 0) {

					dayAndTime = xmlDoc.getElementsByTagName("dayAndTime")[0].childNodes[0].nodeValue;

					dayAndTimeArray = dayAndTime.split("|");

					params = getURLParams();

					if(params["day"] != undefined && params["hour"] != undefined && params["meridian"] != undefined) {
						day = params["day"];
						hour = params["hour"];
						meridian = params["meridian"];
					} else {
						day = dayAndTimeArray[0];
						hour = dayAndTimeArray[1];
						meridian = dayAndTimeArray[2];
					}

					/*
					day = "Sat";
					hour = "3";
					meridian = "PM";
					*/

					isSatOrSun = (day.indexOf("Sat") == 0 || day.indexOf("Sun") == 0);
					isWithinTimeSpanSatOrSun = (hour >= 6 && hour <=11 && meridian == "AM") || hour == 12 && meridian == "PM" || (hour >= 1 && hour < 7  && meridian == "PM");

					isMonThruFri = (isSatOrSun != true);
					isWithinTimeSpanMonThruFri = (hour >= 10 && hour <=11 && meridian == "AM") || hour == 12 && meridian == "PM" || (hour >= 1 && hour < 7  && meridian == "PM");

					/*
					alert("isSatOrSun="+isSatOrSun);
					alert("isWithinTimeSpanSatOrSun="+isWithinTimeSpanSatOrSun);
					alert("isMonThruFri="+isMonThruFri);
					alert("isWithinTimeSpanMonThruFri="+isWithinTimeSpanMonThruFri);
					*/

					if((isSatOrSun && isWithinTimeSpanSatOrSun) || (isMonThruFri && isWithinTimeSpanMonThruFri)) {
						//document.getElementById("liveSupportLink").className = "DisplayBlock";
						document.getElementById("liveSupportLink").style.display = "block";
					} else {
						//document.getElementById("liveSupportLink").className = "DisplayNone";
						document.getElementById("liveSupportLink").style.display = "none";
					}

				}
			}

		}
	}

	xmlHttp.open("GET","http://"+piratesDomain+"/pirates/v3/util/calendar/dayAndTime?cacheBuster=" + Math.random(), true);
	xmlHttp.send(null);

}

function createXMLParser(xText){
	if(typeof DOMParser != "undefined") {
		var parseX = new DOMParser();
		return parseX.parseFromString(xText, "text/xml");
	} else if(typeof ActiveXObject != "undefined"){
		var XMLobj = new ActiveXObject("Microsoft.XMLDOM");
		if(XMLobj) {
			XMLobj.async = false;
			XMLobj.loadXML(xText);
			return XMLobj;
		} else {
			//alert("Could not parse XML in IE");
			return false;
		}
	} else {
		//alert("Failed");
		return false;
	}
}

function postProcessor() {
	checkLiveSupport();
	//window.setTimeout("checkLiveSupport()", 5000); // 5 seconds
	window.setInterval("checkLiveSupport()", 60000 * 15); // 60 seconds * 15
}

function getURLParams() {
	var params = new Array();
	var strHref = window.location.href;
	if ( strHref.indexOf("?") > -1 ){
		var strQueryString = strHref.substr(strHref.indexOf("?")+1);
		var re = new RegExp("^(.*)?\#.*", "g");
		strQueryString = strQueryString.replace(re, "$1");
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ) {
			var aParam = aQueryString[iParam].split("=");
			params[aParam[0]] = aParam[1];
		}
	}
	return params;
}

function getUsername() {
	if(getcookie("USERNAME") != null && getcookie("USERNAME") != "") {
		return getcookie("USERNAME");
	} else {
		return "Guest";
	}
}

function updateElementTextByElementId(id, newText) {
	document.getElementById(id).innerHTML=newText;
}

function setFocus(id){
  document.getElementById(id).focus();
}


