function getcookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

function setcookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
var now = today.getTime();
if (nDays==null || nDays==0) {
	expire.setTime=now-6000;
} else {
	expire.setTime(today.getTime() + 3600000*24*nDays);
}
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
} 

var myimages=new Array()

function preloadimages(){
for (i=0;i<preloadimages.arguments.length;i++){
myimages[i]=new Image()
myimages[i].src=preloadimages.arguments[i]
}
}

preloadimages("/images/arrow_down.gif","/images/arrow_side.gif");


// function below not used and tested.
/*function getCustomerId() {
	 var cid = '<?PHP echo _SESSION['_cid']; ?>';
	 if (cid != '' ) {
		return cid;
	 } else {
		cid = getCookie('_cid');
		if (cid != '')
			return cid;
	}
	return '';
}	
*/

function openWindow(cid, aLink, params) {
	if (cid != "")
		aLink = modifyLink(cid, aLink);
	name = 'Edufone';
	if (params === undefined) {
		aRef = window.open(unescape(aLink), name);
	} else {
		aRef = window.open(unescape(aLink), name, unescape(params));
	}
	aRef.focus();
	var browserName=navigator.appName; 
	if (browserName=="Microsoft Internet Explorer")
	{ 
	 return false;
	}
}

function modifyLink(cid, aLink) {
	CJ_DEFAULT = 'sid=2';   // may be ?sid or &sid
	CJ_ID = 'sid=' + cid;
	MY_CJ_ID = '2457634';
	CJ_AFTER = '';     // put at end of part    
	LI_AFTER = '&subid=0';  // must go after this
	LI_ID = '&u1=' + cid;
	P_ID = '&mid=' + cid;
	P_AFTER = 'pubid=21000000000136134'; // can be at end of part

	if (aLink.indexOf(MY_CJ_ID) >= 0) {
		if (aLink.indexOf(CJ_DEFAULT) >= 0)
			return addIDToLink(aLink, MY_CJ_ID, CJ_ID, "", CJ_DEFAULT);
		else
			return addIDToLink(aLink, MY_CJ_ID, CJ_ID, "", "");
		// linkshare link
		// The field can be up to 72 alphanumeric characters long. The linking URL in this code must be appended with "&u1=xxxxxx" and should be inserted directly after "&subid=0
	} else if (aLink.indexOf('id=a1Vz2s0SU6Q') >= 0) {
		// must go right after &subid=0
	  	return addIDToLink(aLink, 'id=a1Vz2s0SU6Q', LI_ID, LI_AFTER, "");
	} else if (aLink.indexOf(P_AFTER) >= 0) {
	  	return addIDToLink(aLink, P_AFTER, P_ID, "", "");	
	} 
}

function addIDToLink(aLink, searchString, newText, afterText, existingText) {
	//alert('orig: ' + aLink);
		if (aLink.indexOf(searchString) >= 0) { // Just append cid to end of link?
			if (afterText == "" && existingText == "" ) { // some current cj links have ?sid=2
				aLink += newText; 
				
			} else if (existingText != '') { // insert before ending double quotes of string
			//alert('replace existing text' + aLink + ' newText: ' + newText + ' existingText: ' + existingText);
				indx = aLink.indexOf(existingText);
				if (indx >= 0) {
						var re = new RegExp(escape(existingText), 'gi');
						aLink = escape(aLink);
						aLink = aLink.replace(re, newText); // should only be in string once
						aLink = unescape(aLink);
				} 
						//alert('replaced existing text' + aLink);
			} else {
				// must go right after &subid=0. &subid=0 may be last item in string
					indx = aLink.indexOf(afterText);
					if (aLink.length == indx + afterText.length) {
						aLink += newText; 
					} else if (indx >= 0) {  // insert the string
						//len = sLink.length;
						temp = aLink.substr(0, indx);
						temp += newText;
						temp += aLink.substr(indx+1, aLink.length - indx);
						aLink = temp;
						//aLink = substr_replace(aLink, newText, indx + strlen(afterText), 0);
					} 
		  }
	} 
	
	return( aLink );	
}

