$(document).ready(function() {
	$(function() 
	{

	$('abbr').Tooltip({
		delay: 0
	});

	});
	
	$('.box-description').css('display', 'none');


	$('.box-word-item').click( function()
	{
		$(this).children('.box-description').slideToggle('fast');
	    
	$(this).children('h2').toggleClass('on');
	    
		return false;
	});
	
	$('.box-description').click( function(event)
	{
		if ( !$(event.target).is('a') )
		{
			$(this).show('fast');
		}
		else
		{
			location.href = event.target;
		}
		return false;
	});
	
	function formatItem(row) {
		return URLDecode(row[0]);
	}
	function formatResult(row) {
		return URLDecode(row[0]);
	}
	
	$("#inputString").autocomplete('/includes/getCompanies.asp', {
		selectFirst: true,
		multiple: false,
		matchContains: true,
		autoFill: true,
		scroll: true,
		minChars: 1,
		formatItem: formatItem,
		formatResult: formatResult
	});
	
	$("#inputString").result(function(event, data, formatted) {
		var hidden = $(this).find("..+/:input");
		hidden.val( (hidden.val() ? hidden.val() + ";" : hidden.val()) + data[1]);
	});
	
});


/*function lookup(inputString) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("/includes/getCompanies.asp", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup

function fill(thisValue) {
	$('#inputString').val(thisValue);
$('#suggestions').hide();
}*/

function URLDecode(psEncodeString)
{
  // Create a regular expression to search all +s in the string
  var lsRegExp = /\+/g;
  // Return the decoded string
  return unescape(String(psEncodeString).replace(lsRegExp, " "));
}

/*function toggleWordInfoDiv(strHeaderName, strDivName)
{
	var elHeader = document.getElementById(strHeaderName);
	var elDiv = document.getElementById(strDivName);
	var strHeader = elHeader.className;
	
	if(elDiv.style.display == "none")
	{
		elHeader.className = strHeader + "-on";
		elDiv.style.display = "block";
	}
	else
	{
		elHeader.className = strHeader.replace("-on", "");
		elDiv.style.display = "none";
	}
}

function initWordInfoDiv()
{
	
}
*/
function gotoNextStep(strLink, nQuestionId, nLastStepId, nNextStepId, nComplaintFormId, oForm)
{

	if(strLink != "" && strLink+"" != "null")
	{
		oForm.action = strLink;
	}
	else
	{
		oForm.action = "";
	}
	
	oForm.stid.value = nNextStepId;
	oForm.laststid.value = nLastStepId;
	oForm.questionid.value = nQuestionId;
	oForm.cfid.value = nComplaintFormId;
	oForm.submit();

}

function OpenFileDlg( nFileId, nCateId, strConsumerSession, nComplaintFormID, nComplaintLetterID, nFileTypeId, nLogo, oForm )
{
	OFD2( nFileId, nCateId, strConsumerSession, nComplaintFormID, nComplaintLetterID, nFileTypeId, nLogo, oForm );
}				

function OFD2( nFileId, nCateId, strConsumerSession, nComplaintFormID, nComplaintLetterID, nFileTypeId, nLogo, oForm )
{

	try
	{
		var strContents;
		var strResult;
		var nBrowser;
		
		if (window.showModalDialog)
		{	
			nBrowser = 1;
			strContents = escape( '/imageplugin/insertPicture.aspx?fileid='+ nFileId +'&catid='+ nCateId +'&consumerSession='+ strConsumerSession +'&formid='+ nComplaintFormID + '&letterid=' + nComplaintLetterID + '&typeid=' + nFileTypeId + '&logo=' + nLogo + '&browser=' + nBrowser + '&slod=/files/' );
			strResult = showModalDialog('/imageplugin/FormModalDialog.asp?iframeContents='+strContents,'','dialogWidth:330px;dialogHeight:330px;scroll:auto;status:no;resizable:no;');			
		}
		else
		{
			nBrowser = 2;
			strContents = escape( '/imageplugin/insertPicture.aspx?fileid='+ nFileId +'&catid='+ nCateId +'&consumerSession='+ strConsumerSession +'&formid='+ nComplaintFormID + '&letterid=' + nComplaintLetterID + '&typeid=' + nFileTypeId + '&logo=' + nLogo + '&browser=' + nBrowser + '&slod=/files/' );
			
			var height = 330;
			var width = 330;
			var winw = 100;
			var winh = 100;

			if (winw < 0) winw = 0;
			if (winh < 0) winh = 0; 

			window.open('/imageplugin/FormModalDialog.asp?iframeContents='+strContents, 'Modal','height=330px,width=330px,top='+ winh +',left='+ winw +',toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,modal=yes');
		}
		
	}
	catch(e){}
	
	
	
	oForm.submit();
	
}


