/*
	(c) KERVE CREATIVE
	
	AREA: ADMIN
	TYPE: STANDARD FUNCTIONS JAVASCRIPT
	FOR: BRAMBLE AND BLOOM
	VERSION: v1.1
	UPDATED: 2010-06-3
*/

function openWinow(url,winName,popwidth,popheight,popleft,poptop) {
	var new_window = window.open(url,winName,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=' + popwidth + ',height=' + popheight + ',top=' + poptop + ',left=' + popleft);
}

function gotoURL(url) {
	document.location = url;
}

function makeDebugAlertFunction(forcepopup,string) {
	forcepopup = null;
	
	//Check to see if your forcing an alert to be popup, for instance in the ajax cart when there is an error returned you would want to see it, but not standard debug returns
	//Or if forcepopup is false then see if javascript debugging is set to true, if so then alert any JS that has this function in use
	if(forcepopup == true) {
		alert(string);
	} else {
		if(debug_js_alert == true) {
			alert(string);
		}
	}
}

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function var_dump(obj) {
   if(typeof obj == "object") {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } else {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}//end function var_dump

function base64_decode (data) {
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Pellentesque Malesuada
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'

    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof this.window['btoa'] == 'function') {
    //    return btoa(data);
    //}

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, dec = "", tmp_arr = [];

    if (!data) {
        return data;
    }

    data += '';

    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);

    dec = tmp_arr.join('');
    dec = this.utf8_decode(dec);
	
    return dec;
}

function utf8_decode ( str_data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'

    var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
    
    str_data += '';
    
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
	
    return tmp_arr.join('');
}

function readForm(theForm) {
	var elems = document.getElementById(theForm).elements;
	var status = '';
	for (var ix=0; ix < elems.length; ix++) {
		var elem = elems[ix];
		if(elem.value == '') {
			status = '1';
		}
	}
	
	if(status == '1') {
		return false;
	} else {
		return true;
	}
}

function validateContactForm() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
		(document.getElementById('enquiry_details').value != '') &&
		(document.getElementById('first_name').value != '') &&
		(document.getElementById('last_name').value != '') &&
		(document.getElementById('email').value != 'Enter your email') &&
		(filter.test(document.getElementById('email').value))
	) {
		document.getElementById('contact_form').submit();
	} else {
		if((document.getElementById('first_name').value == '') || (document.getElementById('last_name').value == '')) {
			errormessage += "  > Your name\n";
		}
		if(document.getElementById('email').value == '') {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(document.getElementById('email').value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		if(document.getElementById('enquiry_details').value == '') {
			errormessage += "  > Your enquiry message\n";
		}
		
		alert(errormessage);
	}
}


function validateNewsletterForm() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
		(document.getElementById('job_title').value != '') &&
		(document.getElementById('first_name').value != '') &&
		(document.getElementById('last_name').value != '') &&
		(document.getElementById('email').value != 'Enter your email') &&
		(filter.test(document.getElementById('email').value))
	) {
		document.getElementById('contact_form').submit();
	} else {
		if((document.getElementById('first_name').value == '') || (document.getElementById('last_name').value == '')) {
			errormessage += "  > Your name\n";
		}
		if(document.getElementById('email').value == '') {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(document.getElementById('email').value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		if(document.getElementById('job_title').value == '') {
			errormessage += "  > Your job title\n";
		}
		
		alert(errormessage);
	}
}

function validateSignUp(inputid) {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
		(document.getElementById(inputid).value != '') &&
		(document.getElementById(inputid).value != 'Enter your email') &&
		(filter.test(document.getElementById(inputid).value))
	) {
		document.subFormMain.submit();
	} else {
		if(document.getElementById(inputid).value == '') {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(document.getElementById(inputid).value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		
		alert(errormessage);
	}
}
function validateSignUpMini() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
		(document.icpsignup.fields_email.value != '') &&
		(document.icpsignup.fields_email.value != 'Enter your email') &&
		(filter.test(document.icpsignup.fields_email.value))
	) {
		document.icpsignup.submit();
	} else {
		if(document.icpsignup.fields_email.value == '') {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(document.icpsignup.fields_email.value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		
		alert(errormessage);
	}
}
function validateSignUpMini_cm(inputid) {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
		(document.getElementById(inputid).value != '') &&
		(document.getElementById(inputid).value != 'Enter your email') &&
		(filter.test(document.getElementById(inputid).value))
	) {
		document.subForm.submit();
	} else {
		if(document.getElementById(inputid).value == '') {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(document.getElementById(inputid).value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		
		alert(errormessage);
	}
}


function siteSearch() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
		(document.getElementById('search_field').value != '') &&
		(document.getElementById('search_field').value != 'Search Site')
	) {
		document.getElementById('site_search_form').submit();
	} else {
		if((document.getElementById('search_field').value == '') || (document.getElementById('search_field').value == 'Search Site')) {
			errormessage += "  > Please enter a valid search phrase\n";
		}
		
		alert(errormessage);
	}
}

function peopleSearch(search_form) {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var theform = document.getElementById(search_form);
	
	var user_name = theform.elements.user_name,
	index;
	
	if(
		(user_name.value != '') &&
		(user_name.value != 'name, job, area')
	) {
		theform.submit();
	} else {
		if((user_name.value == '') || (user_name.value == 'name, job, area')) {
			errormessage += "  > Please enter a valid search phrase\n";
		}
		
		alert(errormessage);
	}
}


function validateGradForm() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if(
		(document.getElementById('surname').value != '') &&
		(document.getElementById('firstname').value != '') &&
		(document.getElementById('email_address').value != '') &&
		(filter.test(document.getElementById('email_address').value)) && 
		(document.getElementById('home_address').value != '') &&
		(document.getElementById('contact_address').value != '') &&
		(document.getElementById('telephone').value != '')
	) {
		document.getElementById('grad_form').submit();
	} else {
		if(document.getElementById('email_address').value == '') {
			errormessage += "  > Your email address\n";
		} else {
			if((filter.test(document.getElementById('email_address').value)) == false) {
				errormessage += "  > Your email address must be a valid\n";
			}
		}
		if(document.getElementById('surname').value == '') {
			errormessage += "  > Your Surname\n";
		}
		if(document.getElementById('firstname').value == '') {
			errormessage += "  > Your Firstname\n";
		}
		if(document.getElementById('home_address').value == '') {
			errormessage += "  > Your home address\n";
		}
		if(document.getElementById('contact_address').value == '') {
			errormessage += "  > Your contact address\n";
		}
		if(document.getElementById('telephone').value == '') {
			errormessage += "  > Your telephone number\n";
		}
		
		alert(errormessage);
	}
}