function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}

/* Modified from jquery.youtube.js */
function showVideo(objectTag, title, desc)
{
   $tag = unescape(objectTag);
   // replace + with space
   $tag=$tag.replace(/\+/g,' ');
   // Add autoplay
   $tag=$tag.replace(/<embed/,'<param name="autoplay" value="1"><embed');
   $tag=$tag.replace(/" type="application/,'&autoplay=1" type="application');
	
   jQuery('#video').html($tag).focus();
   displayVideoDesc(title, desc); 
}

/* for now hard code the id where the video description/lyrics are displayed */
function displayVideoDesc(title, desc) {
	 if (title != null && title.length > 0) {
		$titl = unescape(title);
		// replace + with space
	    $titl=$titl.replace(/\+/g,' ');
	 	jQuery('#video_title').html($titl);
	 } else {
	   jQuery('#video_title').html('');
	 }
	 if (desc != null && desc.length > 0) {
		$tag = unescape(desc);
	    // replace + with space
	    $tag=$tag.replace(/\+/g,' ');
	 	jQuery('#video_desc').html($tag);
	 } else {
		 jQuery('#video_desc').html('');
	 }
}

// Change content in podcast player to display an Edufone podcast
// for now hard code the id where the video description/lyrics are displayed 
function playEdufonePodcast(title, desc, lang, url, dwnlInstructions, text, 
							   learningUnits, membership, musicianURL, music) {
	//	 alert('displayEdufonePodcast ' + url);
	 if (title != null && title.length > 0) {
		$titl = unescape(title);
		// replace + with space
	    $titl=$titl.replace(/\+/g,' ');
	 	jQuery('#selected_pod_title').html($titl);
	 } else {
	   jQuery('#selected_pod_title').html('');
	 }
	 if (desc != null && desc.length > 0) {
		$tag = unescape(desc);
	    // replace + with space
	    $tag=$tag.replace(/\+/g,' ');
	 	jQuery('#selected_pod_desc').html($tag);
	 } else {
		 jQuery('#selected_pod_desc').html('');
	 }
	  if (url != null && url.length > 0) {
		$url2 = unescape(url);
	    // replace + with space
	    $url2=$url2.replace(/\+/g,' ');
		$dwnldStr = '<a href="' + $url2 + '" target="_blank"><img class="pic_10 no_brdr" src="gifs/orangeArrow.gif" alt="" /></a>';
		$dwnldStr += '<a href="' + $url2 + '" target="_blank">';
		if (lang == 'en') {
			$dwnldStr += 'Download</a> this podcast (MP3)';
		} else {
			$dwnldStr += 'Bajar</a> este podcast (MP3)';
		}
		if (dwnlInstructions != null && dwnlInstructions.length > 0) {
			$dli = unescape(dwnlInstructions);
			$dli=$dli.replace(/\+/g,' ');
		} else {
			$dli = '';
		}
		$dwnldStr += '<p class="font9 ltgry pad_30_left">' + $dli + '</p>';
	 	jQuery('#selected_pod_download').html($dwnldStr);
	 } else {
		 jQuery('#selected_pod_download').html('');
	 }
	  if (url != null && url.length > 0) {
		$url2 = unescape(url);
	    // replace + with space
	    $url2=$url2.replace(/\+/g,' ');
		$str = '<object data="http://www.edufone.com/webplayer.swf" type="application/x-shockwave-flash" width="240" height="64">';
		$str += '<param name="movie" value="http://www.edufone.com/webplayer.swf"/><param name="menu" value="false"/>';
		$str += '<param name="scale" value="noscale"/><param name="bgcolor" value="#FFFFFF"/>';
		$str += '<param name="flashvars" value="src=' + $url2 + '&amp;autostart=yes&amp;loop=no"/></object>';
	 	jQuery('#selected_pod_player').html($str);
	 } else {
		 jQuery('#selected_pod_player').html('');
	 }
	
	 jQuery('#selected_pod_text').html('');
						
	 // Music
	  if (music != null && music.length > 0 &&
		  musicianURL != null && musicianURL.length > 0) {
		$music2 = unescape(music);
	    // replace + with space
	    $music2=$music2.replace(/\+/g,' ');
		$musURL = unescape(musicianURL);
	    // replace + with space
	    $musURL=$musURL.replace(/\+/g,' ');
		$strMusic = '<p class="font9 ltgry">With music by <a href="' + $musURL + '" title="musician\'s website" target="_blank">' + $music2 + '</a></p>';
							
		jQuery('#selected_pod_music').html($strMusic);
	 } else {
		 jQuery('#selected_pod_music').html('');
	 }
	 //alert('displayEdufonePodcast ' + $dwnldStr);
	 //jQuery('#focus_here').focus();
	 jQuery("input:hidden").focus();
}


