YAHOO.namespace("domark");
var successFunc = "global";
var bcolor = "1px solid #E32129";
var dispMsg = "off";
var tipMsg = false;
var showPop = false;
var redirectPage;
var dform;
var yuistat = '';
var yuipop = '';
var yform = '';
var popname = '';
var init = function() {	
	YAHOO.util.Dom.get('waitDiv').style.display = '';
	YAHOO.domark.wait = new YAHOO.widget.Dialog("waitDiv", { width : "250px", height : "70px", 
															  fixedcenter : true, 
															  visible : false,  
															  constraintoviewport : false,
															  draggable : false,
															  modal : true,
															  zIndex : 999999,
															  close : true
															 } );
	YAHOO.domark.wait.render();
	YAHOO.domark.wait.hide();
	
};
YAHOO.util.Event.addListener(window, "load", init);
var genFailure = function(o){
	if(o.responseText !== undefined){
		alert("Error interacting with server.\n\nHTTP status: " + o.status + "\nStatus code message: " + o.statusText);
	} else
		alert("Error interacting with server.");
}


function showBox() {
	if(showPop!='')
		eval('showPop').show();
}

function submitForm(form, actPage, redirect, manual, upload, yui ) {	
	dform = form;
	yuistat = yui;
	eraseMark();   
	YAHOO.domark.wait.show();
	YAHOO.util.Connect.setForm(form, upload);
	if(!actPage) return;
	redirectPage = redirect;
	if(manual) successFunc = "local";
	var request = YAHOO.util.Connect.asyncRequest('POST', actPage + "&nohtml="+Math.random(), actHandle);
	//document.location.href = redirectPage;
}

var actSuccess = function(o){
	if(o.responseText !== undefined){
		var root = o.responseXML.documentElement;
		YAHOO.domark.wait.hide();		
		if (root.nodeName == 'error') {				
			for (i in dform.elements)
				if (dform.elements[i] && dform.elements[i].tagName && (dform.elements[i].tagName == 'INPUT' )) {	
					if (root.getElementsByTagName(dform.elements[i].id).length != 0) {
							//if(yuistat != '') 
							dform.elements[i].style.border = bcolor;								
							//else doMark(dform.elements[i].id, value(root, dform.elements[i].id));
							
					}
				}
			var err = new Array();
			for (i = 0; i < root.childNodes.length; i++) {
				if (root.childNodes[i].nodeName != '#text' && root.childNodes[i].hasChildNodes) {	
					// if(yuistat != '' &&  yuistat != undefined) Edited for the searchresults
					if((yuistat != '' &&  yuistat != undefined) || yuistat == true)
						errHandler.call('content', root.childNodes[i].nodeName, root.childNodes[i].firstChild.nodeValue);
					str = root.childNodes[i].firstChild.nodeValue;
					err.push(str.replace(/\\'/g,"'"));
					
				}	
			}			
			alert("Some required details are missing from the information you provided:\n\n"+err.join("\n"));			
			if(yuistat != '' &&  yuistat != undefined)
				eval("YAHOO.userwin."+yuistat).show();


		} else {
			if(successFunc == "local") processResult(o);
			else { 
				alert(value(root, 'msg'));
				//alert(redirectPage);
				document.location.href = redirectPage;
			}
		}
	}
	
}
var actHandle =
{ 
	upload:actSuccess,
	success:actSuccess,
	failure:genFailure
};


function yuiEdit(param, form, pname) {	
	eraseMark();
	yform = form;
	popname = pname;	
	var request = YAHOO.util.Connect.asyncRequest('GET', "./?"+param+"&nohtml="+Math.random(), editHandle);	
}
function errHandler(iname, context) {	 
	  //alert(iname);
	$('#'+ iname).qtip({
		content: context,
		position: {
			corner: {
				 target: 'rightMiddle',
				 tooltip: 'leftMiddle'
			}
		},
	 show: {
				  effect: 'slide',
				  when: false, // Don't specify a show event
				  ready: true // Show the tooltip when ready
			   },
		hide: { when: { event: 'unfocus' } }   ,
		style: {
			border: {
			width: 1,
			radius: 2
			},
			padding: 3, 
			textAlign: 'center',
			tip: true, // Give it a speech bubble tip with automatic corner detection
			name: 'cream' // Style it according to the preset 'cream' style
		}

		
	});
  }

var editSuccess = function(o){

	if(o.responseText !== undefined){		
		var root = o.responseXML.documentElement;
		
		form = eval("document."+yform);
		if (root.nodeName == 'error') {
			alert(root.firstChild.nodeValue);
			document.location.href = document.location.href;
		} else {
			for (i in form.elements)
				if (form.elements[i] && form.elements[i].tagName && form.elements[i].tagName == 'INPUT' && (form.elements[i].type == 'text' || form.elements[i].type == 'password')) {
					color = '';
					form.elements[i].style.borderColor = color;
					form.elements[i].style.color = color;
				}
			for (i = 0; i < root.childNodes.length; i++) {
				if (root.childNodes[i].nodeName != '#text' && root.childNodes[i].firstChild)
					eval("document."+yform+"."+root.childNodes[i].nodeName).value = root.childNodes[i].firstChild.nodeValue;
					
			}	
			
			eval("YAHOO.userwin."+popname).show();
		}
	}
}
var editHandle =
{
  success:editSuccess,
  failure:genFailure
}

function value(root, node) {
 	return (root.getElementsByTagName(node) && root.getElementsByTagName(node)[0].hasChildNodes && root.getElementsByTagName(node)[0].firstChild && root.getElementsByTagName(node)[0].firstChild.nodeValue)?root.getElementsByTagName(node)[0].firstChild.nodeValue:'';
}