function RemoveHTMLTags(strText, strFormFieldName)
{
	var regEx = new RegExp("<|%3C|>|%22|%3E|%2F|%3c|%3e|%2f/","g");
	var m = regEx.exec(strText.value);
	if (m == null) 
	{
		//alert("No match"); do nothing
	} 
	else 
	{
		alert("\r\nPotentially dangerous characters were removed from the " + strFormFieldName + "\r\n Characters like {<>/} are not allowed\r\n");
	}
	strText.value = strText.value.replace(regEx, "");
}



function selectThumb(thumb, mainPhotoId, noPhotoId) {
	if (thumb.parentNode.className.indexOf('thumbSelected') < 0) {
		var mainPhoto = document.getElementById(mainPhotoId);
		
		mainPhoto.src = thumb.src;
		var anchor = mainPhoto.parentNode;
		anchor.href = mainPhoto.src;
		
		var photoListUL = thumb.parentNode.parentNode;
		var photos = photoListUL.getElementsByTagName('IMG');
		
		for (var i = 0; i < photos.length; i++) {
			photos[i].parentNode.className = photos[i].parentNode.className.replace(' thumbSelected', '');
		}
		
		thumb.parentNode.className += ' thumbSelected';
		
		var noPhoto = document.getElementById(noPhotoId);
		
		if (noPhoto != undefined) {
			noPhoto.style.display = 'none';
		}
		mainPhoto.parentNode.style.display = '';
	}
	
	//update manifest
	for (var i = 0; i < photoManifest.length; i++) {
		photoManifest[i][1] = false;
		if(photoManifest[i][0] == thumb.id) {
			photoManifest[i][1] = true;
		}
	}
}

function nextPhoto() {
	var found = false;
	for (var i = 0; i < photoManifest.length; i++) {
		if(photoManifest[i][1]) {
			found = true;
			if (i < photoManifest.length - 1) {
				document.getElementById(photoManifest[i + 1][0]).onclick();
			}
			else {	//click the first one
				document.getElementById(photoManifest[0][0]).onclick();
			}
			break;
		}
	}
	
	if (!found)
		document.getElementById(photoManifest[0][0]).onclick();
}

function prevPhoto() {
	var found = false;
	for (var i = 0; i < photoManifest.length; i++) {
		if(photoManifest[i][1]) {
			found = true;
			if (i > 0) {
				document.getElementById(photoManifest[i - 1][0]).onclick();
			}
			else {	//click the first one
				document.getElementById(photoManifest[photoManifest.length - 1][0]).onclick();
			}
			break;
		}
	}
	
	if (!found)
		document.getElementById(photoManifest[photoManifest.length - 1][0]).onclick();
}

function constrainPhoto(img) {

	img.style.display = 'none';
				
	var testImg = new Image();
	testImg.src = img.src;
	var maxWidth = parseInt(img.getAttribute("constrainWidth"));
	var maxHeight = parseInt(img.getAttribute("constrainHeight"));
	
	if (maxWidth == 0)
		maxWidth = testImg.width;
	
	if (maxHeight == 0)
		maxHeight = testImg.height;
	
	var heightScale = maxHeight / Math.max(testImg.height, maxHeight);
	var widthScale = maxWidth / Math.max(testImg.width, maxWidth);
	
	var scaleFactor = Math.min(heightScale, widthScale);
	
	img.width = testImg.width * scaleFactor;
	img.height = testImg.height * scaleFactor;
	
	img.style.display = '';
}

function addMatchWord() {
	var matchword = document.getElementById('txtMatchWord').value.toLowerCase().replace(/ /g, '');

    // force input lower case, no space
    document.getElementById('txtMatchWord').value = matchword;

    if(matchword == '') return;

	MatchCore.UIControls.PortraitDisplay.Modules.MatchWords.AddMatchWord(profileIdHash , userIdHash, matchword, callback_addMatchWord);

}

