/* 
******************************************************************
FILE NAME: common_javascript.php		  DATE CREATED: 01/10/2006
******************************************************************
FILE PURPOSE:
******************************************************************
OUTSTANDING ISSUES: None.
******************************************************************
MODIFICATION HISTORY: DATE, NAME, REASON, MODIFICATION
16/04/08, Nadeem, tidy code, code formatting adjustment
20/10/08, Nadeem, need ajax ,add http request functionality
******************************************************************
*/

var http = create_request_object(); 

function create_request_object() { 
	if(window.XMLHttpRequest) {
		// client use Firefox, Opera etc - Non Microsoft product
		try {
			MyHttpRequest = new XMLHttpRequest();
		}
		catch(e) {
			MyHttpRequest = false;
		}
	}
	else if(window.ActiveXObject) {
		// client use Internet Explorer
		try {
			MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try{
				MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				MyHttpRequest = false;
			}
		}
	}
	else{
		MyHttpRequest = false;
	}
	if(!MyHttpRequest) {
		alert("cant create activex htttp request object");
	}
	return MyHttpRequest;
}

function get_brand_types_for_proect_admin(brand) {  
	http.open("get", "../common/AJAX/get_brand_types.php?brand=" + brand , true); 
	http.onreadystatechange = get_brand_types_for_proect_admin_response; 
	http.send(null);
} 
function get_brand_types_for_proect_admin_response() { 
	if(http.readyState == 4 && http.status == 200){ 
		var response = http.responseText;
		if(response) { 
			document.getElementById("project_type").innerHTML = response;
		}
	}
}

function validate_add_project_form(thisform){
	with (thisform){
		if (validate_building_type_one_or_more('project_type[]')==false){
			return false;
		}
		if (validate_required(project_teamleader,"You must add a 'team leader'.")==false){
			project_teamleader.focus();
			return false;
		}
	}
}
function validate_name(field, alerttxt){
	with (field){
		doubleQotPos = value.lastIndexOf("\"");
		
		if (value == null || value == "" || doubleQotPos >= 0){
			alert(alerttxt);
			return false;
		} else { return true; }
	}
}
function validate_required(field, alerttxt){
	with (field){
		doubleQotPos = value.lastIndexOf("\"");
		//singleQotPos = value.lastIndexOf("'");
		if (value == null || value == "" || doubleQotPos >= 0 || singleQotPos >= 0){
			alert(alerttxt);
			return false;
		} else { return true; }
	}
}
function validate_email(field, alerttxt){
	with (field){
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		doubleQotPos = value.lastIndexOf("\"");
		//singleQotPos = value.lastIndexOf("'"); || singleQotPos >= 0
		
		if (apos < 1 || dotpos-apos < 2 || value == null || value == "" || doubleQotPos >= 0){
			alert(alerttxt);
			return false;
		} else { return true; }
	}
}

function validate_building_type(checkbox_name){
	// get all the checkboxes rember getElementSSSSSS
	var cb = document.getElementsByName(checkbox_name);
	var count = 0;
	
    // loop through them
	for(var i=0;i<cb.length;i++)
	{
        if(cb[i].checked == true) count++;
	}
	if(count == 0) {
		cb[0].focus();
		alert("Please select a 'Building Type'");
		return false;
	}
}
function validate_building_type_one_or_more(checkbox_name){
	// get all the checkboxes rember getElementSSSSSS
	var cb = document.getElementsByName(checkbox_name);
	var count = 0;
	
    // loop through them
	for(var i=0;i<cb.length;i++)
	{
        if(cb[i].checked == true) count++;
	}
	if(count > 1 || count < 1) {
		cb[0].focus();
		alert("You must select one 'Building Type'");
		return false;
	}
}
function validate_add_users_form(thisform){
	with (thisform){
		if (validate_required(users_name,"Please enter a User name in the form 'Firstname Lastname.'")==false){
			users_name.focus();
			return false;
		}
		if (validate_required(users_password,"Please enter a password.")==false){
			users_password.focus();
			return false;
		}
		if(users_password.value.length < 8) {
			alert("For security, your password must be at least 8 characters long.");
			users_password.focus();
			return false;
		}
		if (validate_email(users_email,"Please enter a valid email address.")==false){
			users_email.focus();
			return false;
		}
		
		if(users_expires_day.value == "") {
			alert("Please set the account expiry day.");
			users_expires_day.focus();
			return false;
		}
		
		if(users_expires_month.value == "") {
			alert("Please set the account expiry month.");
			users_expires_month.focus();
			return false;
		}
		
		if(users_expires_year.value == "") {
			alert("Please set the account expiry year.");
			users_expires_year.focus();
			return false;
		}
		
		if(users_group.value == "") {
			alert("Please select a Group for this user.");
			users_group.focus();
			return false;
		}
	}
}
function show_users_emailed(users_emailed) {
	/*	w = window.open('','mywin', 'left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
		w.document.write(
						 "<html>" +
						 "<head>" +
						 "<title>Users Emailed</title>" +
						 "</head>" +
						 "<body>" +
						 users_emailed +
						 "</body>" +
						 "</html>"
						 );
		w.focus();*/
		if(document.getElementById("show_users_emailed_div")){
			document.body.removeChild(document.getElementById("show_users_emailed_div"));
		}
		else {
			if(users_emailed.length > 8){
				spinnerdiv = document.createElement('div');
				spinnerdiv.setAttribute('id', 'show_users_emailed_div');
				spinnerdiv.setAttribute('name', 'show_users_emailed_div');
				spinnerdiv.setAttribute('class', 'users_emailed');
				spinnerdiv.setAttribute('className', 'users_emailed');
				spinnerdiv.innerHTML = "<input type='button' value='Close' onClick='show_users_emailed(null)'>"+ 
				"<div class='users_emailed_inner'>"+
				users_emailed+ 
				"<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>"+
				"</div>";
				document.body.appendChild(spinnerdiv);
			}
		}
}
function openwindow(path){
  	open(path,'windowname','width=800,height=600,scrollbars,resizable,toolbar,status');
    //w.document.write("This is a new window");
	//w.document.write("<img src='" + document.getElementById('building_image').value + "'>");
	//w.document.write(document.getElementById('building_image').value);
}

