var PopWin=false;
var Selected=0;
var isChanged=0;  /// if anychanges made in formpage it will become a 'true' or '1'
var requestedDir='';
var actionType="";

function doConfirm(dir)
{
	requestedDir="&rdir="+dir;
	if(isChanged > 0)
	{
		
		if(confirm("Do you want save changes?"))
		{
			
			saveChanges();
			return false;
		}
	}
}

function markAsEdit(e)
{
	//alert(e);
	if(actionType != 'view' )
	{
	
		var tag
			if(document.all)
			tag=e.srcElement.tagName;
			else
				tag=e.target.tagName;
		
		if(tag.toLowerCase()==="input" || tag.toLowerCase()==="select")
		{
			isChanged++;
		}
	}
}

function countSelected(obj)
{
	if(obj.checked)
	{
		Selected++;
	}
	else
	{
		Selected--;
	}
}


function selectWindow()
{
	var success=true;
	try
	{
	if(PopWin)
	{ 
		//alert(typeof(PopWin));
		if(!PopWin.closed)
		{
			//alert('popup exists');
			PopWin.focus();
			//return false;
			success=false;
			
		}
	}
	}
	catch(e)
	{
		success=false;
	}
	return true;
}

function callPopUp(url)
{
	if(actionType != 'view')
	{
		if(PopWin)
		{ 
			if(!PopWin.closed)
			{
				//PopWin.close();
				PopWin.focus();
				
			}
		}
		
		PopWin=window.open(url,'PopWin',"width=100,height=100,menubar=no");
	}
	return false;
}
function getPopUp(thisField,otherTable,otherFields,where,caption)
{
	//alert(thisField);
	document.getElementById(thisField).value=1;
	
	//var yesno=eval("document.forms[0]." + otherFields.substr(0,otherFields.indexOf(",")));
	
	//alert(yesno[0].checked);
	//if(yesno[0].checked)
	//{
		
		var otherField=otherFields.substr(0,otherFields.indexOf(","));
		//alert(otherField);
		if(otherField.indexOf(",")>0)
		{
		otherField1=otherField.substr(0,otherField.indexOf(","));
		}else
		{
			otherField1=otherField;
		}
		//alert(thisField+"_"+otherField1);
		var htmlElement=document.getElementById(thisField+"_"+otherField);	
		//alert(thisField+"_"+otherField1);
		selectedValue=htmlElement.value;
		
		url="RPC/getPopUpdata.php?thisField="+thisField+"&otherTable="+otherTable+"&otherFields="+otherFields+"&selectedValue="+selectedValue+"&where="+where+"&caption="+caption;
		//alert(url);
		getXMLHTTPData(url);
	//}
	/*else
	{
		url="admin.php?sm=oldJanmadartaDetails&type=add";
		callPopUp(url);
	}*/
	
	return false;

}

function showByTag( elmID )
{
	var	ie=document.all;
	if( ie )
	{
		for( i = 0; i < document.all.tags( elmID ).length; i++ )
		{
			obj = document.all.tags( elmID )[i];
			if( !obj || !obj.offsetParent )
			{
				continue;
			}
			obj.style.visibility = "";
		}
	}
}
function hideByTag( elmID )
{
	var	ie=document.all;
	if( ie )
	{
		for( i = 0; i < document.all.tags( elmID ).length; i++ )
		{
			obj = document.all.tags( elmID )[i];
			if( !obj || !obj.offsetParent )
			{
				continue;
			}
			obj.style.visibility = "hidden";
		}
	}
}
function movefocus(field) {
//alert(event.keyCode);
	if(event.keyCode == 13)
	{
		form=field.form;
		fieldName=field.name;
		
	
	  form = typeof form == 'string' ? document[form] : form;
	  var found = false;
	  for (var i = 0; i < form.elements.length; i++)
	    if ((found = form.elements[i].name == fieldName))
	      break;
	  var index=found ? i : -1;
	  form[index+1].focus();
	  return false;
  }
  return true;
}

function makeHide(obj,action)
 {
	
 	if(action)
 	{
 		
 		document.getElementById(obj).style.display='none';
 	}
 	else
 	{
 		document.getElementById(obj).style.display='';
 	}
 	
 }
 
