/* 
iReports v1.0
Developed by    : Tekbroker Ventures LLC
Code Ownership  : Tekbroker Ventures LLC 
 * Created Mar 28, 2009 by Derrick Roach
 * Last modified: 03.28.2009
*/

var ie=document.all;
var nn6=document.getElementById&&!document.all;

function goHome() {
	document.location.replace('Default.aspx');
}
function getDimension(side) {
  var myWidth = 0, myHeight = 0;

  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if (side=="height") {
    return myHeight;
  } else {
    return myWidth;
  }
//  window.alert( 'Width = ' + myWidth );
//  window.alert( 'Height = ' + myHeight );
}

function switchLang(lang) {
    var url=document.location.href;
    url=url.replace("lg=","~");
    uP=url.split("~");
    url=uP[0];
    if (url.substr(url.length-1,1)=="?") {
        url=url.replace("?","");
    }
    if (url.indexOf('?')>-1) {
        addUrl="&lg="+lang;
    } else {
        addUrl="?lg="+lang;
    }
    document.location.replace(url+addUrl);
}

function validateSection(vNo) {
	if (vNo=="0") {
		hasSel=false;
		arrIncs=document.getElementsByName("inc_type");
		for (i=0;i<arrIncs.length;i++) {
			if (arrIncs[i].checked) {
				hasSel=true;
				break;
			}
		}
		if (!hasSel) {
			alert("You must select an Incident Type!");
			return false;
		}
		return true;
	} else if (vNo=="1") {
		hasSel=false;
		arrIncs=document.getElementsByName("person_type_id");
		for (i=0;i<arrIncs.length;i++) {
			if (arrIncs[i].checked) {
				hasSel=true;
				break;
			}
		}
		if (!hasSel) {
			alert("You must select a Person Type!");
			return false;
		}
		return true;
	} else {
        if (vNo=="99") {
            vNo='';
        }
        if (eval('mandatory_fields'+vNo)!="") {
		    manFlds=eval('mandatory_fields'+vNo).split(",");
		    for (i=0;i<manFlds.length;i++) {
                hasVal=false;
			    if (!isNaN(manFlds[i])) {
				    arFlds=document.getElementsByName("fld"+manFlds[i])
			    } else {
				    arFlds=document.getElementsByName(manFlds[i])
			    }
			    for (f=0;f<arFlds.length;f++) {
    			    if (arFlds[f].type=="radio" || arFlds[f].type=="checkbox") {
				        if (arFlds[f].checked) {
				            if (!hasVal) {
				                hasVal=true;
				            }
				        }
    			    } else {
				        if (arFlds[f].value=="" || arFlds[f].value=="0") {
			                alert(" All required fields must be entered with appropriate data.");
			                return false;
				        } else {
				            hasVal=true;
				        }
				    }
			    }
		    }
		    if (!hasVal) {
		        alert("All required fields must be entered with appropriate data.");
		        return false;
		    }
		}
		return true;
	}
}

function validateEditForm() {
    if (eval('mandatory_fields')!="") {
	    manFlds=eval('mandatory_fields').split(",");
	    for (i=0;i<manFlds.length;i++) {
            hasVal=false;
		    if (!isNaN(manFlds[i])) {
                fldname=manFlds[i].substring(0,manFlds[i].length-1);
			    arFlds=document.getElementsByName("fld"+fldname)
		    } else {
                fldname=manFlds[i].substring(0,manFlds[i].length-1);
			    arFlds=document.getElementsByName(fldname)
		    }
		    for (f=0;f<arFlds.length;f++) {
			    if (arFlds[f].type=="radio" || arFlds[f].type=="checkbox") {
			        if (arFlds[f].checked) {
			            if (!hasVal) {
			                hasVal=true;
			            }
			        }
			    } else {
			        if (arFlds[f].value=="" || arFlds[f].value=="0") {
		                alert("All required fields must be entered with appropriate data.");
		                return false;
			        } else {
			            hasVal=true;
			        }
			    }
		    }
	    }
	    if (!hasVal) {
	        alert("All required fields must be entered with appropriate data.");
	        return false;
	    }
	}
	return true;
}