function selectText(oCtrl)
{
	oCtrl.focus();
	oCtrl.select();
}

function checkRequiredField(strLabel, strCtrl)
{
	var bRequiredFieldOK = true;
	if(strCtrl.value == "")
	{
		document.getElementById(strLabel).style.color = "red";
		bRequiredFieldOK =  false;
	}
	else
	{
		document.getElementById(strLabel).style.color = "";
		bRequiredFieldOK =  true;
	}
	
	return bRequiredFieldOK;
}
function checkMatchingField(strLabel, strCtrl, strLabel2, strCtrl2)
{
	var bRequiredFieldOK = true;
	if(strCtrl.value != strCtrl2.value)
	{
		document.getElementById(strLabel).style.color = "red";
		document.getElementById(strLabel2).style.color = "red";
		bRequiredFieldOK =  false;
	}
	/*else
	{
		document.getElementById(strLabel).style.color = "";
		document.getElementById(strLabel2).style.color = "";
		bRequiredFieldOK =  true;
	}*/
	
	return bRequiredFieldOK;
}

function checkEditConsumer(oForm)
{
	var bFormOK = true;
	var bMatchOK = true;
	var strMatchText = "";
	var strEmailText = "";
	
	if(!checkRequiredField('lblConsumerName', oForm.ConsumerName))
	{
		bFormOK = false;
	}
	if(!checkRequiredField('lblUserName', oForm.UserName))
	{
		bFormOK = false;
	}	
	if(!checkEmail(oForm.UserName.value))
	{
		bFormOK = false;
		strEmailText = " Notandanafn þarf að vera löglegt netfang.";
	}
	if(!checkRequiredField('lblPassword', oForm.Password))
	{
		bFormOK = false;
	}
	if(!checkRequiredField('lblConfirmPassword', oForm.ConfirmPassword))
	{
		bFormOK = false;
	}
	if(!checkMatchingField('lblPassword', oForm.Password, 'lblConfirmPassword', oForm.ConfirmPassword))
	{
		bFormOK = false;
		bMatchOK = false;
		if(!bMatchOK)
		{
			strMatchText += " Lykilorðareitirnir verða að innihalda eins lykilorð.";
		}
	}
	
	
	if(!bFormOK)
	{
		document.getElementById( 'lblFormErrorMessage' ).innerHTML = "Allir rauðmerktir reitir þurfa að vera útfylltir."+strMatchText+strEmailText;
		document.getElementById( 'lblFormErrorMessage' ).style.display = "inline";
		document.getElementById( 'lblFormErrorMessage' ).style.color = "red";
	}
	else
	{
		document.getElementById( 'lblFormErrorMessage' ).innerHTML = "";
		document.getElementById( 'lblFormErrorMessage' ).style.display = "none";
		document.getElementById( 'lblFormErrorMessage' ).style.color = "";
	}
	
	return bFormOK;
}

function checkEmail (emailStr) 
{
var emailPat= /^(.+)@(.+)$/
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\,\\[\\]áðéíóúýþæöÁÐÉÍÓÚÝÞÆÖ'"
var validChars="\[^\\s" + specialChars + "\]"
var quotedUser="(\"[^\"]*\")"
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
var atom=validChars + '+'
var word="(" + atom + "|" + quotedUser + ")"
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
var matchArray=emailStr.match(emailPat)

if (matchArray==null) 
{
	return false;
}
var user=matchArray[1]
var domain=matchArray[2]

if (user.match(userPat)==null) 
{
        return false;
}
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) 
{
    // this is an IP address
	for (var i=1;i<=4;i++) 
	{
	    if (IPArray[i]>255) 
	    {
			return false
	    }
    }
    return true
}
var domainArray=domain.match(domainPat)
if (domainArray==null) 
{
    return false
}
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   return false
}

if (len<2) 
{
   
   return false
}
return true;
}

