gadgets.util.registerOnLoadHandler(request);

var viewer_name = '';
var currentview = '';
var viewerId ='';
var ownerId ='';
var html='';
var friends_count = 0;
var friendListStr = null;// gives the id's of all the friends which are selected
var friendNamesStr = null;// gives the names of all the friends which are selected
var friendCurrPage = 1; // this is used for paging.. in the friends list..
var friend_name = '';
var friend_id = '';
var viewer_friends = '';
var viewer_friends_length = '';
var url='';
var viewer = '';
var templ_name= '';
var message= '';
var owner ='';
var owner_name = '';
var allFriends = '';
var received = 0; //total number of received superscraps by the owner.
var friendOnlyFlag = 'false';
var friendId = '';
var friendsToInvite = ''
var noOfFriends = null;
var viewer_invite_friends_length = null; 
var imageCreated = false;
var color = '';
var selLang = 'hi';	// hindi
var orkut_opensocial_user_id='';

/********** app and domain related variables**********/
//var appId = '234455488473'; // staging
var appId = '114612561843'; // live

//var area= 'staging';
var area= 'superscrapopensocial/';
var domain = 'http://apps.postalz.com/';
var image_domain = 'http://apps.postalz.com/';
var containerUrl = 'http://www.orkut.com/';
var prevSelectedTab = 'xmas_tab';
var tabToBeSelected = 'xmas_tab';
/********** /app and domain related variables**********/

/******************get data from query string********/
var querydata = gadgets.views.getParams(); 
var image_url = querydata["image"]; // image contains the url of the image
/*************************end************************/

	var totalTabs = 5;
	var love_tmpl = 12;
	var cause_tmpl = 12;
	var nice_tmpl = 12;
	var spiritual_tmpl = 12;
	var tease_tmpl = 12;
	var totalNoOfTemplates = love_tmpl+cause_tmpl+nice_tmpl+spiritual_tmpl+tease_tmpl;
	var noOfTemplatesLoaded = 0;

	var counter = 100;

function request() 
{		
		if(gadgets.window)
		{
			gadgets.window.adjustHeight('1000px');
		}
		document.getElementById('main').innerHTML='<div id="subMain"></div><div id="loader" style="background-color:#f5f5f5;height:500px;border:solid 1px #e0e0e0;"><center><img style="padding-top:234px;" src="'+domain+area+'/orkut/images/loader.gif" alt="loading" title="loading"/><br/><br/><span style="font-size:12px;color:#7b7b7b;">Loading SuperScrap...</span><br/></center></div>';
		var req = opensocial.newDataRequest();
		var opt_params = { };
		opt_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL,opensocial.Person.Field.NAME,opensocial.Person.Field.THUMBNAIL_URL];
		req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.VIEWER,opt_params),"viewer");
		req.add(req.newFetchPersonRequest(opensocial.IdSpec.PersonId.OWNER,opt_params),"owner");

		var idspec = opensocial.newIdSpec({'userId' : 'OWNER', 'groupId' : 'FRIENDS'});
		var app_friend_params = {};
		app_friend_params[opensocial.DataRequest.PeopleRequestFields.MAX]=1000;
		app_friend_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];
		req.add(req.newFetchPeopleRequest(idspec,app_friend_params),'ownerFriends');
		req.send(response);
}