function test()
{
	alert('hello1');
	return false;
	
}
var fields=new Array();
var count=0;
var check=true;
//To store the fields name in fields array
function nothing()
{
	
}
function checkIt(val)
{
	if(actionType != 'view' )
	{
		fields[count]=val;
		count++;
	}
}
//Used to check the empty fields
function checkEmpty(f)
{
	check=true;
	if(actionType != 'view' )
	{
		for(i=0;i<fields.length;i++)
		{
			var fieldname=fields[i];
			var value=f[fieldname].value;
			if(value==null || value.length==0)
			{
				alert(empty_field+" "+fieldname);
				check=false;
				f[fieldname].className='texterror';
				f[fieldname].focus();
				break;
			}
			
		}
		/*if(check)
		{
			eval("f.action='"+cgifile+"'");
			f.submit();
		}*/
	}
}
//Used to check the validity of email
//@ field name


function eCheckEMail(sn){
    s= sn.value;
    if (s.indexOf("@") == -1) return false;
    if (s.indexOf(".") == -1) return false;
    at=false;
    dot=false;
    for (var i = 0; i < s.length; i++) {
        ch = s.substring(i, i + 1)
        if ((ch >= "A" && ch <= "Z") || (ch >= "a" && ch <= "z")
                || (ch == "@") || (ch == ".") || (ch == "_")
                || (ch == "-") || (ch >= "0" && ch <= "9")) {
                if (ch == "@"){
                  if (at) return false;
                  else at=true;
                }
                if ((ch==".") && at)
                   dot=true;
        }
        else return false;
    }
   return dot;
}
function verify(pform)
{
tform=pform;
if(document.getElementById('naam'))
{
	if(document.getElementById('naam').value=="")
	{
		alert("Naam should not be empty. ");
		return false;
	} 
	else if(document.getElementById('emailadres').value=="")
	{
		alert("E-mail adres should not be empty. ");
		return false;
	} 
	else if( document.getElementById('uwvraag').value=="")
	{
		alert("Boodschap not be empty. ");
		return false;
	} 
	checkEmail(tform,'emailadres');
}
	return check;
	
}

function checkEmail(theForm,fname){
	check=true;
	if(actionType != 'view' && !eval("theForm."+fname+".readOnly"))
	{
		
		if(eval("theForm."+fname+".value")=='')
		{
			check=true; 
		}
        else if (!eCheckEMail(eval("theForm."+fname))){
            alert("Email entry is not valid");
            check=false; 
            eval("theForm."+fname+".focus()");
			//alert("test");
			document.getElementById(fname).focus();
        }
	}
 }