function showPType(lg) {
	selVal=0;
	if (document.getElementById("inc_type").type!="hidden") {
		arrIncs=document.getElementsByName("inc_type");
		for (i=0;i<arrIncs.length;i++) {
			if (arrIncs[i].checked) {
				selVal=arrIncs[i].value;
				break;
			}
		}
		pt="yes";
		showpt="";
	} else {
		selVal=document.getElementById("inc_type").value;
		pt=document.getElementById("person_type_id").value;
		showpt="&show=yes";
	}
	document.location.href="Default.aspx?func=ptype&ty="+selVal+"&pt="+pt+showpt+"&lg="+lg;
}

function showForm(vNo,lg) {
	selVal=0;
	if (document.getElementById("inc_type").type!="hidden") {
		arrIncs=document.getElementsByName("inc_type");
		for (i=0;i<arrIncs.length;i++) {
			if (arrIncs[i].checked) {
				selVal=arrIncs[i].value;
				break;
			}
		}
	} else {
		selVal=document.getElementById("inc_type").value;
		arrPts=document.getElementsByName("person_type_id");
		for (i=0;i<arrPts.length;i++) {
			if (arrPts[i].checked) {
				pt=arrPts[i].value;
				break;
			}
		}
	}
	document.location.href="Default.aspx?func=form&fs="+vNo+"&ty="+selVal+"&pt="+pt+"&lg="+lg;
}

var curSec=0;
function getMoreSection(pgId,ty,pt,fs,lg,secNo) {
    curSec=secNo;
	var url="client_methods.aspx?func=getMoreSection&pgid="+pgId+"&ty="+ty+"&pt="+pt+"&fs="+fs+"&lg="+lg+"&sec="+secNo;
	noShow=true;
	sendData(url,"aftergetMoreSection");
}

function aftergetMoreSection(vData) {
	vData=unescape(vData);
	try {
		if (vData.substring(0,5)=="ERROR") {
			alert(vData);
		} else {
		    sHtm=document.getElementById('repeat_sec'+curSec).innerHTML;
			document.getElementById('repeat_sec'+curSec).innerHTML = StrReplace(sHtm,"`","") + vData;
			curSec=0;
		}
	} catch(e) {
		alert(e)
	}
}

function removeSection(uId, secNo) {
    obj=document.getElementById('section1'+uId);
    document.getElementById('repeat_sec'+secNo).removeChild(obj);
}

var currPage=1;
function showPage(pgNo,iid) {
	if (pgNo<1) {
		document.location.href="Default.aspx?func=itlist&iid="+iid;
		currPage=1;
	} else {
		document.getElementById('section'+currPage).style.display="none";
		document.getElementById('section'+pgNo).style.display="";
		currPage=pgNo;
	}
}

function isNumeric(e, withDecimal, obj) {
    // Get the ASCII value of the key that the user entered
    var key = window.event.keyCode;
    allowedKeys="8,36,35,37,39,46,";
    if (allowedKeys.indexOf(key+",")!=-1) {
        return;
    }
    if (withDecimal) {
        // Verify if the key entered was a numeric character (0-9) or a decimal (.)
        if ( (key > 47 && key < 58) || key == 110 || key==190 || (key > 95 && key < 106) )
            // If it was, then allow the entry to continue
            if (obj.value.indexOf('.')!=-1 && (key==110 || key==190)) { //to allow for only one decimal
                window.event.returnValue = null; 
            } else {
                return;
            }
        else
            // If it was not, then dispose the key and continue with entry
            window.event.returnValue = null; 
    } else { //without decimal
        if ( (key > 47 && key < 58) || (key > 95 && key < 106) )
            // If it was, then allow the entry to continue
            return;
        else
            // If it was not, then dispose the key and continue with entry
            window.event.returnValue = null; 
    }
}

