function locator(a, b, exp) {
    if (exp == true) {
        var newLocation = location.href.replace(a, b);
        alert(newLocation);
        window.location.href = newLocation;
    }
}

function relocate(exp) 
{
    if (exp)
    {   
        //window.location.href.substring(window.location.href.indexOf('?'))    
        //var mySplitResult = window.location.href.split("?");
        if (window.location.href.indexOf('?') !=-1){
            window.location.href = "Default2.aspx" + window.location.href.substring(window.location.href.indexOf('?'));
        }
        else{
            window.location.href = "Default2.aspx"
        }
    }
}


function gotoComplaints()
{
    var home = window.opener;
    var complaintsPage = "http://www.southampton.gov.uk/customer-service/comments/";
    if (home != null)
    {
        window.opener.location = complaintsPage;
        window.close();
    }
    else
    { 
        window.location = complaintsPage;
        window.moveTo(0,0);
        window.resizeTo(screen.width, screen.height);
    }    
}

function resizeRelocate()
{
    var width = 800;
    var height = 770;
    top.resizeTo(width, height);
    self.moveTo(75, 5);
    //document.getElementById("uxPageReviewed").value = window.opener.document.title;
    //window.opener.document.bgColor = "beige";
    try
    {
        if ( window.opener.document )
    {
        //alert('found');
        document.getElementById("uxPageReviewed").value = window.opener.document.title;
    }
    }
    catch(e)
    {
        alert('Error: No parent window');
    }
    
}

//Mike
function emailValidation()
{
    var emailText = document.getElementById("uxemail").value;
    var isEmailOkay = isEmailValid(emailText);    
    
    if (isEmailOkay)
    {
        document.getElementById("lblEmail").style.color = "#000000";
        document.getElementById("emailValidation").innerHTML = "";
        //lblEmail
    }
    else
    {
        document.getElementById("lblEmail").style.color = "#ff0000";		
        document.getElementById("emailValidation").innerHTML = "Email must be in correct format";
        document.getElementById("uxemail").focus();
    }
    
    if ((document.getElementById("uxWantResponse").checked) && (emailText.length == 0))
    {
        isEmailOkay = false;
        document.getElementById("lblEmail").style.color = "#ff0000";		
        document.getElementById("emailValidation").innerHTML = "Email field must be filled in if you would like a response to this feedback";
        document.getElementById("uxemail").focus();
    }
    
    return isEmailOkay;
}

//Mike
function overAllValidation()
{
    var isOverallChecked = VerifyOverallChecked();
    var isCommentGiven = CheckIfCommentGiven();    
	
    DisplayOverallValidation(isOverallChecked);		
	DisplayCommentValidation(isCommentGiven);
	
    toggleValidationDisplay(isCommentGiven, isOverallChecked);
	
	return isOverallChecked && isCommentGiven;
}

function VerifyOverallChecked()
{
	for (counter = 0; counter < document.getElementsByName("uxOverallRating").length; counter++) 
	{
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.getElementsByName("uxOverallRating")[counter].checked) 
		{		    
			isOverallChecked = true;
			return true;
		}
	}	
    
    return false;
}

function toggleValidationDisplay(isCommentGiven, isOverallChecked)
{
    if(isOverallChecked)
    {
	    document.getElementById("idOverallRating").style.color = "#000000";
	}
	else
	{
	    document.getElementById("idOverallRating").style.color = "#ff0000";
	}
	
	if(isCommentGiven) //idWebPageComment
	{
	    document.getElementById("idWebPageComment").style.color = "#000000";
	}
	else
	{
	    document.getElementById("idWebPageComment").style.color = "#ff0000";
	}
}

function DisplayOverallValidation(isOverallChecked)
{
	if (isOverallChecked == false) 
	{
		document.getElementById("overallvalidation").innerHTML = "An overall rating must be chosen.";
	}
	else
	{	 
	    document.getElementById("overallvalidation").innerHTML = "";
	}
}

function DisplayCommentValidation(isCommentGiven)
{
    if(! isCommentGiven)
    {
		document.getElementById("overallvalidation").innerHTML += "  A comment must be entered.";
    }    
}

function CheckIfCommentGiven()
{
    var commentText = document.getElementById("uxCommentText").value;
    commentText.replace(/^\s+|\s+$/g,"");
    return (commentText.length > 0);
}

//Mike
function isEmailValid(string)
{
	if(string.length == 0)
	{
		return true;
	}
	
	if(string.length > 50)
	{
	    return false;
	}

	return (string.search(/^[a-zA-Z]+([_\.-]?[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([\.-]?[a-zA-Z0-9]+)*(\.[a-zA-Z]{2,4})+$/) != -1);
}

function maxChars(source) {
    var data = document.getElementById(source).value
    if (document.getElementById(source).value.length > 1000) {
        document.getElementById(source).value = data.substr(0, 1000);
    }
}

function validation() 
{
    var emailValid = emailValidation();
    var overallValid = overAllValidation();
    
    return (emailValid && overallValid);
}

function disableButton(theButton) 
{   
    //check validation
    if (!validation()) 
    {
        return false;
    }

    //Do page disabling here 
    $find('ModalPopupExtender1').show();

    //prevents double submitting
    theButton.disabled = true;

    //give the user a chance to read the message
    //also disrupt down possible automated submitting
    setTimeout("finishRequest()", 3500);
}

function finishRequest()
{
    //submit the form
    __doPostBack('', '');

    //by default, gifs do not animate during a postback
    animateSpinner();
}

function animateSpinner() 
{    
    document.images["Spinner"].src = "images/Indicator_Big.gif";  
}