//to check Url 
//@ f name of form
//@ fname name of field
function checkUrl(f,fname)
{
	check=true;
	if(actionType != 'view' && !eval("f."+fname+".readOnly"))
	{
		var value=eval("f."+fname+".value");
		if(value!="" && value.length>0)
		{
			var ind_h=value.indexOf("http://");
			var ind_dot=value.indexOf(".");
			
			
			if(ind_h==-1 || ind_dot==-1)
			{
				check=false;
				alert(invalid_url);
				eval("f."+fname+".focus()");
			}
		}
	}
}
//Used to format the no of decimal in given text value
//@ field name 
//@ no of digit after dot
function formatDecimal(f,fname,no)
{
		check=true;
	if(actionType != 'view' && !eval("f."+fname+".readOnly"))
	{
		var value=eval("f."+fname+".value");
		if(checkNumber(value))
		{
			check=true;
		}
		else
		{
			alert(not_a_number);
			check=false;
			//eval("f."+fname+".value=''");
			eval("f."+fname+".focus()");
		}
	}
		
}
function checkInt(f,fname)
{
	var value=eval("f."+fname+".value");
	if(actionType != 'view' && !eval("f."+fname+".readOnly"))
	{
		check=false;
		if(checkNumber(value))
		{
			if(value.indexOf(".")==-1 && value.indexOf(",")==-1)
			{
				check=true;
			}
		}
		if(check==false)
		{
			alert(not_a_number);
		}
	}
}
//To check whether the given value is nubmer or not
//@ text value
function checkNumber(val,extra)
{
	var check=true;
	if(actionType != 'view')
	{
		for(i=0;i<val.length;i++)
		{
			var c=val.charAt(i);
			//alert(c);
			if(!((c>='0' && c<='9')||(c==".")||(c==",")||(c==extra)))
			{
				check=false;
				break;
			}
		}
	}
	return check;
}
//Function to check the date format
//@ f form name
//@ fname field name
function checkDate(f,fname)
{
	check=true;
	if(actionType != 'view' && !eval("f."+fname+".readOnly"))
	{
		var value=eval("f."+fname+".value");
	
		if(checkNumber(value,"-"))
		{
			if(value!="" && value.length>0)
			{
				var beforeindex=value.indexOf("-");
				
				var afterval=value.substring(value.indexOf("-")+1,value.length);
				var afterindex=afterval.indexOf("-");
				if(beforeindex!=4 || afterindex!=2)
				{
					alert(invalid_date_format);
					check=false;
				}
			}
		}
		else
		{
			alert(invalid_date_format);
			check=false;
		}
	}
}
//this function prompts the message to before deleting item 
//@form is the reference of the form
//@loc redirect page
function confirmDel(form,loc)
{
	if(Selected > 0)
	{

	if(confirm(delete_item_message))
	{
		//alert(typeof(form)=='object');
		if(typeof(form)=='object'){
			form.action=loc;
			form.submit();
		}
		else{
			document.forms[0].action=loc;
			document.forms[0].submit();
		}
	}
	}
	else
	{
		alert("No record(s) selected");
		return false;
	}
}
//Redirect the page to the given location 
//@ form reference of form
//@ loc redirect page 
function redirectTo(form,loc)
{
	form.action=loc;
	form.submit();
}
//call the display with select no of items 
function changeSize(form,action)
{
	form.action +="?"+action;
	//alert(form.action);
	form.submit();
}
//call the display with sorted order
//@ index to of start position
//@ index for which position of array to sort
//@ sort type in which way to sort 
function sortA(ind,sortind,stype)
{
	location='display.php?start='+ind+'&sortind='+sortind+'&sorttype='+stype;
}
function resetForm(formname)
{
	eval("formname.reset()");
}
function callForm(action,formname)
{
	
	action=action + requestedDir;

		if(check==true)
	{
		
		eval(formname+".action='"+action+"'");
		eval(formname+".submit()");
	}
	return false
}
/*function showpopup(obj)
{
	vReturnValue = window.showModalDialog("attachlink.php?type=rlink&path=images&file_type=image","","dialogHeight:550px;dialogWidth:500px");
	if(vReturnValue)
		obj.value= vReturnValue;
}*/

function showfilepopup(ban,path,type,thumbsize)
{
	if(PopWin)
	{ 
		if(!PopWin.closed)
		{
			//PopWin.close;
			PopWin.focus();
			
		}
	}
	
	PopWin=window.open("browser/popups/insert_image.php?fileobj="+ban+"&location="+path+"&filetype="+type+"&thumbsize="+thumbsize,"PopWin","width=600,height=450,menubar=no");
	//ban.value = returnValue;

}

function elementFocus(f,fname,cl)
{
	
	if(selectWindow())
	{
		f[fname].className=cl;
	}
	{
		return false;
	}
	//eval("f."+fname+".style.backgroundColor='"+cl+"'");
}
function chageRowColOver(rowname)
{
	
		//eval("document."+rowname+".className=='trhover'");
}
function chageRowColOut(rowname)
{
	
		//eval("document."+rowname+".className=='trnormal'");
}

/*function elementBlur(f,fname)
{
	eval("f."+fname+".class='textbox'");
}*/



	function  showhideCanvas(img,canvasid)
	{
		if(canvasid.className=='show')
		{
			canvasid.className='hide';
			img.src="framework/images/tb_down.gif";
			
		}
		else
		{
			canvasid.className='show';
			img.src="framework/images/tb_up.gif";
		}
	}
	
	
	
	