function callback_addMatchWord(res)
{
    var matchword = document.getElementById('txtMatchWord').value;

    var retVallue = res.value.split(',');     //res format: returnValue + "," + tagID

    var oUL;
    var oNewNode;
    if (retVallue[0] == '1')   // 1=whitelist add to other list
    {

        oUL = document.getElementById('listOtherMatchWords');

        oNewNode = document.createElement('LI');

        oNewNode.innerHTML = '<a href=\"../matchwords/' + matchword +' id=\"word' +  matchword + '\" >' +  matchword + '</a>';

        oUL.appendChild(oNewNode);
    }
    else if (retVallue[0] == '0' ) // && retVallue[1] != '0' )    // 0,num=unreviewed, 0,0=invalid tag
    {
        document.getElementById('divPendingLable').style.display = '';

        oUL = document.getElementById('listPendingMatchWords');

        oNewNode = document.createElement('LI');
        oNewNode.innerHTML = '<span>' + matchword + '</span>';

        oUL.appendChild(oNewNode);
    }

    document.getElementById('txtMatchWord').value = '';
}

function addMatchWordToMyProfile(matchWord)
{    
    MatchCore.UIControls.PortraitDisplay.Modules.MatchWords.AddMatchWordToLoggedInUsersProfile(userIdHash, matchWord, callback_addMatchWordToMyProfile);
}

function callback_addMatchWordToMyProfile(res)
{    
    var retVallue = res.value.split(',');     //res format: returnValue + "," + tagID + "," + Tag

    switch (retVallue[0]) {
        case '1':
        case '3':
        case '255':
            document.getElementById('listWord' + retVallue[2]).className = "highlight";
            break;
        case '5':
            alert("You have added too many MatchWords to your Portrait.  Please remove some MatchWords from your Portrait before adding more.");
            break;
        default: 
            alert("Error::193879");
    }

}


function searchBlock()
{
    var btnString;
    btnString = document.getElementById('hlnkSearchBlockbottom').innerHTML;

    if (btnString)
    {
        if ( btnString.indexOf('Block') == 0 )
        {
            gotoSearchBlockPage('block');
        }
        else
        {
            gotoSearchBlockPage('unblock');
        }
    }
    return;

}

function gotoSearchBlockPage(context)
{
    if ( context == 'block' )
    {
        document.getElementById('hlnkSearchBlockbottom').href = removeThisProfile;
    }
    else
    {
        document.getElementById('hlnkSearchBlockbottom').href = undoRemoveThisProfile;
    }

    document.getElementById('hlnkSearchBlockbottom').click();
    return;
}


function blockContact()
{
    gotoBlockContactPage();

    var btnString;
    btnString = document.getElementById('hlnkContactBlockbottom').innerHTML;

    if (btnString)
    {
        if ( btnString.indexOf('Block') == 0 )
        {
            gotoBlockContactPage('block');
        }
        else
        {
            gotoBlockContactPage('unblock');
        }
    }
    return;

}

function gotoBlockContactPage(context)
{
    if ( context == 'block' )
    {
        document.getElementById('hlnkContactBlockbottom').href = contactBlock;
    }
    else
    {
        document.getElementById('hlnkContactBlockbottom').href = contactUnblock;
    }
    document.getElementById('hlnkContactBlockbottom').click();
    return;
}

function showMatchWordsHelp(show)
{
	document.getElementById('matchWordsHelp').style.display = (show) ? '' : 'none';
}

function openCompatWin(sec)
{
	myWin = window.open('../html/custom/template.blank.html', 'compatWin', 'width=715,height=550,status=0,titlebar=0,toolbar=0,scrollbars=1,resizable=1');
	myWin.name = 'compatWin';
	document.forms['matchMainForm'].target = 'compatWin';
	document.forms['matchMainForm'].action = compatWin + '&sec=' + sec;
	document.forms['matchMainForm'].submit();
}
