 // JavaScript Document
function checkText(box, val){
	if(box.value == val)
		box.value = "";
	else if(box.value == "")
		box.value = val;
}

function show(div){
	if(!$(div))
		return;
	$(div).style.display = "block";	
}
function hide(div){
	if(!$(div))
		return;
	$(div).style.display = "none";	
}

function showhide(div){
	if(!$(div))
		return;
	if($(div).style.display=="none" || $(div).style.display==""){
		show(div);
		if(div == "signin"){
			 $('user').focus();
		}
	} else
		hide(div);
	
}

function loadPage(url,waitdiv,loaddiv,postvars, waiting){
	if(postvars==null)
		vartype='get';
	else
		vartype='post';
	
	if(waiting==null)
		waiting = '<p align="center"><span style="display:block; height:100%; vertical-align:middle;">Please wait:<br /><img src="/mainsite/images/loader.gif"></span></p>';
		
	if(waitdiv==null)
		waitdiv = 'content_div';
	if(loaddiv==null || !$(loaddiv))
		loaddiv = waitdiv;
	if($(waitdiv)){
		$(waitdiv).innerHTML= waiting;	
	}
	new Ajax(url, {
		evalScripts: true,
		update: loaddiv,
		method: vartype,
		postBody: postvars
		}).request();
}

function FlexOpenURL( url, target ){
	try {
		var popup = window.open( url, target );
		if ( popup == null )
			return false;						
		if ( window.opera )								
			if (!popup.opera)
				return false;
	} catch(err) {
		return false;								
	}								
	return true;
}

function login(){
	post = "user="+$('user').value;
	post += "&pass="+$('pass').value;
	loadPage('/mainsite/login.html', 'login_message', 'login_message', post, 'Please wait...');	
}

function submitenter(myfield,e){
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	else 
		return true;

	if (keycode == 13){
   		login();
   	} else {
	   return true;
	}
}

function vote(choice_id, outfit){
	MOOdalBox.open('/mainsite/social/vote.php?choice_id='+choice_id+'&vote_for='+outfit,'','400 100');
}

function getGossip(){
	MOOdalBox.open('/mainsite/social/gossip_get_message.php?user_id='+page_id,'','400 200');
}

function sendGossip(){
	var escaped = escape($('new_gossip').value);
	escaped = escaped.replace(/%0A/g,'');
	escaped = escaped.replace(/%0D/g,'');
	if($('new_gossip').value == "" || escaped == ""){
		$('gossip_message').innerHTML = "You must enter a message then select post.";
		return false;
	}
	loadPage('/mainsite/social/addComment.php?user_id='+page_id+'&message='+escape($('new_gossip').value),'gossip_message','gossip_message','','Sending...');	
}

function addFriend(user_id){
	if(user_id==null)
		user_id = '?user_id='+page_id;
	else
		user_id = '?user_id='+user_id;
	MOOdalBox.open('/mainsite/social/addFriendMessage.php'+user_id,'','400 190');
}

function sendAddFriend(user_id){
	if(user_id == null)
		user_id = page_id;
	loadPage('/mainsite/social/addFriend.php?user_id='+user_id+'&message='+escape($('new_friend').value),'friend_message','friend_message','','Sending...');	
	$('new_friend_submit').disabled = true;
}

function maxChars(textfield, message, maxChars){
	charsLeft = maxChars - textfield.value.length;
	$(message).innerHTML = charsLeft+" characters left";
}

function getWardrobe(type){
	if(type==null)
		type = "";
	loadPage('/mainsite/social/get_wardrobe.php?user_id='+page_id+'&type='+type,'getwardrobe_div');	
}

function displayPage(type){
	offset = (_page-1)*perpage;
	html = "";
	switch(type){
		case 'hot':
			link_start = "<a href=\"javascript:setHotPiece('itemcode');\">";
			link_end = "</a>";
			break;
		default:
			link_start = "";
			link_end = "";
			break;
	}
	
	for(var i=0; i<perpage; i++){
		if(i+offset < wardrobelist.length)
			html += "<div class='wardrobe_item' onclick='showZoom(\""+wardrobelist[i+offset]+"\")'>"+link_start.replace(/itemcode/,wardrobelist[i+offset])+"<img src='"+getImage(wardrobelist[i+offset])+"' height='100%' border='0' />"+link_end+"</div>";
	}
	$('wardrobe_page').innerHTML = html;
	doPageLinks('wardrobe', type);
}


function displayOutfitPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < outfitslist.length)
			html += "<div class='outfit_item'><img src='"+outfitslist[i+offset]+"' height='100%' /></div>";
	}
	$('wardrobe_page').innerHTML = html;
	doPageLinks('outfits');
}

function displayOutfitChoicesPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < outfitslist.length)
			html += "<div class='outfit_item'><a href='javascript:addOutfitChoice("+(i+offset)+");'><img src='"+outfitslist[i+offset]+"' height='100%' border='0' /></a></div>";
	}
	$('wardrobe_page').innerHTML = html;
	doPageLinks('outfit_choices');
}

function displayAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length)
			html += "<div class='stylist_item'><a href='/stylists/"+advisernames[i+offset]+"' class='option' style='text-transform:none;'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"</a></div>";
	}
	$('wardrobe_page').innerHTML = html;
	doPageLinks('advisers');
}

function displaySearchAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length){
			if(pendingadviser.indexOf(adviserIDs[i+offset]) == -1){
				html += "<div class='stylist_item_add'><a href='javascript:addFriend("+adviserIDs[i+offset]+");' class='option' style='text-transform:none;'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<br><img src='/mainsite/images/skins/"+skin_name+"/add_advisers.png' border='0' align='absmiddle'><span style='text-transform:uppercase; padding-left:5px;'>Add</span></a> | <a href='/stylists/"+advisernames[i+offset]+"' class='option'><span style='text-transform:uppercase;'>View</span></a></div>";
			} else {
				html += "<div class='stylist_item_add'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<br><img src='/mainsite/images/skins/"+skin_name+"/pending_advisers.png' border='0' align='absmiddle'><span style='text-transform:uppercase; padding-left:5px; font-size:0.8em;'>Pending</span></a></div>";
			}
		}
	}
	$('wardrobe_page').innerHTML = html;
	doPageLinks('search_advisers');
}

function displayPendingAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length){
			html += "<div class='stylist_item_pending'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<div class='stylist_item_pending_links'>"+
			"<a href='javascript:confirmFriend("+adviserIDs[i+offset]+");' class='option'>Confirm</a> |"+
			"<a href='javascript:denyFriend("+adviserIDs[i+offset]+");' class='option' style='padding-left:5px;'>Deny</a><br>"+
			"<a href='/stylists/"+advisernames[i+offset]+"' class='option'>View</a>"+
			"</div></div>";
		}
	}
	$('wardrobe_page').innerHTML = html;
	doPageLinks('pending_advisers');
}

function displayEditAdviserPage(){
	offset = (_page-1)*perpage;
	html = "";
	for(var i=0; i<perpage; i++){
		if(i+offset < adviserlist.length){
			html += "<div class='stylist_item_pending'><img src='/mainsite/social/"+adviserlist[i+offset]+"' height='90' border='0' /><br>"+advisernames[i+offset]+"<div class='stylist_item_pending_links'>"+
			"<img src='/mainsite/images/skins/"+skin_name+"/clear_advisers.png' border='0' align='absmiddle'><a href='javascript:removeAdviser("+adviserIDs[i+offset]+");' class='option' style='padding-left:5px;'>Remove</a>"+
			"</div></div>";
		}
	}
	$('wardrobe_page').innerHTML = html;
	doPageLinks('edit_advisers');
}

function doPageLinks(type, linktype){
	switch(type){
		case 'wardrobe':
			array = wardrobelist;
			break;
		case 'outfits':
		case 'outfit_choices':
			array = outfitslist;
			break;
		case 'advisers':
		case 'search_advisers':
		case 'pending_advisers':
		case 'edit_advisers':
			array = adviserlist;
	}
	total = array.length;
	pages = Math.ceil(total/perpage);
	html = "";
	if(pages>0){
		if(_page>1)
			html += "<a href=\"javascript:prevPage('"+type+"', '"+linktype+"');\">&lt; previous</a> | ";
		html += "page "+_page+" of "+pages+" ";
		if(_page<pages)
			html += "| <a href=\"javascript:nextPage('"+type+"', '"+linktype+"');\">next &gt;</a>";
	}
	$('wardrobe_page_links').innerHTML = html;
}

function nextPage(type, linktype){
	_page ++;
	switch(type){
		case 'wardrobe':
			displayPage(linktype);
			break;
		case 'outfits':
			displayOutfitPage();
			break;
		case 'outfit_choices':
			displayOutfitChoicesPage();
			break;
		case 'advisers':
			displayAdviserPage();
			break;
		case 'search_advisers':
			displaySearchAdviserPage();
			break;
		case 'pending_advisers':
			displayPendingAdviserPage();
			break;
		case 'edit_advisers':
			displayEditAdviserPage();
			break;
	}
}
function prevPage(type, linktype){
	_page --;
	switch(type){
		case 'wardrobe':
			displayPage(linktype);
			break;
		case 'outfits':
			displayOutfitPage();
			break;
		case 'outfit_choices':
			displayOutfitChoicesPage();
			break;
		case 'advisers':
			displayAdviserPage();
			break;
		case 'search_advisers':
			displaySearchAdviserPage();
			break;
		case 'pending_advisers':
			displayPendingAdviserPage();
			break;
		case 'edit_advisers':
			displayEditAdviserPage();
			break;
	}
}