function display_thread_textarea(postback_addr) {
	if(!document.getElementById('threadform')) {
		form = document.createElement('form');
		form.setAttribute('id', 'threadform');
		form.setAttribute('method', 'post');
		form.setAttribute('action', postback_addr);
	
		textarea = document.createElement('textarea');
		textarea.setAttribute('name', 'thread');
		textarea.setAttribute('rows', '8');
		textarea.setAttribute('cols', '80');
		textarea.setAttribute('class', 'threadadd');
		textarea.setAttribute('className', 'threadadd');
			
		submitbtn = document.createElement('input');
		submitbtn.setAttribute('name', 'submit');
		submitbtn.setAttribute('type', 'submit');
		submitbtn.setAttribute('value', 'Create new topic');
		submitbtn.setAttribute('class', 'threadaddbutton');
		submitbtn.setAttribute('className', 'threadaddbutton');
		
		document.getElementById('add_thread_area').appendChild(form);
		document.getElementById('threadform').appendChild(textarea);
		document.getElementById('threadform').appendChild(submitbtn);
		
		document.getElementById('newthread').style.display = "none";
	}
}

function display_thread_textarea_reply(postback_addr, div_id) {
	if(!document.getElementById('threadmessage'+div_id)) {
		form = document.createElement('form');
		form.setAttribute('id', 'threadmessage'+div_id);
		form.setAttribute('method', 'post');
		form.setAttribute('action', postback_addr);
	
		textarea = document.createElement('textarea');
		textarea.setAttribute('name', 'message');
		textarea.setAttribute('rows', '8');
		textarea.setAttribute('cols', '80');
		textarea.setAttribute('class', 'threadadd');
		textarea.setAttribute('className', 'threadadd');
		
		hidden = document.createElement('input');
		hidden.setAttribute('name', 'thread_id');
		hidden.setAttribute('type', 'hidden');
		hidden.setAttribute('value', div_id);
		
		submitbtn = document.createElement('input');
		submitbtn.setAttribute('name', 'submit');
		submitbtn.setAttribute('type', 'submit');
		submitbtn.setAttribute('value', 'Reply');
		submitbtn.setAttribute('class', 'threadaddbutton');
		submitbtn.setAttribute('className', 'threadaddbutton');
		
		document.getElementById('add_message_area_'+div_id).appendChild(form);
		document.getElementById('threadmessage'+div_id).appendChild(textarea);
		document.getElementById('threadmessage'+div_id).appendChild(submitbtn);
		document.getElementById('threadmessage'+div_id).appendChild(hidden);
		
		document.getElementById('button_'+div_id).style.display = "none";
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

//Search boxes
function checksearch() {
	if (document.searchform.query.value == "Search") {
		alert("Please enter a search word before pressing 'GO'.");
		return;
	}
	else if (document.searchform.query.value == "") {
		alert("Please enter a search word before pressing 'GO'.");
		return;
	}
	else {
		showsearchloading();
		document.searchform.submit();
	}
}                 

function showsearchloading() {
	//setTimeout(showsearchloading,1000); 
	if (! document.getElementById('spinnerdiv') || ! document.getElementById('searchimage')) {
		
		spinnerdiv = document.createElement('div');
		spinnerdiv.setAttribute('id', 'spinnerdiv');
		spinnerdiv.setAttribute('class', 'spinner');
		spinnerdiv.setAttribute('className', 'spinner');
		spinnerdiv.innerHTML = "<h1>Searching ...</h1>";
		
		//spinnerimg = document.createElement('img');
		//spinnerimg.setAttribute('src', 'images/common/loader.gif');
		//spinnerimg.setAttribute('id', 'searchimage');
		
		document.body.appendChild(spinnerdiv);
		//document.getElementById('spinnerdiv').appendChild(spinnerimg);
	}
}

function submitsearch() {
}

function clearsearch() {
	if (document.searchform.query.value == "Search") {
		window.document.searchform.query.value = "";
	}
}

function fillsearch() {
	if (document.searchform.query.value == "") {
		window.document.searchform.query.value = "Search";
	}
}
function logincheck() {
	if (loginform.name.value == "") {
		alert("Please enter your name before pressing 'GO'.");
	}
	else if (loginform.password.value == "") {
		alert("Please enter your password before pressing 'GO'.");
	}
	else {
		document.loginform.submit();
	}
}
function reloader(){
//	document.form["theform"].submit();
	document.getElementsByName('theform')[0].submit();
}
function submit_form(formname){
	document.getElementsByName(formname)[0].submit();
}


function openComment(id, thelink, type) {
	url = "comment.php?id="+id+"&link="+thelink+"&type="+type;
	window.open(url, "CommentWin", "scrollbars, resizable");
}

function formviewer(whichLayer) {
	if (document.getElementById) {
		// Real browsers
		var style2 = document.getElementById(whichLayer).style;
		style2.display = style2.display? "":"block";
	}
	else if (document.all) {
		// OLD MSIE Hack
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if (document.layers) {
		// Old NN4 Hack
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}
function feedback(feedbacktext){
	if ((feedbacktext != null) && (feedbacktext != "")) {
		alert(feedbacktext);
	}
}

function movie_popup(title, file) {
	win = window.open("movie.php?title=" + title + "&file=" + file, "movie_popup", "HEIGHT=420, WIDTH=360, resizable");
	win.focus();

}
function help_popup() {
	win = window.open("help.php", "help_popup", "HEIGHT=420, WIDTH=500, scrollbars, resizable");
	win.focus();

}
function term_pop() {
	url = "tandc.php";
	window.open(url, "TandCWin", "HEIGHT=420, WIDTH=360, scrollbars, resizable");
}
function accept_popup(file) {
	win = window.open("accept.php?file=" + file, "accept_popup", "HEIGHT=420, WIDTH=360, resizable");
	win.focus();

}
function add_it(form_name, element_used, hidden_element, container_id, new_div_prefix) {

	//Sort out the values
	var formelement = eval("document." + form_name +"." + element_used);
	var hiddenelement = eval("document." + form_name +"." + hidden_element);
	var thevalue = formelement.value;
	var thetext = formelement.options[formelement.selectedIndex].text;

	if(thevalue != null) {
		//Set the hidden value for updating
		if(hiddenelement.value == "") hiddenelement.value = "," + thevalue + ",";
		else hiddenelement.value += thevalue + ",";
	
		//Add the text to the page
		var container_div = document.getElementById(container_id);
		var new_div = document.createElement("div");
		var div_id = new_div_prefix + thevalue;
		new_div.setAttribute("id", div_id);
		new_div.setAttribute("class", "itemtext");
		new_div.innerHTML = thetext + " <a href=\"javascript:remove_element('" + form_name + "', '" + hidden_element + "', '" + container_id + "', '" + div_id + "', '" + thevalue + "')\">Remove</a>";
		container_div.appendChild(new_div);
	}
}

function remove_element(form_name, hidden_element, container_id, div_id, id) {
	
	//Sort out the values
	var hiddenelement = eval("document." + form_name +"." + hidden_element);
	var hiddenelementvalue = hiddenelement.value;

	//Set the hidden value for updating and clean up
	var regular_only = eval("/^" + id + "$/");
	var regular_only_commas = eval("/^," + id + ",$/");
	var regular_start = eval("/^" + id + "[^0-9]/");
	var regular_middle = eval("/[^0-9]" + id + "[^0-9]/");
	var regular_end = eval("/[^0-9]" + id + "$/");

	var new_hiddenelementvalue = hiddenelementvalue.replace(regular_only, "");
	new_hiddenelementvalue = new_hiddenelementvalue.replace(regular_only_commas, "");
	new_hiddenelementvalue = new_hiddenelementvalue.replace(regular_start, "");
	new_hiddenelementvalue = new_hiddenelementvalue.replace(regular_middle, ",");
	new_hiddenelementvalue = new_hiddenelementvalue.replace(regular_end, "");
	hiddenelement.value = new_hiddenelementvalue;
	
	//Remove text from list
	var container_div = document.getElementById(container_id);
	var div_removed = document.getElementById(div_id);
	container_div.removeChild(div_removed);
}
function edit_file(openedby, id) {
	win = window.open("projectfiles.php?project_id=" + openedby + "&id=" + id, "edit_file", "HEIGHT=480, WIDTH=760, resizable=yes, scrollbars=yes, status=yes");
	win.focus();

}
function ccsubmitresize() {
	document.ccresizeform.submit();
	showccprocessing();
}
function showccprocessing() {
	//setTimeout(showsearchloading,1000); 
	if (! document.getElementById('spinnerdiv') || ! document.getElementById('searchimage')) {
		
		spinnerdiv = document.createElement('div');
		spinnerdiv.setAttribute('id', 'spinnerdiv');
		spinnerdiv.setAttribute('class', 'spinner');
		spinnerdiv.setAttribute('className', 'spinner');
		spinnerdiv.innerHTML = "<h1>Processing ...</h1>";
	
		document.body.appendChild(spinnerdiv);
	}
}