function getOrkutIdByUrl(profile_url){
	var regex = /uid=([^&#]+)/;
	var result = profile_url.match(regex);
	if (result.length == 2) {
	  ownerOrkutUid = result[1];
	  /* uid now contains the viewer's orkut UID */
	} else {
	  /* there was a problem getting the UID */
	}
	return ownerOrkutUid;
}

function getOrkutId(user){
	var ownerOrkutUid = '';
	var profile_url = '';
	if(user == 'undefined' || user == null ){
		profile_url = user.getField(opensocial.Person.Field.PROFILE_URL);
	}else{
		profile_url = user.getField(opensocial.Person.Field.PROFILE_URL);
	}
	var regex = /uid=([^&#]+)/;
	var result = profile_url.match(regex);
	if (result.length == 2) {
	  ownerOrkutUid = result[1];
	  /* uid now contains the viewer's orkut UID */
	} else {
	  /* there was a problem getting the UID */
	}
	return ownerOrkutUid;
}

function switchTemplateTab(pTabName,pPageNo){
	tabToBeSelected = pTabName+"_tab";
	document.getElementById(tabToBeSelected).className='linkbold';
	if(prevSelectedTab != tabToBeSelected){
		document.getElementById(prevSelectedTab).className='linknormal';
		prevSelectedTab = tabToBeSelected;
	}
	document.getElementById("id_templ_container").style.display = '';
	document.getElementById("id_templ_container").innerHTML = '<div align="center" style="height:420px;width:800px;"><img src ="'+domain+area+'/orkut/images/loader.gif" /></div>';
	if(pPageNo==null){
		pPageNo=1;
	}
	fetchTemplates(pTabName,pPageNo);
	document.getElementById("id_canvas").style.display = 'none';
	document.getElementById("id_editor").style.display = 'none';
	document.getElementById("blank_textarea").style.display = 'none';
}


function initTab(){
	document.getElementById(prevSelectedTab).className='linkbold';
	
}

function setDomain(responseText)
{
		domain = trimString(responseText.text) + "/";
		setTimeout(setDomain,2000);
}

function fetchTemplates(tabName,pageNo)
{
	var d = new Date();
	var noCacheStr = d.getTime(); // this is to fool the browser
	gadgets.io.makeRequest(domain+area+'/orkut/getTemplates.jsp?reqTab='+tabName+'&reqPageNo='+pageNo+'&ver='+noCacheStr, setTemplateHtml,null);			
	gadgets.window.adjustHeight();
}

function fetchTemplateTabs()
{
	var d = new Date();
	var noCacheStr = d.getTime(); // this is to fool the browser
	gadgets.io.makeRequest(domain+area+'/orkut/tabMenu.jsp?ver='+noCacheStr, setTabHtml,null);			
}

function response(data) 
{
	viewer = data.get("viewer").getData();
	owner = data.get("owner").getData();
	owner_name = owner.getDisplayName();
	ownerId = getOrkutId(owner);
	
	var d = new Date();
	var noCacheStr = d.getTime(); // this is to fool the browser
	if(viewer == null || viewer == 'undefined')
		{
			// ani : app not installed //
			var aniHtmlArray = new Array();
			/* show image from url */
			//aniHtmlArray.push('<div style="clear:both;"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/google_adsense.html" width="750px"></iframe></div>');
			aniHtmlArray.push('<div style="clear:both;"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/mb_ad_top.html" width="750px"></iframe></div>');
			aniHtmlArray.push('</center><br/><br/><br/>');
			aniHtmlArray.push('<a href="javascript:redirectUrl()"><img src='+image_url+'></a>');
			aniHtmlArray.push('<br /><br /><div><a href="javascript:redirectUrl()">');
			aniHtmlArray.push('<h1 style="font-size:15px;color:#2B5696;font-weight:bold;">Send a SuperScrap now!</h1></a><br/></div><br /><br />');
			/*end*/
			aniHtmlArray.push('<center><br/><div style="font-size:14px;width:700px;height:20px;');
			aniHtmlArray.push(' color:#2B5696;font-weight:bold;">');
			aniHtmlArray.push('OMG! You haven\'t added Superscrap application yet. Add now. You will love it...</div><br/>');
			aniHtmlArray.push('<center><img src="'+domain+area+'/orkut/images/ss_orkut_profile.jpg" /></center>');
			aniHtmlArray.push('<center><img src="'+domain+area+'/orkut/images/ss_logo.gif" /></center>');
			aniHtmlArray.push('<br/><div><a href="'+containerUrl+'AppInfo.aspx?appId='+appId+'"');
			aniHtmlArray.push(' target="_parent" style="font-size:15px;color:#2B5696;font-weight:bold;">Add now!</a><br/>');
			aniHtmlArray.push('</center>');

			document.getElementById('main').innerHTML= aniHtmlArray.join("");
		}
		else{
			viewerId = getOrkutId(viewer);
			ownerId = getOrkutId(owner);
			owner_name = owner.getDisplayName();
			if(viewerId != ownerId)
			{
				friend_name = owner_name;
				fetchOwnerReceivedData(ownerId);
			}else if(viewerId == ownerId)
				{
				orkut_opensocial_user_id = gadgets.util.getUrlParameters()["gadgetOwner"];//to generate user's open social id
				gadgets.io.makeRequest(domain+area+'/orkut/checkUserStatus.jsp?owner='+ownerId+'&appId='+appId+'&orkut_opensocial_user_id='+orkut_opensocial_user_id+'&ver='+noCacheStr, postCheckUserStatus,null);
				activeTab ='create';
				viewer_friends = data.get('ownerFriends').getData();
//				viewer_friends_length = 1000;
				viewer_name = viewer.getDisplayName();
				
			
				var htmlArray = new Array();
				htmlArray.push('<style type=\"text/css\">a {color:#02679C; text-decoration:none;} a:visited {color:#02679C;}  a:hover{color:#00344F;text-decoration:underline;} body{overflow-y:hidden;}</style>');
				//htmlArray.push('<div style="clear:both;"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/google_adsense.html" width="750px"></iframe></div>');
				htmlArray.push('<div style="clear:both;"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/mb_ad_top.html" width="750px"></iframe></div>');
				htmlArray.push('<div id="id_body"><form name="ss_form" id="id_ss_form" action="" method="post">');
				htmlArray.push('<div style="color:#8C8C8C; position:absolute;margin-left:520px;">');
				htmlArray.push('<img src="'+domain+area+'/orkut/images/ss_fav_icon.png"> Superscrap created by ');
				htmlArray.push('<span><a href="http://www.experiencecommerce.com" target="_blank" style="color:#DF353B; font-weight:bold;">Experience Commerce</a></span>');
				htmlArray.push('</div>');

				// tabs section //
				htmlArray.push('<ul class="intabs" style="margin-left:5px;margin-top:10px;">');
				htmlArray.push('<li class="sel" id="create"><span class="ltab"></span>');
				htmlArray.push('<a href="javascript: void(0);" onClick="switchTab(\'create\'), this.blur();">Create</a>');
				htmlArray.push('</li>');
				htmlArray.push('<li class="" id="sent"><span class="ltab"></span>');
				htmlArray.push('<a href="javascript: void(0);" onClick="switchTab(\'sent\'), this.blur();">Sent</a>');
				htmlArray.push('</li>');
				htmlArray.push('<li class="" id="received"><span class="ltab"></span>');
				htmlArray.push('<a href="javascript: void(0);" onClick="switchTab(\'received\'), this.blur();">Received</a>');
				htmlArray.push('</li>');
				htmlArray.push('</ul>');				
				// tabs section //

				// create section //
				htmlArray.push('<div id="create_sec">');
				htmlArray.push('<table><tr><td><div id="blank_textarea" style="position: absolute; left:270px; top:190px; z-index: 1001; height:200px; width:300px; overflow-y:hide; "><div name="id_hindimessage" id="id_hindimessage" style="position: absolute; left:0px; top:0px; z-index: 1001; font-size: 16px; font-weight: bold; height:200px; width:200px; overflow-y:hide; ">  </div><br /></div></td></tr></div>');
				htmlArray.push('<div style="float:left;width:835px;">');
				htmlArray.push('<table class="ss_table" style="height:500px;" border="0" cellpadding="0" cellspacing="0" width="100%">');
				htmlArray.push('<tbody>'); 
				htmlArray.push('<tr style="height:3px;">');
				htmlArray.push('<td></td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr>');
				htmlArray.push('<td id="id_templ_tabs" class="tmpl_tabs" >'); 
				htmlArray.push('</td>'); 
				htmlArray.push('</tr>');
				htmlArray.push('<tr>'); 
				htmlArray.push('<td id="id_templ_container"  style="margin-left:50px;" class="tmpl_sel">');
				htmlArray.push('</td>'); 
				htmlArray.push('</tr>'); 
				fetchTemplates('xmas','1');
				htmlArray.push('<tr>');
				htmlArray.push('<td id="id_temp2_tabs" class="tmpl_tabs" ><div style="background-image:url(\''+domain+area+'/orkut/images/ss_noti.gif\'); background-repeat:repeat-x; height:16px; margin:0 16px 8px 6px; padding: 4px 4px 4px 8px; font-family:verdana; font-size:12px; font-weight:bold; width:97%; color:#323232;">Enter your message</div>'); 
				htmlArray.push('</td>'); 
				htmlArray.push('</tr>'); 
				//remove 
				htmlArray.push('<tr><td><div id="hindiText" style="display:none"></div></td></tr>');
				//remove
				htmlArray.push('<tr>');
				htmlArray.push('<td id="id_temp3_tabs" class="tmpl_tabs" ><div style="background-image:url(\''+domain+area+'/orkut/images/ss_noti.gif\'); background-repeat:repeat-x; height:16px; margin:0 16px 8px 6px; padding: 4px 4px 4px 8px; font-family:verdana; font-size:12px; font-weight:bold; width:97%; color:#323232;">Copy and Paste this Superscrap Code to friends scrapbook or send it to them directly !!!</div>'); 

				htmlArray.push('</td>'); 
				htmlArray.push('</tr>'); 
				htmlArray.push('<tr id="id_canvas" style="display:none;">');
				htmlArray.push('<td align="center" class="canvas"><div style="height: 250px; width: 350px;"><img id="id_canvas_img" src="'+domain+area+'/orkut/images/loader.gif"></div>');
				htmlArray.push('</td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr id="id_embed" style="display: none;">');
				htmlArray.push('<td valign="top" align="center"> <div align="center" style="clear:both;  padding:0 0 10px 0;"><span style="color: rgb(77, 77, 77); font-size:13px;">You can copy & paste this code in your friend\'s scrapbook: &nbsp;&nbsp; </span><input id="id_embdCode" type="text" onclick="this.select();" style= "width: 339px; font-size: 10px;"></div>');
				htmlArray.push('</td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr id="id_frnz" style="display: none;">');
				htmlArray.push('<td align="center">');
				
				htmlArray.push('<strong style="font-size:15px;">OR</strong><br/><br/><a href="javascript:toggle(\'Link1\');"><div style="font-family:Verdana;font-size:12px;"><img src="http://apps.postalz.com/'+area+'/orkut/images/ss_open_friendslist.gif" border="0" style="margin:0 0 -10px 0;"></a>&nbsp;&nbsp;to open friend\'s list and send superscrap to them directly.</div>');
				htmlArray.push('<div id="Link1" style="display:none;padding:15px 0 0 0;"><div style="font-family: Verdana; font-size: 12px;"><div style="margin-left:50px;"><span style="float:left;"><a href="javascript:selectAllFriends();">Select All</a>&nbsp;|&nbsp;<a href="javascript:selectNoneFriends();">Clear All</a></span></div><div style="margin: 20px 55px 10px 0;" align="right"> <span><a href="javascript:done(\'t\');"><img src="http://apps.postalz.com/'+area+'/orkut/images/ss_send.gif" border="0" height="28" width="123"></a></span> &nbsp; &nbsp; <span><a href="javascript:clearAllAndGoHome();"><img src="http://apps.postalz.com/'+area+'/orkut/images/ss_cancel.gif" border="0" height="28" width="123"></a></span> </div></div><div style="clear:both;"></div>');

				htmlArray.push('<div class="st_main" style="border:none;">'); 
				htmlArray.push('<div class="st_friends" id="id_st_friends">');	
				viewer_friends.each(function(person) 
				{ 
					friends_count+=1; 
					friend_name = person.getDisplayName(); 
					var fr_profile_url = '';
					fr_profile_url = person.getField(opensocial.Person.Field.PROFILE_URL);
					var fr_regex = /uid=([^&#]+)/;
					var fr_result = fr_profile_url.match(fr_regex);
					if (fr_result.length == 2)
					{
						friend_id = fr_result[1];
					}
					htmlArray.push('<div><input name="friendsCheckBox" type="checkbox" value="'+friend_id+'"><label id="'+friend_id+'"> '+friend_name.substring(0,17)+'</label></div>');
				});
				htmlArray.push('<div style="clear:both;"></div>');
				htmlArray.push('</div></div><!-- st_friendsDiv ends here --></div><!-- st_main div ends here -->');
				htmlArray.push('<br/></td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr id="navigation_div">');
				htmlArray.push('<td>');
				htmlArray.push('<span id="prev" style="display:none;"><a href="javascript:void(0);" onclick="getPrevSetOfFriends()">prev</a></div>');
				htmlArray.push('<span id="next" style="display:none;"><a href="javascript:void(0);" onclick="getNextSetOfFriends()">next</a></div>');
				htmlArray.push('</td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr id="id_status" style="display: none;">');
				htmlArray.push('<td valign="top" align="center"> <div style="padding: 0 10px 10px; margin:0; color: rgb(42, 92, 165); font-size: 14px;" align="center">Your SuperScrap has been sent Successfully!</div><br><div id="id_sendAnother" style="display: none;"><div style="padding: 10px; color: rgb(42, 92, 165); font-size: 14px;" align="center"><a href="javascript:clearAllAndGoHome()"><img src="http://apps.postalz.com/'+area+'/orkut/images/ss_send_another.gif" border="0" alt="send another" height="28" width="123"></a></div></div>');
				htmlArray.push('<div align="center" style="clear:both;"></div><div id="id_sendAnother" style="display: none;"><div style="padding: 10px; color: rgb(42, 92, 165); font-size: 14px;" align="center"><a href="javascript:clearAllAndGoHome()"><img src="http://apps.postalz.com/'+area+'/orkut/images/ss_send_another.gif" border="0" alt="send another" height="28" width="123"></a></div></div></td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr >');
				htmlArray.push('<td></td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr id="id_editor" style="display: none;">');
				htmlArray.push('<td class="writing_panel">');
				htmlArray.push('<div>');
				htmlArray.push('<div align="center" style="margin: 0pt 20px 0pt 0pt; padding: 60px 5px 0pt 99px; float: left;"><span id="anotherStyleDiv"><a href="javascript:showStyles()"><img  src="'+domain+area+'/orkut/images/ss_back.gif" id="id_canvas_img" border="0" alt="select another" height="28" width="123"></a></span></div>');
				htmlArray.push('<div id="englishBox" style="margin: 0pt 0pt 50px -15px; padding: 20px; background: rgb(230, 200, 192) url(http://apps.postalz.com/'+area+'/orkut/images/english-bg.gif) no-repeat scroll 20px 14px; float: left; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; width: 330px; height: 215px;">');
				htmlArray.push('<select id="langId" name="langId" style="margin:-1px 0 0 105px;width:110px;font-size:9px;" onChange="doLangChanged();"> ');
				htmlArray.push('<option value="en">ENGLISH</option>');
				htmlArray.push('<option value="hi">HINDI</option>');
				htmlArray.push('</select>');
				htmlArray.push('<textarea name="textarea" style="margin: 20px 0pt 0px 0; width: 330px; height: 80px; font-size: 16px; color: rgb(102, 102, 102); font-weight: bold;" id="id_engmessage" onclick="clearText();" onKeyDown="textCounter(this,\'progressbar1\',120);" onKeyUp="textCounter(this,\'progressbar1\',120);getWords();" onFocus="textCounter(this,\'progressbar1\',120);getWords();">  </textarea><br />');
				htmlArray.push('<img id="hindiImg" src="http://apps.postalz.com/'+area+'/orkut/images/explain-img1.jpg" border="0" style="margin:20px 0 0 0;">');  
				htmlArray.push('</div>');
			
				htmlArray.push('</div>');

				htmlArray.push('<div style="margin-top: 5px; float:left;padding:60px 0 0 10px;"><span> <a href="javascript:done(\'n\');"> <img src="http://apps.postalz.com/'+area+'/orkut/images/ss_next.gif" border="0" alt="select_friends" height="28" width="123"></a> </span> </div>');

				//htmlArray.push('<br>');

				htmlArray.push('</div>');
				htmlArray.push('<div id="progressbarcontainer" style="visibility:hidden;border:solid #cdcdcd 1px;margin-left: 250px; width: 330px;clear:both;" class="iefix"><div id="progressbar1" class="progress" style="align:left;margin-left:0px;"></div></div>');

				htmlArray.push('</td>');
				htmlArray.push('</tr>');
				htmlArray.push('<tr>');
				//htmlArray.push('<td align="center"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/google_adsense2.html" width="750px"></iframe><br/><br/>');
				htmlArray.push('<td align="center"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/mb_ad.html" width="750px"></iframe><br/><br/>');
				htmlArray.push('</td>');
				htmlArray.push('</tr>');
				htmlArray.push('</tbody>');
				htmlArray.push('</table>');
				htmlArray.push('</div>');
				htmlArray.push('</div>');
				htmlArray.push('<div id="sent_sec" style="display:none;height:1250px;overflow:scroll;"></div>');
				htmlArray.push('<div id="received_sec" style="display:none;height:1250px;overflow:scroll;"></div>');
				htmlArray.push('<div id="invite_sec" class="ss_table" style="display:none;height:1250px;overflow:scroll;"></div>');
				htmlArray.push('</form>');
				htmlArray.push('</div>');
				html = htmlArray.join('');
				document.getElementById("subMain").innerHTML = html;
				switchTab(activeTab);

			}
		}//else ends for viewer who has installed app...
}//response ends here..


function doLangChanged() {
	document.getElementById("englishBox").value = "";
	if (document.getElementById("langId").value == "en")
	{
		document.getElementById("englishBox").style.backgroundColor = '#e6c8c0';
		document.getElementById("englishBox").style.backgroundImage = 'http://apps.postalz.com/'+area+'/orkut/images/english-bg.gif';
		document.getElementById("hindiImg").src="http://apps.postalz.com/"+area+"/orkut/images/explain-img1.jpg";

	} else if (document.getElementById("langId").value == "hi")
	{
		document.getElementById("englishBox").style.backgroundColor = '#b5ecc4';
		document.getElementById("englishBox").style.backgroundImage = 'http://apps.postalz.com/'+area+'/orkut/images/hindi-bg.gif';
		document.getElementById("hindiImg").src="http://apps.postalz.com/"+area+"/orkut/images/explain-img.gif";
	}


}

/**************** character translation script ******************/
var iStr ='';
	function getTranslatedStr(e) {
		
		
		if (document.getElementById("langId").checked)
		{
		
			var keynum;
			var keychar;
			var numcheck;
			
			if(window.event) // IE
			{
				keynum = e.keyCode;
			}
			else if(e.which) // Netscape/Firefox/Opera
			{
				keynum = e.which;
			}
			keychar = String.fromCharCode(keynum);
			// if space or !( A-Z or a-z or 0-9)
			// if space or any special character

			//if ((keynum >= 65 && keynum <= 90) || (keynum >=48 && keynum <= 57))
			if (keynum == 32)
			{
				var queryString = '';
				var opt_params = {};
				opt_params[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;
				opt_params[gadgets.io.RequestParameters.METHOD]=gadgets.io.MethodType.POST;
				opt_params[gadgets.io.RequestParameters.CONTENT_TYPE]=gadgets.io.ContentType.JSON;
				var d = new Date();
				var noCacheStr = null;
				var str = document.getElementById("id_engmessage").value;
				if (str != "")
				{
		
					var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
					var left_trimmedStr = str.replace(initial_whitespace_rExp, "");
					var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
					var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");

					iStr = cleanedStr.replace(/[ ]+/g, ",");
					//http://www.google.com/transliterate/indic?tlqt=1&tl_app=3&langpair=en|hi&text=wahan%2Cse%2Caana%2Chai
					var querystring= 'tlqt=1&tl_app=3&langpair=en|hi&text='+iStr+'&ver='+noCacheStr;
					opt_params[gadgets.io.RequestParameters.POST_DATA]=querystring;
					gadgets.io.makeRequest('http://www.google.com/transliterate/indic',getTStr,opt_params);
				}
			}
		}
	}
  
    function getWords() {
		print_many_words();
		document.getElementById("id_hindimessage").style.color = color;
    }

    function getTStr(response){
		//alert(response.data);
		var noOfWords = response.data;
		
		var fullStr = document.getElementById("id_engmessage").value;
		var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
		var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
		var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
		var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
		var splitString = fullStr.split(" ");
		//alert (splitString);
		var word_count = splitString.length -1;
		if (fullStr.length <2) {
			word_count = 0;
		}
		
		var str;
		var puriString ='';
		var i=0;
		for (i=0; i<word_count; i++)
		{

			if (response.data != null && response.data != "undefined")
			{
				if (response.data[i] != null && response.data[i] != "undefined")
				{
					str = response.data[i].hws[0];
					puriString = puriString + ' ' + str;
				}
			}
			
		}
		
		document.getElementById("hindiText").innerHTML=puriString;
		document.getElementById("id_hindimessage").value=document.getElementById("hindiText").innerHTML;
		document.getElementById("id_hindimessage").style.color = color;
	}


	function textCounter(field,counter,maxlimit,linecounter,e) {
		document.getElementById("progressbarcontainer").style.visibility = '';
		imageCreated = false;
		// text width//
		var fieldWidth =  parseInt(field.offsetWidth);
		var charcnt = field.value.length;

		if(charcnt == 0){
			hidecolor();
		}else{
			showcolor();
		}	

		// trim the extra text
		if (charcnt > maxlimit) { 
			field.value = field.value.substring(0, maxlimit);
		}
		else { 
		// progress bar percentage
		var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
		document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
		// color correction on style from CCFFF -> CC0000
		setcolor(document.getElementById(counter),percentage,"background-color");
		}
	}

	function setcolor(obj,percentage,prop){
		obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
	}

	function hidecolor(){
		document.getElementById("progressbar1").style.backgroundColor = '#ffffff';
	}
	function showcolor(){
		document.getElementById("progressbar1").style.backgroundColor = '#ADCBFF';
	}
	function done(flag){
		
		var msg = '';
		var id_radio = '';
		var lId = 'en';
		var hm = '';

		message = document.getElementById("id_engmessage").value;
		//alert("langId:" + document.getElementById("langId").value);
		if (document.getElementById("langId").value == "hi") {
			lId = "hi";
			hm = document.getElementById("id_hindimessage").innerHTML;
		} else if (document.getElementById("langId").value == "en") {
			lId = "en";
			hm = document.getElementById("id_engmessage").value;
		}  
		
		if(message == ''){
			return;
		}
		message= escape(message);
		var queryString = '';
		var opt_params = {};
		opt_params[gadgets.io.RequestParameters.AUTHORIZATION]=gadgets.io.AuthorizationType.SIGNED;
		opt_params[gadgets.io.RequestParameters.METHOD]=gadgets.io.MethodType.POST;
		opt_params[gadgets.io.RequestParameters.CONTENT_TYPE]=gadgets.io.ContentType.TEXT;
		var preview = 'f';
		var composeNotSent = 'n';
		var composedAndSendToFriends = 't';

		var d = new Date();
		var noCacheStr = null;

		if(flag == composedAndSendToFriends){
			viewer_name= escape(viewer_name);
			noCacheStr = d.getTime(); // this is to fool the browser
			document.getElementById("id_editor").style.display = 'none';
			document.getElementById("blank_textarea").style.display = 'none';
			document.getElementById("id_status").style.display = '';
			document.getElementById("id_temp2_tabs").style.display = 'none';
			document.getElementById("id_temp3_tabs").style.display = 'none';
			
			document.getElementById("id_engmessage").value= '';
			document.getElementById("id_frnz").style.display = 'none';
			document.getElementById("id_embed").style.display = 'none';
			document.getElementById("id_sendAnother").style.display = '';
			msg = ' has superscrapped someone special';
			friendListStr = createFriendList();
			friendNamesStr = createFriendNames();
			var hmNCR = hm.toCharRef();
		    //alert('frnzList=' + friendListStr + '&frnzNames=' + friendNamesStr);
			var querystring1 = 'frnzList=' + friendListStr + '&frnzNames=' + friendNamesStr + '&langId='+ lId + '&hm=' + replaceSpChars(hmNCR) + '&m='+message+'&t='+templ_name+'&uId='+viewerId+'&n='+viewer_name+'&d='+flag+'&c='+color+'&image_domain='+image_domain+'&ver='+noCacheStr;

			opt_params[gadgets.io.RequestParameters.POST_DATA]=querystring1;
			gadgets.io.makeRequest(domain+area+'/orkut/create_ss.jsp',getImgUrl,opt_params);


			createActivity(msg);
		}else if(flag == preview){

			noCacheStr = d.getTime(); // this is to fool the browser
			document.getElementById("id_canvas").style.display = '';
			document.getElementById("id_editor").style.display = '';
			document.getElementById("blank_textarea").style.display = '';
			document.getElementById("id_sendAnother").style.display = '';
			document.getElementById("id_status").style.display = 'none';
			document.getElementById("id_temp3_tabs").style.display = 'none';
			document.getElementById("id_frnz").style.display = 'none';
			document.getElementById("id_embed").style.display = '';

			//if(!imageCreated && (message.length > 0)){
				document.getElementById("id_canvas_img").src= domain+area+'/orkut/images/loader.gif';
				var hmNCR = hm.toCharRef();
				var querystring1 = 'langId='+ lId + '&hm=' + replaceSpChars(hmNCR) + '&m='+message+'&t='+templ_name+'&uId='+viewerId+'&d='+flag+'&c='+color+'&image_domain='+image_domain+'&ver='+noCacheStr;
				opt_params[gadgets.io.RequestParameters.POST_DATA]=querystring1;
				gadgets.io.makeRequest(domain+area+'/orkut/create_ss.jsp', reloadImg,opt_params);
			//}
		}if(flag == composeNotSent){
			noCacheStr = d.getTime(); // this is to fool the browser
			document.getElementById("id_editor").style.display = 'none';
			document.getElementById("blank_textarea").style.display = 'none';
			document.getElementById("id_status").style.display = 'none';
			document.getElementById("id_temp3_tabs").style.display = 'none';
			//if(!imageCreated && (message.length > 0)){
				document.getElementById("id_canvas_img").src= domain+area+'/orkut/images/loader.gif';
				var hmNCR = hm.toCharRef();
				var querystring1 = 'langId='+ lId + '&hm=' + replaceSpChars(hmNCR) + '&m='+message+'&t='+templ_name+'&uId='+viewerId+'&d='+flag+'&c='+color+'&image_domain='+image_domain+'&ver='+noCacheStr;
				opt_params[gadgets.io.RequestParameters.POST_DATA]=querystring1;
				gadgets.io.makeRequest(domain+area+'/orkut/create_ss.jsp', reloadImg,opt_params);
			//}
			document.getElementById("id_frnz").style.display = '';
			document.getElementById("id_embed").style.display = '';
			document.getElementById("id_temp3_tabs").style.display = '';
			document.getElementById("id_temp2_tabs").style.display = 'none';
			document.getElementById("id_sendAnother").style.display = 'none';

			getAllFriends();
		}
	}

	function replaceSpChars (istr) {	
		var ostr = istr.replace(/[#]+/g,"x");
		ostr=ostr.replace(/[&]+/g,"y");
		ostr=ostr.replace(/[;]+/g,"z");
		return ostr;
	}

var imgName = '';
var imgUrl = '';

	function reloadImg(responseText){
		imageCreated = true;
		var d = new Date();

		var noCache = d.getTime(); // this is to fool the browser
		//imgName = domain+'opensocial/superscrap/'+trimString(responseText.text)+'?ver='+noCache;
		//alert(responseText.text);
		imgName = trimString(responseText.text)+'?ver='+noCache;
		imgUrl = trimString(responseText.text);
		//alert(imgName);
		setTimeout(setImg,2000);
	}
	function setImg(){
		document.getElementById("id_canvas_img").src= imgName;	
		document.getElementById("id_embdCode").value='<div style="width: 350px; height: 309px;"><a href="'+containerUrl+'Application.aspx?appId='+appId +'"><img border="0" style="padding: 0pt 0pt 10px;" src="'+imgName+'"/></a><a href="'+containerUrl+'Application.aspx?appId='+appId+'" style="float: right;">Send a Superscrap</a></div>';
	}
	function getImgUrl(responseText){
		imageCreated = true;
		var d = new Date();
		var noCache = d.getTime();
		imgName = domain+'opensocial/superscrap/'+trimString(responseText.text)+'?ver='+noCache;
		document.getElementById("id_embdCode").value='<div style="width: 350px; height: 309px;"><a href="'+containerUrl+'Application.aspx?appId='+appId+'"><img border="0" style="padding: 0pt 0pt 10px;" src="'+imgName+'"/></a><a href="'+containerUrl+'Application.aspx?appId='+appId+'" style="float: right;">Send a Superscrap</a></div>';
		
	}

	function getTemplateNo(templateName,templateNumber){
		document.getElementById("id_canvas_img").src= domain+area+'/orkut/images/loader.gif';
		var w_templ = 'id_radio_template_'+templateName;
		var templ_color = 'id_colorValue_'+templateName+"_"+templateNumber;
		color = document.getElementById(templ_color).value;
		templ_name = templateName;
		document.getElementById(w_templ).checked = true;
		document.getElementById("id_canvas_img").src = domain+area+'/orkut/images/templates/'+templ_name;
		showEditor();
	}

	
	var longitud = 0;
	var enterCount = 0;
	var enterFlag = false;
	var xx = 0;
	function checkCharCount(frmTextArea,frmSpan,textNote,totalCharacters,e) {
	  imageCreated = false;
	  var frmTextAreaObj=document.getElementById(frmTextArea);
	  var frmSpanObj=document.getElementById(frmSpan);
	  
	  if(e.keyCode == 13){
		  enterFlag = true;
		  enterCount = enterCount + 1;
		  longitud = totalCharacters - frmTextAreaObj.value.length - (20*enterCount);
	  }else{
		  if(enterFlag){
			  longitud = longitud - 1;
		  }else{
		  	  longitud = totalCharacters - frmTextAreaObj.value.length;
		  }
	  }
	  if(frmTextAreaObj.value.length == 0){
		  enterCount = 0;
		  longitud = 120;
		  enterFlag = false;
  	  }
	  if(longitud > 0){
		xx = frmTextAreaObj.value.length + (enterCount*20);
  	  }
	  if(longitud <= 0) {
		longitud=0;
		textNote='<span class="disable"> '+textNote+' </span>';
		frmTextAreaObj.value=frmTextAreaObj.value.substr(0,xx);
	  }
	  frmSpanObj.innerHTML = textNote.replace("{CHAR}",longitud);
	}

	function postActivity(text,body) {
	  var params = {};
	  params[opensocial.Activity.Field.TITLE] = text;
	  params[opensocial.Activity.Field.BODY] = body;
	  var activity = opensocial.newActivity(params);
	  opensocial.requestCreateActivity(activity, opensocial.CreateActivityPriority.HIGH, function() {});
	}        

	function createActivity(msg) {  
		var title = msg;
		var a=friendListStr.split(',');
		var recipient_id=a[0];
		var img_src='image';
		var body = '<table><tr><td><a href="http://www.orkut.co.in/Main#Application.aspx?uid='+ownerId+'&appId='+appId+'&appParams=%7B%20%22'+img_src+'%22%20%3A%20%22'+imgUrl+'%22%20%7D"><img src="'+imgName+'" height=75 width=103/></a></td><td><a href="http://www.orkut.co.in/Main#Application.aspx?uid='+ownerId+'&appId='+appId+'&appParams=%7B%20%22'+img_src+'%22%20%3A%20%22'+imgUrl+'%22%20%7D">Why scrap when you can superscrap?</a></td>';		
		postActivity(title, body); 
	}

	function createFriendList(){
		allFriends = ss_form.friendsCheckBox;
		var check=false;
		friendListStr = new Array();

		if((allFriends != null) && (allFriends.length == null)){
				if(allFriends.checked == false){
					allFriends.checked = true;
				}
			friendListStr.push(ss_form.friendsCheckBox.value);
		}else{
			for (i = 0; i < allFriends.length; i++){
				if(ss_form.friendsCheckBox[i].checked){
						check = true;
						if(i != allFriends.length-1){
							friendListStr.push(ss_form.friendsCheckBox[i].value+",");
						}else{
							friendListStr.push(ss_form.friendsCheckBox[i].value);
						}
					}//if
			}//for
		}
		//alert("friendListStr:" + friendListStr);
		return friendListStr.join('');
	}

	function createFriendNames(){
		allFriends = ss_form.friendsCheckBox;
		var check=false;
		friendNamesStr = new Array();
		var tmp_name_holder =''; 
		if((allFriends != null) && (allFriends.length == null)){
				if(allFriends.checked == false){
					allFriends.checked = true;
				}
			friendNamesStr.push(document.getElementById(ss_form.friendsCheckBox.value).innerHTML);
		}else{
			for (i = 0; i < allFriends.length; i++){
				if(ss_form.friendsCheckBox[i].checked){
						check = true;
						tmp_name_holder = document.getElementById(ss_form.friendsCheckBox[i].value).innerHTML;
						if(i != allFriends.length-1){
						 friendNamesStr.push(tmp_name_holder+",");
						}else{
						 friendNamesStr.push(tmp_name_holder);
						}
					}//if
			}//for
		}
		return friendNamesStr.join('');
	}
	function removeBg(){
		document.getElementById("id_canvas_img").className="removeBgX";
	}

	function fetchOwnerData(Id){
		var d = new Date();
		var noCacheStr = d.getTime(); // this is to fool the browser
		var opt_params = {
				'authenticationType':'signed',
                'methodType':'post',
                'contentType':'html',
                'postData':''
            };
		gadgets.io.makeRequest(domain+area+'/orkut/getCounts.jsp?owner='+Id+'&ver='+noCacheStr,doNothing,opt_params);
	}

	function doNothing(countDetails){
		countDetails = trimString(countDetails.text);
		if(friendOnlyFlag == true){
			received = countDetails.substring(countDetails.indexOf('r=')+2,countDetails.indexOf('#s='));
			var setCountDiv = document.getElementById("id_recvdCount");
			if(null != setCountDiv){
				setCountDiv.innerHTML = received;
				if(received == "1"){
					document.getElementById("recvd_panel").innerHTML = 'superscrap';
 				}
			}
			friendOnlyFlag = false;
			if(received == "0"){
				document.getElementById("id_more").innerHTML = '';
				document.getElementById("id_view").innerHTML = '';
			}
		}else{
			document.getElementById("id_recvdCount").innerHTML = countDetails.substring(countDetails.indexOf('r=')+2,countDetails.indexOf('#s='));
			received = countDetails.substring(countDetails.indexOf('r=')+2,countDetails.indexOf('#s='));
		}
	}

	function setInboxData(inboxDetails){
		inboxDetails = trimString(inboxDetails.text);
		inboxDetails  = inboxDetails.replace("#pg#"," ");
		var inBoxDetailsStr = new Array();
		inBoxDetailsStr.push('<div style="border:1px solid #cfcfcf; background-color:#f5f5f5; margin:0 auto;">');
		inBoxDetailsStr.push(inboxDetails);
		inBoxDetailsStr.push('</div>');
		document.getElementById('received_sec').innerHTML = inBoxDetailsStr.join('');
	}

	function setOutboxData(outboxDetails){
		outboxDetails = trimString(outboxDetails.text);
		outboxDetails = outboxDetails.replace("#pg#","%");
		outboxDetails = outboxDetails.replace("%2C",",");
		outboxDetails = unescape(outboxDetails);
		var outBoxDetailsStr = new Array();
		outBoxDetailsStr.push('<div style="border:1px solid #cfcfcf;background-color:#f5f5f5;margin:0 auto;">');
		outBoxDetailsStr.push(outboxDetails);
		outBoxDetailsStr.push('</div>');
		document.getElementById('sent_sec').innerHTML = outBoxDetailsStr.join('');
	}
	
	function showProfileData(profileData){
		profileData = trimString(profileData.text);
		document.getElementById('id_profileData').innerHTML = profileData;
		document.getElementById('main').className = 'scroller';
		if(document.getElementById("id_height").value == '0');{
		//	document.getElementById("id_height").value = 130;
		}
		gadgets.window.adjustHeight(document.getElementById("id_height").value);
	}

	function fetchOwnerReceivedData(id){
		friendId = id;
		friendOnlyFlag = true;
		fetchOwnerData(id);		
		setTimeout(fetchInbox,5000);
	}
	
	function fetchInbox(){
		var opt_params = {
				'authenticationType':'signed',
                'methodType':'post',
                'contentType':'html',
                'postData':''
            };
		gadgets.io.makeRequest(domain+area+'/orkut/getInboxDetails.jsp?owner='+friendId+'&limit='+received+'&friendview=t',setOwnerReceivedData,opt_params);
	}	

	function setOwnerReceivedData(receivedData){
		receivedData = trimString(receivedData.text);
		receivedData = receivedData.replace("#pg#"," ");
		var receivedDatatmlArr = new Array();		
		receivedDataHtmlArr.push('<div style="clear:both;"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/google_adsense.html" width="750px"></iframe></div>');
		//receivedDataHtmlArr.push('<div style="clear:both;"><iframe id="advt" FRAMEBORDER=0 scrolling="no" style="border:0px;" height="100px" src ="http://apps.postalz.com/superscrapopensocial/orkut/js/mb_ad.html" width="750px"></iframe></div>');
		receivedDataHtmlArr.push('</center><br/><br/><br/>');
		receivedDataHtmlArr.push('<a href="http://www.orkut.co.in/Main#Application.aspx?appId='+appId+'" target="_blank"><img src='+image_url+'></a>');
		receivedDataHtmlArr.push('<br /><br /><div><a href="http://www.orkut.co.in/Main#Application.aspx?appId='+appId+'" target="_blank">');
		receivedDataHtmlArr.push('<h1 style="font-size:15px;color:#2B5696;font-weight:bold;">Send a SuperScrap now!</h1></a><br/></div>');
		/********************************************end**********************************************/
		document.getElementById('main').innerHTML = receivedDataHtmlArr.join('');
	}



	function createFriendProfileLinks(){
		var friendLinksArr = new Array();
		var friendListStrArr = friendListStr.split(",");
		var friendNamesStrArr = friendNamesStr.split(",");
		for(var i =0;i<friendListStrArr.length;i++){
			friendLinksArr.push('<a href="'+containerUrl+'Profile.aspx?uid=');
			//alert("JJJ:" + friendListStrArr[i]);
			friendLinksArr.push(friendListStrArr[i]);
			friendLinksArr.push('" target="_parent" />');
			friendLinksArr.push(friendNamesStrArr[i]);
			if(i != friendListStrArr.length-1){
				friendLinksArr.push("</a>"+", ");
			}else{
				friendLinksArr.push("</a>");
			}
		}
		return friendLinksArr.join('');
	}

	function gotoCanvas(navigateTo) {
		var tab_param = {
			tab : navigateTo
		}
		var canvas_view = new gadgets.views.View("canvas");
		gadgets.views.requestNavigateTo(canvas_view, tab_param);
	}

	function gotoCanvasWithName(tab_name,withName) {
		window.parent.location = containerUrl+'Application.aspx?appId='+appId+'&uid='+getOrkutId(viewer)+'&name='+withName;
		var tab_param = {
			tab : tab_name,
			name : withName
		}
	}

	function showEditor(){
		if(templ_name == ''){
			templ_name = 'scr/bt_0.png';
			document.getElementById("id_canvas_img").src= domain+area+'/orkut/images/templates/scr/bt_0.png';
		}else{
			document.getElementById("id_canvas_img").src= domain+area+'/orkut/images/templates/'+templ_name;
		}
		if(document.getElementById("id_engmessage").value != ""){
			done('f');
		}
		document.getElementById("id_status").style.display = 'none';
		document.getElementById("id_temp3_tabs").style.display = 'none';
		document.getElementById("id_templ_container").style.display = 'none';
		document.getElementById("id_templ_tabs").style.display = 'none';
		document.getElementById("id_temp2_tabs").style.display = '';
		document.getElementById("id_canvas").style.display = '';
		document.getElementById("id_editor").style.display = '';
		document.getElementById("blank_textarea").style.display = '';
		document.getElementById("id_hindimessage").innerHTML = "";
		document.getElementById("id_engmessage").value = "";
		document.getElementById("id_frnz").style.display = 'none';
		document.getElementById("id_embed").style.display = 'none';
	}
	
	function showStyles(){
		document.getElementById("id_status").style.display = 'none';
		document.getElementById("id_temp3_tabs").style.display = 'none';
		document.getElementById("id_sendAnother").style.display = "none";
		document.getElementById("id_canvas_img").src= domain+area+'/orkut/images/loader.gif';
		document.getElementById("id_canvas_img").className= 'loader';
		document.getElementById("id_canvas").style.display = 'none';
		document.getElementById("id_editor").style.display = 'none';
		document.getElementById("blank_textarea").style.display = 'none';
		document.getElementById("id_hindimessage").innerHTML = "";
		document.getElementById("id_engmessage").value = "";
		document.getElementById("id_frnz").style.display = 'none';
		document.getElementById("id_embed").style.display = 'none';
		document.getElementById("id_templ_container").style.display = '';
		document.getElementById("id_templ_tabs").style.display = '';
		document.getElementById("id_temp2_tabs").style.display = 'none';
		document.getElementById("anotherStyleDiv").style.display = '';
	}

	function clearAllAndGoHome(){
		// clear text Area
		document.getElementById("id_canvas_img").src= domain+area+'/orkut/images/loader.gif';		
		document.getElementById("id_engmessage").value = "";
		document.getElementById("id_sendAnother").style.display = "";
//		document.getElementById("frmSpan").innerHTML = '(120 characters)';
		// showStyles
		showStyles();
	}

	function getAllFriends()
	{
		document.getElementById('id_st_friends').innerHTML='<div align="center" class="ss_table" style="width:680px;height:185px;"><center><img src="'+domain+area+'/orkut/images/loader.gif" alt="loading" title="loading"/></div>';
		var req = opensocial.newDataRequest();
		var params = {};
		friendCurrPage = friendCurrPage +1;
		var set = 20*friendCurrPage;

		var idspec = opensocial.newIdSpec({'userId' : 'OWNER', 'groupId' : 'FRIENDS'});
		var app_friend_params = {};
		app_friend_params[opensocial.DataRequest.PeopleRequestFields.MAX]=1000;
		app_friend_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];
		req.add(req.newFetchPeopleRequest(idspec,app_friend_params),'ownerFriends');


/*		var app_friend_params = {};
		app_friend_params[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL,opensocial.Person.Field.NAME,opensocial.Person.Field.THUMBNAIL_URL];
		app_friend_params[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.ALL;
		req.add(req.newFetchPeopleRequest(opensocial.newIdSpec({"userId" : "OWNER", "groupId" : "FRIENDS"}), app_friend_params), "ownerFriends");		
		*/
		req.send(reWriteFriendDiv);

	}


	function reWriteFriendDiv(friendsList)
	{
		var inner = document.getElementById("id_st_friends").innerHTML;
		viewer_friends = friendsList.get("ownerFriends").getData();
		var frnzArray = new Array();
		viewer_friends.each(function(person) 
			{ 
			var fr_profile_url = '';
			var fr_thumbnail_url = '';
			friend_name = person.getDisplayName(); 
			fr_profile_url = person.getField(opensocial.Person.Field.PROFILE_URL);
			friend_id = getOrkutIdByUrl(fr_profile_url);	 
			fr_thumbnail_url = person.getField(opensocial.Person.Field.THUMBNAIL_URL);
			frnzArray.push('<div style="background-color:#f5f5f5;margin:5px 6px 5px 5px;padding:8px 0 0 2px;float:left;height:96px;width:220px;"><input name="friendsCheckBox" type="checkbox" value="'+friend_id+'" class="editcheck"><img class="listimg" src="'+fr_thumbnail_url+'" border="0" align="left"><label id="'+friend_id+'"> '+friend_name.substring(0,17)+'</label></div>');
		   });

		document.getElementById("id_st_friends").innerHTML = frnzArray.join('');
	}

	function postCheckUserStatus(userStatus)
	{
		var msg = '';
		userStatus = trimString(userStatus.text);
		if(userStatus == "true")
		{
			msg = '<a href="'+containerUrl+'Profile.aspx?uid='+viewerId+'">'+viewer.getDisplayName() + '</a> added SuperScrap application!';
			postActivity(msg);
		}else if(userStatus == "false"){}
	}

	function createFriendListToInvite(){
		var req = opensocial.newDataRequest();
		var params_all = {};
		params_all[opensocial.DataRequest.PeopleRequestFields.MAX] = 1000;
		params_all[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL];
		req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.OWNER_FRIENDS, params_all), "invite_friends");
		
		var params_has_app = {};
		params_has_app[opensocial.DataRequest.PeopleRequestFields.MAX] = viewer_friends_length;
		params_has_app[opensocial.DataRequest.PeopleRequestFields.FILTER] = opensocial.DataRequest.FilterType.HAS_APP;
		params_has_app[opensocial.DataRequest.PeopleRequestFields.PROFILE_DETAILS] = [opensocial.Person.Field.PROFILE_URL ];
		req.add(req.newFetchPeopleRequest(opensocial.DataRequest.Group.OWNER_FRIENDS, params_has_app), "has_app_friends");
		req.send(generateFriendListHtml);
	}

	function generateFriendListHtml(friendsList){
		var temp_id_arr = new Array();
		var temp_name_arr = new Array();
		var temp_thumbnailurl_arr = new Array();
		var all_friends = 0;
		viewer_invite_friends = friendsList.get("invite_friends").getData();
		viewer_having_app_friends = friendsList.get("has_app_friends").getData();
		viewer_invite_friends_length = viewer_having_app_friends.getTotalSize();
		viewer_friends_length = viewer_invite_friends.getTotalSize();
		var viewer_all_friends_id_array = new Array();
		var viewer_all_friends_name_array = new Array();
		var viewer_all_friends_thumbnailurl_array = new Array();
		if(viewer_invite_friends_length == viewer_friends_length){
			document.getElementById('invite_sec').style.height= '35px';
			document.getElementById('invite_sec').innerHTML= '<br/>Wow! all your friends already have Superscrap Application.<br/>';
		}else{
			viewer_invite_friends.each(function(person) { 
				all_friends = all_friends + 1; 
				friend_name = person.getDisplayName(); 
				var fr_profile_url = '';
				var fr_thumbnail_url = '';
				fr_profile_url = person.getField(opensocial.Person.Field.PROFILE_URL);
				fr_thumbnail_url = person.getField(opensocial.Person.Field.THUMBNAIL_URL);
				friend_id = getOrkutIdByUrl(fr_profile_url);	 
				viewer_all_friends_name_array[all_friends] = friend_name;
				viewer_all_friends_thumbnailurl_array[all_friends] = fr_thumbnail_url;
				viewer_all_friends_id_array[all_friends] =  friend_id;
			});

			var viewer_having_app_friends_id_array = new Array();
			var viewer_having_app_friends_thumbnailurl_array = new Array();
			var viewer_having_app_friends_name_array = new Array();
			var has_app_count = 0;
			viewer_having_app_friends.each(function(person) { 
				has_app_count = has_app_count+1;
				friend_name = person.getDisplayName(); 
				var fr_profile_url = '';
				var fr_thumbnail_url = '';
				fr_profile_url = person.getField(opensocial.Person.Field.PROFILE_URL);
				fr_thumbnail_url = person.getField(opensocial.Person.Field.THUMBNAIL_URL);
				friend_id = getOrkutIdByUrl(fr_profile_url);	 
				viewer_having_app_friends_id_array[has_app_count] =  friend_id;
				viewer_having_app_friends_thumbnailurl_array[has_app_count] =  fr_thumbnail_url;
				viewer_having_app_friends_name_array[has_app_count] =  friend_name;
			});

			temp_id_arr = viewer_all_friends_id_array;
			temp_name_arr = viewer_all_friends_name_array;
			temp_thumbnailurl_arr = viewer_all_friends_thumbnailurl_array;
			var count = 0;
			for(var i=0; i<viewer_all_friends_id_array.length; i++){
				for(var j=0; j<viewer_having_app_friends_id_array.length; j++){
					if(viewer_all_friends_id_array[i] != null){
						if(viewer_all_friends_id_array[i] == viewer_having_app_friends_id_array[j]){
							count = i - j;
							temp_id_arr.splice(count+1,1);
							temp_name_arr.splice(count+1,1);
							temp_thumbnailurl_arr.splice(count+1,1);
						}
					}
				}		
			}
			var frnzArray = new Array();
			frnzArray.push('<br/><br/><div id="selection_panel_top" style="text-align:right;margin-right:60px;margin-left:60px;"><span style="float:left;"><a href="javascript:selectAll();">Select All</a>&nbsp;|&nbsp;<a href="javascript:selectNone();">Clear All</a></span>');
			frnzArray.push('<span style="float:right;"><a href="javascript:invite();"><img src="http://apps.postalz.com/'+area+'/orkut/images/ss_btn_invite.gif" border="0" alt="invite button" height="18" width="112"></a></span></div>');
			frnzArray.push('<br/><center><div class="st_main"  style="border:none;">'); 
			frnzArray.push('<div class="st_friends" style="height:300px;" id="id_invite_friends">');			
			for(var x=1;x<temp_id_arr.length;x++){
				frnzArray.push('<div style="background-color:#f5f5f5;margin:5px 6px 5px 5px;padding:8px 0 0 2px;float:left;height:96px;width:220px;" align="left"><input name="inviteCheckBox" type="checkbox" value="'+temp_id_arr[x]+'" class="editcheck"><img class="listimg" src="'+temp_thumbnailurl_arr[x]+'" border="0" align="left"><label style="padding:0 0 0 5px;" id="'+temp_id_arr[x]+'">'+temp_name_arr[x].substring(0,17)+'</label></div>');
			}
			frnzArray.push('<div style="clear:both;"></div>');
			frnzArray.push('</div>');
			frnzArray.push('</div></center>');
			frnzArray.push('<div id="selection_panel" style="text-align:right;margin-right:60px;margin-left:60px;"><span style="float:left;"><a href="javascript:selectAll();">Select All</a>&nbsp;|&nbsp;<a href="javascript:selectNone();">Clear All</a></span>');
			frnzArray.push('<span style="float:right;"><a href="javascript:invite();"><img src="http://apps.postalz.com/'+area+'/orkut/images/ss_btn_invite.gif" border="0" alt="invite button" height="18" width="112"></a></span></div>');
			friendsToInvite = frnzArray.join('');
			document.getElementById('invite_sec').innerHTML= friendsToInvite;
		}
	}	
	
	function noOfFriendsToInvite(){
		allFriends = ss_form.inviteCheckBox;
		var check=false;
		if((allFriends != null) && (allFriends.length == null)){
				if(allFriends.checked == false){
					allFriends.checked = true;
				}
			noOfFriends = noOfFriends +1;
		}else{
			for (i = 0; i < allFriends.length; i++){
				if(ss_form.inviteCheckBox[i].checked){
					check = true;
					noOfFriends = noOfFriends +1;
				}//if
			}//for
		}
		return noOfFriends;
	}

	function invite(){
		var num = noOfFriendsToInvite();
		var msg = viewer_name +' has invited '+num + ' friends to use Superscrap application.';
		if(noOfFriends == 0 || noOfFriends == null){
			alert("please select a friend to invite..");
			return;	
		} else if(noOfFriends == 1){
			msg = viewer_name +' has invited one friend to use <a href="'+containerUrl+'Application.aspx?appId='+appId+'&uid='+viewerId+'">Superscrap application</a>.';
		} else {
			msg = viewer_name +' has invited '+num+ ' friends to use <a href="'+containerUrl+'Application.aspx?appId='+appId+'&uid='+viewerId+'">Superscrap application</a>.';
		}
		postActivity(msg);
		document.getElementById('invite_sec').innerHTML= "<center style=\"padding:20px;\">Your invite has been sent Succesfully! Happy Superscrapping!</center>";
	}

	function clearText(){
		if(document.getElementById("id_engmessage").value == "Your text here..."){
			document.getElementById("id_engmessage").value = "";
			document.getElementById("id_hindimessage").innerHTML = "";
		}		
	}
	function registerImgOnLoad(){
		noOfTemplatesLoaded = noOfTemplatesLoaded +1;
		if(noOfTemplatesLoaded == totalNoOfTemplates){
			document.getElementById("id_templ_loading").style.display = 'none';
			document.getElementById("id_templ_container").style.display = '';
			document.getElementById("id_templ_tabs").style.display = '';
			document.getElementById("id_temp2_tabs").style.display = 'none';
			document.getElementById("id_temp3_tabs").style.display = 'none';
			document.getElementById('subMain').style.display = '';
			document.getElementById('loader').style.display = 'none';

			noOfTemplatesLoaded = 0;
		}	
		counter = counter - 1;
		document.getElementById("counter").innerHTML = counter;
	}

	function selectAll(){
		var field = ss_form.inviteCheckBox;
		var i = 0;
		if(field.length == null){
			field.checked = true ;
		}else{
			var x = 1;
			for (i = 0; i<field.length; i=i+1){
				field[i].checked = true;
			}
		}
	}

	function selectNone(){
		var field = ss_form.inviteCheckBox;
		var i = 0;
		if(field.length == null){
			field.checked = true ;
		}else{
			var x = 1;
			for (i = 0; i<field.length; i=i+1){
				field[i].checked = false;
			}
		}
	}

	function selectAllFriends(){
		var field = ss_form.friendsCheckBox;
		var i = 0;
		if(field.length == null){
			field.checked = true ;
		}else{
			var x = 1;
			for (i = 0; i<field.length; i=i+1){
				field[i].checked = true;
			}
		}
	}

	function selectNoneFriends(){
		var field = ss_form.friendsCheckBox;
		var i = 0;
		if(field.length == null){
			field.checked = true ;
		}else{
			var x = 1;
			for (i = 0; i<field.length; i=i+1){
				field[i].checked = false;
			}
		}
	}
	function setTemplateHtml(templateHtml)
	{
		fetchTemplateTabs();
		templateHtml = trimString(templateHtml.text);
		document.getElementById("id_templ_container").innerHTML = '';
		document.getElementById("id_templ_container").innerHTML = templateHtml;
		document.getElementById("loader").style.display = 'none';
	}

	function setTabHtml(tabHtml){
		//alert (tabHtml);
		var initStr = '<div style="background-image:url(\''+domain+area+'/orkut/images/ss_noti.gif\'); background-repeat:repeat-x; height:16px; margin:0 16px 8px 6px; padding: 4px 4px 4px 8px; font-family:verdana; font-size:12px; font-weight:bold; width:97%; color:#323232;">Select a template</div>';
		tabHtml = initStr + trimString(tabHtml.text);
		document.getElementById("id_templ_tabs").innerHTML = tabHtml;
		document.getElementById(tabToBeSelected).className='linkbold';
		var d = new Date();
		var noCacheStr = d.getTime(); // this is to fool the browser
	}
 function toggle(targetId) {
	target = document.all(targetId);
	if (target.style.display == "none"){
	target.style.display="";
	} else {
	target.style.display="none";
	}
}
function hlink1(){
window.parent.location="http://www.orkut.co.in/Application.aspx?appId=315751537744";
}


(function(){
	if(!String.prototype.toCharRef)
		String.prototype.toCharRef = function()
		{
			var charRefs = [], codePoint, i;
		
			for(i = 0; i < this.length; ++i)
			{
				codePoint = this.charCodeAt(i);
		
				if(0xD800 <= codePoint && codePoint <= 0xDBFF)
				{
					i++;
					codePoint = 0x2400 + ((codePoint - 0xD800) << 10) + this.charCodeAt(i);
				}
				charRefs.push('&#' + codePoint + ';');
			}
			return charRefs.join('');
		};

	if(!String.prototype.fromCharRef)
		String.prototype.fromCharRef = function()
		{
			var el = document.createElement("div");
			el.innerHTML = this;
			return el.firstChild.data;			
		};

})();