function isPhoneNumber(obj) {
    if(obj.value != "") {
        pnum=StrReplace(obj.value,"(","");
        pnum=StrReplace(pnum,")","");
        pnum=StrReplace(pnum,"-","");
        pnum=StrReplace(pnum," ","");
        
        if (pnum.length < 10 ) { 
            alert("Phone is empty or less than 10 digits"); 
            obj.value="";
            obj.focus(); 
            return false; 
        }
    } 
}

function checkNumericX(e,obj,sType) {
	/*
	Syntax: checkNumeric(event,object,'NUMBER,[<no of decimals>],[<allow Negatives>]')
	object				:	control object
	<no of decimals>	:	Optional. Number of decimal places allowed
	<allow Negatives>	:	Optional. 'true' to allow negatives. By default, no negatives are allowed.

	NOTE: call this function on the 'onKeyDown' event
	*/
//	oSel=document.selection.createRange();
	txt=getSel();
//	if (oSel.text!="") {
	if (txt!="") {
		e.returnValue=true;
		if (nn6) { e.preventDefault(); }
		obj.value="";
	}					
	allowNegatives=false;
	if (sType.indexOf(',')!=-1) {
	    arrVals=sType.split(',');
	    vDecPlaces=arrVals[1]-1;
		if (arrVals[2]=='true') {
			allowNegatives=true;
		}
		keys='48,49,50,51,52,53,54,55,56,57,58,96,97,98,99,100,101,102,103,104,105,110,190,8,35,36,';
	} else {
	    vDecPlaces=0;
		keys='48,49,50,51,52,53,54,55,56,57,58,96,97,98,99,100,101,102,103,104,105,8,35,36,';
	}
	if (e.keyCode==46 || e.keyCode==9 || e.keyCode==109 || e.keyCode==189 || e.keyCode==13 || (e.keyCode>35 && e.keyCode<41)) {
		if (e.keyCode==109 || e.keyCode==189) {
			if (allowNegatives && obj.value.length<1) {
				e.returnValue=true;
			} else {
				e.returnValue=false;
				if (nn6) { e.preventDefault(); }
			}
		} else {
			e.returnValue=true;
		}
	} else {
		if (e.shiftKey && (e.keyCode>=48 && e.keyCode<=57)) {
			e.returnValue=false;
			if (nn6) { e.preventDefault(); }
		}
		if (keys.indexOf(e.keyCode+',')==-1) {
			e.returnValue=false;
			if (nn6) { e.preventDefault(); }
		} else {
			if (vDecPlaces>0) {
				if (e.keyCode==110 || e.keyCode==190) {
					if (obj.value.indexOf('.')!=-1) {
						e.returnValue=false
						if (nn6) { e.preventDefault(); }
					}
				} else {
 					//check the number of decimal places
					if (e.keyCode!=8 && e.keyCode!=35 && e.keyCode!=36) {
						if (obj.value.indexOf('.')!=-1) {
							arrVals=obj.value.split('.')
							if (arrVals[1].length>vDecPlaces && e.keyCode!=9) {
								e.returnValue=false;
								if (nn6) { e.preventDefault(); }
							}
						}
					}
				}
			}
		}
	}
}

function getSel()
{
	var txt = '';
	var foundIn = '';
	if (window.getSelection)
	{
		txt = window.getSelection();
//		foundIn = 'window.getSelection()';
	}
	else if (document.getSelection)
	{
		txt = document.getSelection();
//		foundIn = 'document.getSelection()';
	}
	else if (document.selection)
	{
		txt = document.selection.createRange().text;
//		foundIn = 'document.selection.createRange()';
	}
	else return;
//	document.forms[0].selectedtext.value = 'Found in: ' + foundIn + '\n' + txt;
	return txt;
}

function validateEmail(sValue) {
	if (sValue!="") {
		//for proper format of email
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if( !(filter.test(sValue)) ) {
			return false;
		} else {
			return true;
		}
	}
	return true;
}