function getXMLHTTP() {
		//return new ActiveXObject("Msxml2.XMLHTTP");
		var xmlhttp=false;
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
			try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp=false; } }
		@end @*/
		if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest();
		return xmlhttp;
	}
	
	function processReqChange() {
		// only if req shows "loaded"
		if (req.readyState == 4) {
			// only if "OK"
			if (req.status == 200) {
				// ...processing statements go here...
			} else {
				alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}
	}
	
	function GetPage( strURL) {
		var req = getXMLHTTP();
		
		if (req) {
			//req.onreadystatechange = processReqChange;
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {
						// Processing statements go here...
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}
			}
			req.open("GET", strURL, true);
			req.send(null);
		}
	}
	function setDataArea()
	{
		//alert('hello');
		
		var mainAreaWidth=document.getElementById("dataTable").offsetWidth;
		var mainAreaHeight=document.getElementById("dataTable").offsetHeight;
		//alert(mainAreaWidth);
		var dataAreaWidth=mainAreaWidth-10;
			var dataAreaHeight=mainAreaHeight;
		
		//alert(dataAreaWidth);
		
		document.getElementById("dataArea").style.width=dataAreaWidth;
		document.getElementById("dataArea").style.height=dataAreaHeight;
		document.getElementById("dataArea").style.display='';
		
		if(document.getElementById('file_remote_files_id'))
		{
				if(document.all)
			{
				document.getElementById('file_remote_files_id').style.width=mainAreaWidth-40;
				//document.getElementById('local_files_id').style.width=mainAreaWidth-400;
				document.getElementById('file_remote_files_id').style.height=mainAreaHeight-62;
			}else
			{
				
			document.getElementById('file_remote_files_id').style.width=mainAreaWidth-20;
			//document.getElementById('local_files_id').style.width=mainAreaWidth-400;
			document.getElementById('file_remote_files_id').style.height=mainAreaHeight-65;
			}
		}
	}
	
	function getXMLHTTPData(url)
	{
		xmlhttp1=getXMLHTTP();
		xmlhttp1.open('GET',url);
		//alert(url);
		xmlhttp1.onreadystatechange=function() {
			if(xmlhttp1.readyState==4&&xmlhttp1.status==200) {
				//alert(xmlhttp1.responseText);
				eval(xmlhttp1.responseText);
			}
		}
		xmlhttp1.send(null);
	}
	
	
	/////////////// scripts for color picker
	
	function showColorPallet(id, id1)
	{
		oldColor=document.getElementById(id1).value;
		document.getElementById(id).style.display='';
	}
	
	function selectColor(id) {
        document.getElementById(id+"_colorPallet").style.display='none';
    }
    function showColor(color,id) {
    	
        document.getElementById(id+"_box").style.backgroundColor = color;
        document.getElementById(id).value = color;
    }