function getImage(itemcode){
	var rcode = itemcode.substr(2,2);
	return '/nn4m/dbproduct/'+rcode+'/small/'+itemcode+'.jpg';
}

function editSites(){
	MOOdalBox.open('/mainsite/social/edit_sites.php?user_id='+page_id, '', '500 260');
}

function updateSites(){
	url = "/mainsite/social/update_sites.php?user_id="+page_id+"&"+$('sites_form').toQueryString();
	loadPage(url,'sites_message');
}

function clearTextField(name, defaultVal){
	if(defaultVal == null)
		defaultVal = "";
	$(name).value = defaultVal;	
}

function removeGossip(id){
	MOOdalBox.open('/mainsite/social/gossip_confirm_remove.php?mess_id='+id, '', '400 100');	
}

function confirmRemoveGossip(id){
	loadPage('/mainsite/social/gossip_remove.php?mess_id='+id, 'gossip_remove_message');	
}

function editHotPiece(){
	MOOdalBox.open('/mainsite/social/show_wardrobe.php?user_id='+page_id+'&type=hot', 'Select the item you want as your favourite piece', '500 520');	
}

function setHotPiece(itemcode){
	loadPage('/mainsite/social/hot_item_set.php?itemcode='+itemcode, 'wardrobe_message');	
}

function editProfile(){
	MOOdalBox.open('/mainsite/social/update_profile.php?user_id='+page_id, '', '500 500');
	
	if($('wardrobe_div')){
		setTimeout(1000,$('wardrobe_div').style.visibility = 'visible');	
	}
}

function showFileProgress(){
	$('file_upload_message').innerHTML = "Uploading <img src='/mainsite/images/loader.gif' align='absmiddle' />";
}

function friendSearch(){
	searchval = $('friend_search').value;
	if(searchval.length>1)
		MOOdalBox.open('/mainsite/social/search_advisers.php?search='+searchval, '', '500 440');	
}

function friendRequests(){
	MOOdalBox.open('/mainsite/social/pending_advisers.php?user_id='+page_id, '', '500 360');	
}

function confirmFriend(friend_id){
	loadPage('/mainsite/social/update_advisers.php?action=confirm&friend_id='+friend_id,'wardrobe_message');	
}

function denyFriend(friend_id){
	loadPage('/mainsite/social/update_advisers.php?action=deny&friend_id='+friend_id,'wardrobe_message');	
}

function editChoices(){
	MOOdalBox.open('/mainsite/social/edit_choices.php?user_id='+page_id, 'Select the two outfits that want your friends to vote on.', '670 510');	
}

function addOutfitChoice(outfit_index){
	if(outfit_choices.length == 0){
		outfit_choices[0] = outfitIDs[outfit_index];
		$('outfit1').innerHTML = "<p align='center'><img src='"+outfitslist[outfit_index]+"' height='150' /></p>";
	} else 	if(outfit_choices.length == 1){
		outfit_choices[1] = outfitIDs[outfit_index];
		$('outfit2').innerHTML = "<p align='center'><img src='"+outfitslist[outfit_index]+"' height='150' /></p>";
	} else {
		alert("You can only select two outfits. Please remove one of your selections first.");	
	}
}

function clearOutfitChoice(index){
	outfit_choices.splice(index,1);
	$('outfit'+(index+1)).innerHTML = "<p align='center'></p>";	
	
	if(index==0){
		$('outfit1').innerHTML = $('outfit2').innerHTML;
		$('outfit2').innerHTML = "<p align='center'></p>";		
	}
}

function saveOutfitChoice(){
	if(outfit_choices.length<2){
		alert("You must select two outfits to proceed");
	} else {
		loadPage('/mainsite/social/save_outfit_choice.php?user_id='+page_id+'&outfit1='+outfit_choices[0]+'&outfit2='+outfit_choices[1], 'outfit_choice_save');
	}
}

function editAdvisers(){
	MOOdalBox.open('/mainsite/social/edit_advisers.php?user_id='+page_id, '', '550 500');	
}

function removeAdviser(id){
	if(confirm("Are you sure you want to remove this style adviser?")){
		loadPage('/mainsite/social/remove_adviser.php?user_id='+page_id+'&friend_id='+id, 'remove_friend');	
	}
}