function checkEmailFormat(obj) {
    if (obj.value!="") {
	    if (!validateEmail(obj.value)) {
		    alert("Please enter a valid email address.");
		    obj.value="";
            obj.focus();
		    return false;
        }
    }
	return true;
}

function validateValue(obj,ques,fName) {
    cObj=document.getElementById(fName)
    if (obj.value!="" && obj.value!=cObj.value) {
	    alert("The "+ques+" does not match.");
        obj.value="";
	    obj.focus();
	    return false;
    }
    return true;
}

function editFormValue(vId) {
	document.getElementById('disp'+vId).style.display='';
	document.getElementById('text'+vId).style.display='none';
	document.getElementById('edit'+vId).style.display='none';
	document.getElementById('save'+vId).style.display='';
	document.getElementById('cancel'+vId).style.display='';
}
function cancelFormValue(vId) {
	document.getElementById('disp'+vId).style.display='none';
	document.getElementById('text'+vId).style.display='';
	document.getElementById('edit'+vId).style.display='';
	document.getElementById('save'+vId).style.display='none';
	document.getElementById('cancel'+vId).style.display='none';
//	document.getElementById('frmReport').reset();
}
function saveFormValue(vId,fId) {
	nVal=document.getElementById('fld'+fId).value;
	vfinal=document.getElementById('final'+vId).value;
	if (mandatory_fields.indexOf(fId+",")!=-1) {
		//check if value has been entered
		if (nVal=="") {
			alert("A value must be entered/selected.");
			document.getElementById('fld'+fId).focus();
			return false;
		}
	}
	
	var url="client_methods.aspx?func=saveFormValue&vid="+vId+"&fid="+fId+"&fin="+vfinal+"&val="+nVal;
	noShow=true;
	sendData(url,"aftersaveFormValue");
}
function aftersaveFormValue(vData) {
	vData=unescape(vData);
	try {
		if (vData.substring(0,5)=="ERROR") {
			alert(vData);
		} else {
		    arVals=vData.split("|");
		    vId=arVals[0];
		    fId=arVals[1];
		    if (document.getElementById('fld'+fId).type=="select-one") {
		        idx=document.getElementById('fld'+fId).selectedIndex;
		        val=document.getElementById('fld'+fId).options[idx].text;
		    } else {
		        val=document.getElementById('fld'+fId).value;
		    }
		    document.getElementById('text'+vId).innerHTML=val;
		    cancelFormValue(vId);			
		}
	} catch(e) {
		alert(e)
	}
}

function deleteTmpReport(rId,lg) {
	if (confirm("All the information you have entered will be lost.\n\nAre you sure you want to cancel this report?")) {
		document.location.replace("Default.aspx?func=delrep&rid="+rId+"&lg="+lg);
	}
}

function checkExt(obj) {
	val=obj.value.toLowerCase();
	if(val.lastIndexOf(".jpg")==-1 && val.lastIndexOf(".jpeg")==-1) {
	    alert("Please select a file with a JPG or JPEG extension.");
        ClearBrowseContent(obj);
	    return false;
//	} else { 
//	    document.getElementById('imgPhoto'+pId).src="file:\/\/"+obj.value;
//	    document.getElementById('imgPhoto'+pId).style.display='';
//	    return true;
	}
}

function ClearBrowseContent(control)
{    
    var newbrowse= control.cloneNode(false);   
    control.parentNode.replaceChild(newbrowse ,control);
}

function deleteSection(pId,sno,rId,lg) {
	if (confirm("Are you sure you want to delete this section?")) {
		document.location.replace("Default.aspx?func=delsec&pid="+pId+"&sno="+sno+"&rid="+rId+"&lg="+lg);
	}
}
 
function restartForm() {
    document.getElementById(dvPreview).style.display="none";
    document.getElementById(pgHolder).style.display="";
}