function IsNumeric(sText) {
	var ValidChars = "0123456789.)+( ";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1)  {
			IsNumber = false;
		}
	}
	return IsNumber;
}

// JavaScript Document
$(document).ready(function(){

	$("#fbutton1").mouseover(function() {
		$("#fade1").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton1").mouseout(function() {
				$("#fade1").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});
	$("#fbutton2").mouseover(function() {
				$("#fade2").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton2").mouseout(function() {
				$("#fade2").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});
	$("#fbutton3").mouseover(function() {
				$("#fade3").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton3").mouseout(function() {
				$("#fade3").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});
	$("#fbutton4").mouseover(function() {
				$("#fade4").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton4").mouseout(function() {
				$("#fade4").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});
	$("#fbutton5").mouseover(function() {
				$("#fade5").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton5").mouseout(function() {
				$("#fade5").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});
	$("#fbutton6").mouseover(function() {
				$("#fade6").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton6").mouseout(function() {
				$("#fade6").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});
	$("#fbutton7").mouseover(function() {
				$("#fade7").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton7").mouseout(function() {
				$("#fade7").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});
	$("#fbutton8").mouseover(function() {
				$("#fade8").stop().animate({opacity: 1.0}, 500, "easeInOutQuart");
	});
	$("#fbutton8").mouseout(function() {
				$("#fade8").stop().animate({opacity: 0.0}, 500, "easeInOutQuart");
	});

});