// Change content in podcast player to play a podcast from another podcaster
// for now hard code the id where the video description/lyrics are displayed 
function playPodcast(title, desc, url, podcaster, lang, dwnlInstructions) {
	//	 alert('displayEdufonePodcast ' + url);
	 if (title != null && title.length > 0) {
		$titl = unescape(title);
		// replace + with space
	    $titl=$titl.replace(/\+/g,' ');
	 	jQuery('#selected_pod_title').html($titl);
	 } else {
	   jQuery('#selected_pod_title').html('');
	 }
	 if (desc != null && desc.length > 0) {
		$tag = unescape(desc);
	    // replace + with space
	    $tag=$tag.replace(/\+/g,' ');
	 	jQuery('#selected_pod_desc').html($tag);
	 } else {
		 jQuery('#selected_pod_desc').html('');
	 }
	  if (url != null && url.length > 0) {
		$url2 = unescape(url);
	    // replace + with space
	    $url2=$url2.replace(/\+/g,' ');
		$dwnldStr = '<a href="' + $url2 + '" target="_blank"><img class="pic_10 no_brdr" src="gifs/orangeArrow.gif" alt="" /></a>';
		$dwnldStr += '<a href="' + $url2 + '" target="_blank">';
		if (lang == 'en') {
			$dwnldStr += 'Download</a> this podcast (MP3)';
		} else {
			$dwnldStr += 'Bajar</a> este podcast (MP3)';
		}
		if (dwnlInstructions != null && dwnlInstructions.length > 0) {
			$dli = unescape(dwnlInstructions);
			$dli=$dli.replace(/\+/g,' ');
		} else {
			$dli = '';
		}
		$dwnldStr += '<p class="font9 ltgry pad_30_left">' + $dli + '</p>';
	 	jQuery('#selected_pod_download').html($dwnldStr);
	 } else {
		 jQuery('#selected_pod_download').html('');
	 }
	  if (url != null && url.length > 0) {
		$str = '<object data="http://www.edufone.com/webplayer.swf" type="application/x-shockwave-flash" width="240" height="64">';
		$str += '<param name="movie" value="http://www.edufone.com/webplayer.swf"/><param name="menu" value="false"/>';
		$str += '<param name="scale" value="noscale"/><param name="bgcolor" value="#FFFFFF"/>';
		$str += '<param name="flashvars" value="src=' + $url2 + '&amp;autostart=yes&amp;loop=no"/></object>';
	 	jQuery('#selected_pod_player').html($str);
	 } else {
		 jQuery('#selected_pod_player').html('');
	 }
	 			
	 // podcaster
	  if (podcaster != null && podcaster.length > 0) {
		$podcaster2 = unescape(podcaster);
	    // replace + with space
	    $podcaster2=$podcaster2.replace(/\+/g,' ');
		
		$strpodcaster = '<p class="font9 ltgry">Podcaster: ' + $podcaster2 + '</p>';
							
		jQuery('#selected_pod_music').html($strpodcaster);
	 } else {
		 jQuery('#selected_pod_music').html('');
	 }
	 //alert('displayEdufonePodcast ' + $dwnldStr);
	 jQuery('#focus_here').focus();
	 //jQuery("input:hidden").focus();
}