function printColorPallet(id)
{
	
	var fieldid=id;
	var colorPicker = "";
    var colors = new Array(
        "#000000",
        "#000033",
        "#000066",
        "#000099",
        "#0000cc",
        "#0000ff",
        "#330000",
        "#330033",
        "#330066",
        "#330099",
        "#3300cc",
        "#3300ff",
        "#660000",
        "#660033",
        "#660066",
        "#660099",
        "#6600cc",
        "#6600ff",
        "#990000",
        "#990033",
        "#990066",
        "#990099",
        "#9900cc",
        "#9900ff",
        "#cc0000",
        "#cc0033",
        "#cc0066",
        "#cc0099",
        "#cc00cc",
        "#cc00ff",
        "#ff0000",
        "#ff0033",
        "#ff0066",
        "#ff0099",
        "#ff00cc",
        "#ff00ff",
        "#003300",
        "#003333",
        "#003366",
        "#003399",
        "#0033cc",
        "#0033ff",
        "#333300",
        "#333333",
        "#333366",
        "#333399",
        "#3333cc",
        "#3333ff",
        "#663300",
        "#663333",
        "#663366",
        "#663399",
        "#6633cc",
        "#6633ff",
        "#993300",
        "#993333",
        "#993366",
        "#993399",
        "#9933cc",
        "#9933ff",
        "#cc3300",
        "#cc3333",
        "#cc3366",
        "#cc3399",
        "#cc33cc",
        "#cc33ff",
        "#ff3300",
        "#ff3333",
        "#ff3366",
        "#ff3399",
        "#ff33cc",
        "#ff33ff",
        "#006600",
        "#006633",
        "#006666",
        "#006699",
        "#0066cc",
        "#0066ff",
        "#336600",
        "#336633",
        "#336666",
        "#336699",
        "#3366cc",
        "#3366ff",
        "#666600",
        "#666633",
        "#666666",
        "#666699",
        "#6666cc",
        "#6666ff",
        "#996600",
        "#996633",
        "#996666",
        "#996699",
        "#9966cc",
        "#9966ff",
        "#cc6600",
        "#cc6633",
        "#cc6666",
        "#cc6699",
        "#cc66cc",
        "#cc66ff",
        "#ff6600",
        "#ff6633",
        "#ff6666",
        "#ff6699",
        "#ff66cc",
        "#ff66ff",
        "#009900",
        "#009933",
        "#009966",
        "#009999",
        "#0099cc",
        "#0099ff",
        "#339900",
        "#339933",
        "#339966",
        "#339999",
        "#3399cc",
        "#3399ff",
        "#669900",
        "#669933",
        "#669966",
        "#669999",
        "#6699cc",
        "#6699ff",
        "#999900",
        "#999933",
        "#999966",
        "#999999",
        "#9999cc",
        "#9999ff",
        "#cc9900",
        "#cc9933",
        "#cc9966",
        "#cc9999",
        "#cc99cc",
        "#cc99ff",
        "#ff9900",
        "#ff9933",
        "#ff9966",
        "#ff9999",
        "#ff99cc",
        "#ff99ff",
        "#00cc00",
        "#00cc33",
        "#00cc66",
        "#00cc99",
        "#00cccc",
        "#00ccff",
        "#33cc00",
        "#33cc33",
        "#33cc66",
        "#33cc99",
        "#33cccc",
        "#33ccff",
        "#66cc00",
        "#66cc33",
        "#66cc66",
        "#66cc99",
        "#66cccc",
        "#66ccff",
        "#99cc00",
        "#99cc33",
        "#99cc66",
        "#99cc99",
        "#99cccc",
        "#99ccff",
        "#cccc00",
        "#cccc33",
        "#cccc66",
        "#cccc99",
        "#cccccc",
        "#ccccff",
        "#ffcc00",
        "#ffcc33",
        "#ffcc66",
        "#ffcc99",
        "#ffcccc",
        "#ffccff",
        "#00ff00",
        "#00ff33",
        "#00ff66",
        "#00ff99",
        "#00ffcc",
        "#00ffff",
        "#33ff00",
        "#33ff33",
        "#33ff66",
        "#33ff99",
        "#33ffcc",
        "#33ffff",
        "#66ff00",
        "#66ff33",
        "#66ff66",
        "#66ff99",
        "#66ffcc",
        "#66ffff",
        "#99ff00",
        "#99ff33",
        "#99ff66",
        "#99ff99",
        "#99ffcc",
        "#99ffff",
        "#ccff00",
        "#ccff33",
        "#ccff66",
        "#ccff99",
        "#ccffcc",
        "#ccffff",
        "#ffff00",
        "#ffff33",
        "#ffff66",
        "#ffff99",
        "#ffffcc",
        "#ffffff"
    );

    colorPicker += '<table border="0" cellspacing="1" cellpadding="0" bgcolor=\"#ffffff\">'
    	+ '<tr bgcolor=\"#cccccc\"><td colspan=\"18\" align=\"right\"><input type=\"image\" src=\"images/top_close.gif\" border=\"1\" onclick=\"javascript:selectColor(\''+fieldid+'\');document.getElementById(\''+fieldid+'\').value=oldColor; document.getElementById(\''+fieldid+'_box\').style.backgroundColor=oldColor;return false;\" align=\"absmiddle\" /></td></tr>'
        + '<tr>';
        
    for (var i=0; i<colors.length; i++) {
        colorPicker += '<td bgcolor="' + colors[i] + '">'
            + '<a href="#top" onclick="selectColor(\''+fieldid+'\');return false;" onmouseover="showColor(\'' + colors[i] +  '\',\''+fieldid+'\');">'
            + '<img border="0" src="images/spacer.gif" width="10" height="10" /></a></td>';
        if ((i+1) % 18 == 0)
            colorPicker += '</tr><tr>';
           
    }
    colorPicker += '</table>';
        document.write(colorPicker);
}




function getAbsLeft(o) {
	oLeft = o.offsetLeft;
	while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oLeft += oParent.offsetLeft;
		o = oParent;
	}
	return oLeft;
}

function getAbsTop(o) {
	oTop = o.offsetTop;
	while(o.offsetParent!=null) {
		oParent = o.offsetParent;
		oTop += oParent.offsetTop;
		o = oParent;
	}
	return oTop;
}