function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveEnd('character', o.value.length)
		if (r.text == '') return o.value.length
		return o.value.lastIndexOf(r.text)
	} else return o.selectionStart
}

function getSelectionEnd(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveStart('character', -o.value.length)
		return r.text.length
	} else return o.selectionEnd
}

function addSmiley(val, textfield){
	var v = textfield.value;
	var s = getSelectionStart(textfield);
	var e = getSelectionEnd(textfield);
	
	var newText = v.substr(0,s) + val + v.substr(e);
	textfield.value = newText;
}

function checkUsername(dir){
	if(dir=='in'){
		if($('user').value == "USERNAME / EMAIL")
			$('user').value = "";
	} else {
		if($('user').value == "")
			$('user').value = "USERNAME / EMAIL";
	}
}

function checkPassword(dir){
	if(dir=='in'){
		$('pass_holder').style.display = "none";
		$('pass').style.display = "block";
		$('pass').focus();
	} else {
		if($('pass').value == ""){
			$('pass').style.display = "none";
			$('pass_holder').style.display = "block";
		}
	}
}

function checkDetails(){
	if($('pass1').value != ""&& !validatePassword())
		return false;
  if(document.pressed == 'Confirm Details')
  {
	loadPage('/mainsite/social/save_profile_details.php?user_id='+page_id+'&'+$('details_form').toQueryString(), 'details_upload_message');

  }
  else
  if(document.pressed == 'Update')
  {
  loadPage('/mainsite/social/confirm_profile_details.php?user_id='+page_id+'&'+$('details_form').toQueryString(), 'details_upload_message');
  
  }
}

function showTab(tabs, selectedTab){
	for(var i=0; i<tabs.length; i++){
		$(tabs[i]+'_tab').className = "";	
		$(tabs[i]+'_pane').style.display = "none";	
	}
	$(selectedTab+'_tab').className = "current";
	$(selectedTab+'_pane').style.display = "block";
}

var profileTabs = new Array('details', 'image', 'skin', 'layout');

function prevSkin(){
	var idx = $('selected_skin').value;
	
	$('sample_box').style.borderColor = borderColours[idx];
	$('sample_box').style.backgroundColor = boxColours[idx];
	
	$('sample_banner').style.color = headerTextColours[idx];
	$('sample_banner').style.backgroundColor = headerColours[idx];
	
	$('sample_link').style.color = linkColours[idx];
}

function changeSkin(){
	var idx = $('selected_skin').value;
	loadPage('/mainsite/social/save_skin.php?user_id='+page_id+'&skin_id='+idx, 'skin_upload_message');
}

function enlargeFavourite(url, size){
	MOOdalBox.open(url, '', size);	
}

function storeWindowSize() {
  return;
	
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
 
  loadPage('/mainsite/social/window_size.php?width='+myWidth+'&height='+myHeight, '');
}

function validatePassword(){
	if($('pass1').value != $('pass2').value){
		alert("The passwords you have entered do not match");
		return false;
	} else if($('pass1').value.length < 6){
		alert("The password you have entered is not long enough, it must be between 6 and 15 characters long");
		return false;
	}
	return true;
}

function showZoom(img){
	retailer = img.substr(2,2);
	type = img.substr(0,2);
	img_url = '/nn4m/dbproduct/'+retailer+'/large/'+img;
	
	switch(type){
		case 'TP':
		case 'SK':
		case 'DR':
		case 'PA':
		case 'JA':
			img_url+='.gif';
			break;
		default:
			img_url+='.jpg';
			break;
	}
	
	$('wardrobe_zoom_image').src = img_url;
	$('wardrobe_zoom').style.display = 'block';
}

function closeZoom(){
	$('wardrobe_zoom').style.display = 'none';
}

var currHelp = 1;

function showHelp(number){
	for(i = 1; i < 5; i++){
		$('help_0'+i+'_img').src = 'images/help_0'+i+'.jpg';
		hide('help_0'+i+'_content');
	}
	
	currHelp = number;
	$('help_0'+number+'_img').src = 'images/help_0'+number+'_over.jpg';
	show('help_0'+number+'_content');
}

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];
}

function updateHeader(){
	loadPage('/mainsite/header.php?view='+_currentPage,'','header_div');	
}

function addToWardrobe(itemcode){
	MOOdalBox.open('/mainsite/social/addToWardrobe.php?itemcode='+itemcode,'','300x160');	
}

function removeFromWardrobe(itemcode){
	MOOdalBox.open('/mainsite/social/removeFromWardrobe.php?itemcode='+itemcode,'','300x160');	
}