function adjustHeight() {
    hgt=getDimension('height')-137;
    document.getElementById(clientID+'_left_nav').style.height=hgt+'px';
}

function CheckLength(obj,maxlen) {
	if (obj.value.length>maxlen) {
		obj.value=obj.value.substring(0, maxlen);
	}
}

var n;
var p;
var p1;
function ValidatePhone() {
    p=p1.value
    if(p.length==3) {
    	//d10=p.indexOf('(')
    	pp=p;
    	d4=p.indexOf('(')
    	d5=p.indexOf(')')
    	if(d4==-1) {
    		pp="("+pp;
    	}
    	if(d5==-1) {
    		pp=pp+")";
    	}
	    //pp="("+pp+")";
    	p1.value="";
    	p1.value=pp;
    }
    if(p.length>3) {
    	d1=p.indexOf('(')
    	d2=p.indexOf(')')
    	if (d2==-1) {
    		l30=p.length;
    		p30=p.substring(0,4);
    		//alert(p30);
    		p30=p30+")"
    		p31=p.substring(4,l30);
    		pp=p30+p31;
    		//alert(p31);
    		p1.value="";
    		p1.value=pp;
    	}
	}
    if(p.length>5) {
    	p11=p.substring(d1+1,d2);
    	if(p11.length>3) {
        	p12=p11;
        	l12=p12.length;
        	l15=p.length
        	//l12=l12-3
        	p13=p11.substring(0,3);
        	p14=p11.substring(3,l12);
        	p15=p.substring(d2+1,l15);
        	p1.value="";
        	pp="("+p13+")"+p14+p15;
        	p1.value=pp;
        	//obj1.value="";
        	//obj1.value=pp;
    	}
    	l16=p.length;
    	p16=p.substring(d2+1,l16);
    	l17=p16.length;
    	if(l17>3&&p16.indexOf('-')==-1){
    		p17=p.substring(d2+1,d2+4);
    		p18=p.substring(d2+4,l16);
    		p19=p.substring(0,d2+1);
    		//alert(p19);
        	pp=p19+p17+"-"+p18;
        	p1.value="";
        	p1.value=pp;
        	//obj1.value="";
        	//obj1.value=pp;
    	}
    }
//}
    setTimeout(ValidatePhone,100)
}

function IsPhone(m) {
    n=m.name;
    //p1=document.forms[0].elements[n]
    p1=m
    ValidatePhone()
}

function testphone(obj1) {
    p=obj1.value
    //alert(p)
    p=p.replace("(","")
    p=p.replace(")","")
    p=p.replace("-","")
    p=p.replace("-","")
    //alert(isNaN(p))
    if (isNaN(p)==true){
        alert("Check phone number");
        return false;
    }
}

function storeSections(tNo) {
    for (z=1;z<tNo+1;z++) {
        htm=encodeHtml(document.getElementById('repeat_sec'+z).innerHTML);
        document.getElementById('hdnSec'+z).value=htm;
    }
}

function encodeHtml(inHtml) {
    inHtml=StrReplace(inHtml,"`","");
    encodedHtml = escape(inHtml);
/*
    encodedHtml = encodedHtml.replace("&#90","&acute");
    encodedHtml = encodedHtml.replace(/\//g,"%2F");
    encodedHtml = encodedHtml.replace(/\?/g,"%3F");
    encodedHtml = encodedHtml.replace(/=/g,"%3D");
    encodedHtml = encodedHtml.replace(/&/g,"%26");
    encodedHtml = encodedHtml.replace(/@/g,"%40");
*/
    return encodedHtml;
}

function restoreSections(tNo) {
    for (z=1;z<tNo+1;z++) {
        if (document.getElementById('hdnSec'+z)!=null) {
            if (document.getElementById('hdnSec'+z).innerHTML!="") {
                htm=unescape(document.getElementById('hdnSec'+z).innerHTML);
                document.getElementById('repeat_sec'+z).innerHTML=htm;
            }
        }
    }
}

function StrReplace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += StrReplace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
