function trim(s) {

    if((s==null)||(typeof(s)!='string')||!s.length) {
        return '';
    }
    return s.replace(/^\s+/,'').replace(/\s+$/,'');

}

function check_blank(field) {
    
    var stripped = trim(field.replace(/(<([^>]+)>)/ig,""));
    stripped = stripped.replace(/&nbsp;/ig,"");

    if (field == '') {
        return false;
    }
    if (trim(field) == '') {
        return false;
    }
    if (stripped == '') {
	return false;
    }
    return true;
}

function validate_login() {
    
    var userName = document.form1.userName.value;
    var password = document.form1.password.value;

    if (!check_blank(userName)) {
	alert ('User Name cannot be blank.');
	return false;
    }
    else if (!check_blank(password)) {
	alert ('Password cannot be blank.');
	return false;
    }

    return true;
}

function validate_content() {
   
    var pageID = document.form1.pageID.value;
    var typeID = document.form1.typeID.value;
    if (pageID == 0) {
	var header = document.form1.header.value;
	var slogan = document.form1.slogan.value;
	var mEditor = FCKeditorAPI.GetInstance('mandate');
        var mDOM = mEditor.EditorDocument;
        var mandate = '';
        if (document.all) {
	    mandate = mDOM.body.innerText;
        }
        else {
	    var m = mDOM.createRange();
	    m.selectNodeContents(mDOM.body);
	    mandate = m.toString(); 
        }
	
	if (!check_blank(header)) {
	    alert('Header cannot be blank.');
	    return false;
	}
	else if (!check_blank(slogan)) {
	    alert('Slogan cannot be blank.');
	    return false;
	}
	else if (!check_blank(mandate)) {
	    alert('Mandate cannot be blank.');
	    return false;
	}

	return true;
    }
    else {
        var title = document.form1.title.value;
        var oEditor = FCKeditorAPI.GetInstance('content');
        var oDOM = oEditor.EditorDocument;
        var content = '';
        if (document.all) {
	    content = oDOM.body.innerText;
        }
        else {
	    var r = oDOM.createRange();
	    r.selectNodeContents(oDOM.body);
	    content = r.toString(); 
        }

        if (!check_blank(title)) {
	    alert('Title cannot be blank.');
	    return false;
    	}
   	else if (!check_blank(content)) {
	    alert('Content cannot be blank.');
	    return false;
    	}

	if (typeID == 1) {
	    var image = document.form1.image.value;
	    var video = document.form1.flash.value;
	    if (check_blank(image) && check_blank(video)) {
		alert('Your image will override your video since only one can be displayed on the page.');
	    }
	}

        return true;
    }
}

function validate_quicklink() {
    
    var title = document.form1.title.value;
    var pageID = document.form1.pageID.value;

    if (!check_blank(title)) {
	alert('Title cannot be blank.');
	return false;
    }
    else if (pageID == '0') {
	alert('You must select a Linked Page.');
	return false;
    }

    return true;
}

function validate_user() {
    
    var userName = document.form1.userName.value;
    var password = document.form1.password.value;

    if (!check_blank(userName)) {
	alert('User Name cannot be blank.');
	return false;
    }
    else if (!check_blank(password)) {
	alert('Password cannot be blank.');
	return false;
    }

    return true;
}

function validate_hotspot() {
    
    var oEditor = FCKeditorAPI.GetInstance('hContent');
    var oDOM = oEditor.EditorDocument;
    var content = '';
    if (document.all) {
        content = oDOM.body.innerText;
    }
    else {
 	var r = oDOM.createRange();
	r.selectNodeContents(oDOM.body);
	content = r.toString(); 
    }

    if (!check_blank(content)) {
	alert('Content cannot be blank.');
	return false;
    }

    return true;
}

function validate_add_page() {
    
    var title = document.form1.title.value;
    var typeID = document.form1.typeID.value;
    var oEditor = FCKeditorAPI.GetInstance('content');
    var oDOM = oEditor.EditorDocument;
    var content = ''
    if (document.all) {
        content = oDOM.body.innerText;
    }
    else {
 	var r = oDOM.createRange();
	r.selectNodeContents(oDOM.body);
	content = r.toString(); 
    }

    if (!check_blank(title)) {
	alert('Title cannot be blank.');
	return false;
    }

    else if (!check_blank(content)) {
	alert('Content cannot be blank.');
	return false;
    }
    
    if (typeID == 2) {
	var hotspot = document.form1.hotspot.value;
	var hContent = document.form1.hContent.value;
	if (!check_blank(hotspot)) {
	    alert('You must upload a hot spot image.');
	    return false;
	}
	else if (!check_blank(hContent)) {
	    alert('Hot spot content cannot be blank.');
	    return false;
	}
    }
    
    return true;
}

function validate_quicklink() {
    
    var title = document.form1.title.value;

    if (!check_blank(title)) {
	alert('Title cannot be blank.');
	return false;
    }

    return true;
}

function validate_contact() {
    
    var comments = document.form1.comments.value;
    var name = document.form1.name.value;
    var email = document.form1.email.value;
    var captcha = document.form1.captcha.value;

    if (!check_blank(name)) {
	alert('Name cannot be blank.');
	return false;
    }

    if (!check_blank(email)) {
	alert('Email cannot be blank.');
	return false;
    }

    if (!echeck(email)) {
	alert('Your email address is invalid.');
	return false;
    }

    if (!check_blank(comments)) {
	alert('Comments cannot be blank.');
	return false;
    }

    if (!check_blank(captcha)) {
	alert('You must input what you see in the image.');
	return false;
    }

    return true;
}
