// global.js
// copied from  /v3/core.js
function makeHttpRequest(url, callback_function, return_xml, refreshrequired, noload) {

   if(!noload) {
	   var loading = 'Loading.....';
	   eval(callback_function + '(loading)');
   }

   // IE sometimes caches requests, so send a random number with each query
   var number = Math.random();
   var http_request = false;

   if (window.XMLHttpRequest) { // Mozilla, Safari,...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
           http_request.overrideMimeType('text/xml');
       }
   } else if (window.ActiveXObject) { // IE
       try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
           try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) {}
       }
   }

   if (!http_request) {
       alert('Unfortunatelly you browser doesn\'t support this feature.');
       return false;
   }
   http_request.onreadystatechange = function() {
       if (http_request.readyState == 4) {
           if (http_request.status == 200) {
               if (return_xml) {
                   eval(callback_function + '(http_request.responseXML)');
               } else {
                   eval(callback_function + '(http_request.responseText)');
               }

			   // this ensures that the page has loaded AND that we require a data refresh
			   if (refreshrequired) {
			   		getData();
			   }

           } else {
               alert('There was a problem with the request.(Code: ' + http_request.status + ')');
           }
       }
   }
   http_request.open('GET', url + '&rand=' + number, true);

   http_request.send(null);

   return false;
}


function makeHttpPostRequest(url, params, callback_function, refreshrequired) {

	var number = Math.random();
      var loading = 'Loading.....';

	  params = params + '&n=' + number;

   //eval(callback_function + '(loading)');

   if (window.XMLHttpRequest) { // Mozilla, Safari,...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
           http_request.overrideMimeType('text/xml');
       }
   } else if (window.ActiveXObject) { // IE
       try {
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (e) {
           try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
           } catch (e) {}
       }
   }

		http_request.open("POST", url, true);

		//Send the proper header information along with the request
		http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		http_request.setRequestHeader("Content-length", params.length);
		http_request.setRequestHeader("Connection", "close");

		http_request.onreadystatechange = function() {//Call a function when the state changes.
			if(http_request.readyState == 4 && http_request.status == 200) {
				eval(callback_function + '(http_request.responseText)');
			}

			   // this ensures that the page has loaded AND that we require a data refresh
			   if (refreshrequired) {
			   		refreshAfterPost();
			   }
		}
		http_request.send(params);
		return false;
}

function showhide(id)
{
	if(document.getElementById(id).style.display == "none")
	{
		document.getElementById(id).style.display = "";
	}
	else
	{
		document.getElementById(id).style.display = "none";
	}
}

function SetAllCheckBoxes(FormName, FieldName, CheckValue)
{

	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}



//DB 27/03/08 - same as above but toggles all on and all off depending on the value of the check all checkbox.
function SetAllCheckBoxesToggle(FormName, FieldName, CheckValue, Check)
{


	if (document.forms[FormName].elements[Check].checked) {
		CheckValue = true;
	}else{
		CheckValue = false;
	}

	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}

// disable a form element
function disable(string)
{
		string.disabled=true;
		return true;
}

function showHideFlash() {


	var e=document.getElementsByTagName("embed");
	for(i=0;i<e.length;i++){
			if (e[i].style.visibility == "hidden") {
				e[i].style.visibility="visible";
			}else{
				e[i].style.visibility="hidden";
			}
		}
}


function show(id)

{

		document.getElementById(id).style.display = "";

		return true;

}



function hide(id)

{

		document.getElementById(id).style.display = "none";

		return true;

}


// thanks to tim for this little gem

// http://www.briardene.com/js/js.js

// Determine Browser Height and Width definition

function GetHeightDef()

{

	if (self.innerHeight){ // FF and Safari

		HeightDef = self.innerHeight;

		WidthDef = self.innerWidth;

	}

	else if (document.documentElement && document.documentElement.clientHeight) { // IE 6

		HeightDef = document.documentElement.clientHeight;

		WidthDef = document.documentElement.clientWidth;

	}

	else if (document.body){ // IE

		HeightDef = document.body.clientHeight;

		WidthDef = document.body.clientWidth;

	}

}



function uploadPanel() {



	GetHeightDef();

	// divs are called

	// uploadingscreen and uploadingpanel

	var panelWidth = 450;

	var panelHeight = 200;

	document.getElementById('uploadingscreen').style.height = HeightDef + "px";

	document.getElementById('uploadingscreen').style.width = WidthDef + "px";

	// now position the panel

	document.getElementById('uploadingpanel').style.top = (HeightDef - (panelHeight+100))/2 + "px";

	document.getElementById('uploadingpanel').style.left = (WidthDef - panelWidth)/2 + "px";

	document.getElementById('uploadingscreen').style.display='';

	document.getElementById('uploadingpanel').style.display='';



}

function checkEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false;
		}


		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false;

		 }

		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false;
		 }
 		 return true;
}

function selectMenuRedirect(field)
{
    var myindex  = field.selectedIndex;
    var SelValue = field.options[myindex].value;
    window.location.href = SelValue;
    return true;
}

function ucfirst(string) {
  // makes the first letter of the string uppercase
  // string becomes String
  return string.substr(0, 1).toUpperCase() + string.substr(1);
}

function open_window(mypage,myname,w,h,scrollbars,status,pos){

	GetHeightDef();


	if(pos == 'center'){
	  LeftPosition=(WidthDef)?(WidthDef-w)/2:100;
	  TopPosition=(HeightDef)?(HeightDef-h)/2:100;
	}
	else if((pos != 'center' && pos != 'random') || pos==null){
	  LeftPosition=0;
	  TopPosition=20;
	}


	win=window.open(mypage,myname, "width=" + w + ", height=" + h + ",top=" + TopPosition + ",left=" + LeftPosition + ",scrollbars="+scrollbars+",location=no,directories=no,status=" + status + ",menubar=no,toolbar=no,resizable=no");



}

function URLcheck(string)
{
	if(confirm('You are about to navigate away from Pitchero.\nPitchero is not responsible for the content of external internet sites.\n\nProceed to ' + string + '?\n\n(If you have a popup blocker enabled, you may need to control click the link)'))
	{
		window.open(string, '_blank');
	}
}

String.prototype.reverse = function(){
splitext = this.split("");
revertext = splitext.reverse();
reversed = revertext.join("");
return reversed;
}

// thanks to tim for this little gem

// http://www.briardene.com/js/js.js

// Determine Browser Height and Width definition

function GetHeightDef()

{

	if (self.innerHeight){ // FF and Safari

		HeightDef = self.innerHeight;

		WidthDef = self.innerWidth;

	}

	else if (document.documentElement && document.documentElement.clientHeight) { // IE 6

		HeightDef = document.documentElement.clientHeight;

		WidthDef = document.documentElement.clientWidth;

	}

	else if (document.body){ // IE

		HeightDef = document.body.clientHeight;

		WidthDef = document.body.clientWidth;

	}

}

function ToolTipie(){
// ie6 only
if (document.all&&document.getElementById) {


    if(document.getElementById('network-nav')) {


        litags = document.getElementById('network-nav').getElementsByTagName('li');
        for (i=0;i<litags.length;i++){
            litags[i].onmouseover=function() {
                this.className+=" over";
            }
            litags[i].onmouseout=function() {
                this.className=this.className.replace(" over", "");
            }
        }
    }

    if(document.getElementById('marketing-nav')) {


        litags = document.getElementById('marketing-nav').getElementsByTagName('li');
        for (i=0;i<litags.length;i++){
            litags[i].onmouseover=function() {
                this.className+=" over";
            }
            litags[i].onmouseout=function() {
                this.className=this.className.replace(" over", "");
            }
        }
    }

}



}


function setCommentParentID(media_type, media_id, parent_id, comment_id, author) {
    if(parent_id) {

        document.forms['comment_' +  media_type + '_' + media_id].parent_id.value = parent_id;

        document.getElementById('replying_to_' +  media_type + '_' + media_id + '_author').innerHTML = author;
        document.getElementById('replying_to_' +  media_type + '_' + media_id + '_link').href = '#comment_' + comment_id;
        show('replying_to_' +  media_type + '_' + media_id);
    }
}

function removeCommentParentID(media_type, media_id) {
    document.forms['comment_' +  media_type + '_' + media_id].parent_id.value = '';
    hide('replying_to_' +  media_type + '_' + media_id);
}

function checkCommentForm(form) {

    if(form.words.value) {
        return true;
    }
    else {
        alert('Please type in your comment......');
        form.words.focus();
        return false;
    }

}

function bookmark() {
    alert("To bookmark or add this page to your favourites, click OK and then press CTRL + D");
}

function gaTracker(path) {

    // JM 04/01/2010
    // Only attempt to track the link of GA has loaded
    // This way, we can keep the GA code at the bottom of the page

    if (typeof pageTracker === 'undefined') {
        // variable is undefined
    }
    else {
        pageTracker._trackPageview('/internal-tracker' + path);
    }
    return false;
}




function recordOutboundLink(link, category, action) {

    if (typeof pageTracker === 'undefined') {
        // GA not loaded
        // degrade gracefully
        document.location = link.href;
    }
    else {
        pageTracker._trackEvent(category, action);
        setTimeout('document.location = "' + link.href + '"', 100);
    }
}






$(document).ready(ToolTipie);
