// JavaScript Document
var C_ATTRIBUTE = 0
var C_TEXT = 1
var C_DATA = 2

Number.prototype.roundTo = roundTo;

var dwzAjaxValidateMaster = new Array()
var dwzAjaxValidate = new Array()
var dwzAjaxValidate_MainMessage = ""
var dwzAjaxValidate_LockForm = true
var dwzAjaxValidate_FormName = ""
var dwzAjaxValidate_IsValid = false
var dwzAjaxValidate_Redirect = ""
var dwzAjaxValidate_JsAlert = ""
var dwzAjaxValidate_StartPath = ""
var dwzAjaxValidate_objXml = null
var dwzAjaxValidate_ProgressDiv = "dwzAjaxValidatorProgressDiv"
var dwzAjaxValidate_BackGround = "dwzAjaxValidatorBackGround"
var dwzAjaxValidate_Opacity = .25
var dwzAjaxValidate_BgColor = ""
var dwzAjaxValidate_progressDivWidth = 200
var dwzAjaxValidate_progressDivHeight = 75
var dwzSubmitForm = false
var dwzAjaxValidate_instance = ""
var dwzAjaxValidate_ClassName = ""

function dwzAjaxServerValidate(instance){
	
	if(dwzSubmitForm){
		return true
	}
	
	window.scroll(0,0); 
	
	dwzAjaxValidate_instance = instance.toString()
	dwzAjaxValidate = dwzAjaxValidateMaster[instance].Field
	dwzAjaxValidate_JsAlert = dwzAjaxValidateMaster[instance].JsAlert
	dwzAjaxValidate_StartPath = dwzAjaxValidateMaster[instance].StartPath
	dwzAjaxValidate_MainMessage = dwzAjaxValidateMaster[instance].MainMessage
	dwzAjaxValidate_LockForm = dwzAjaxValidateMaster[instance].LockForm
	dwzAjaxValidate_Opacity = dwzAjaxValidateMaster[instance].Opacity
	dwzAjaxValidate_BgColor = dwzAjaxValidateMaster[instance].BgColor
	dwzAjaxValidate_FormName = dwzAjaxValidateMaster[instance].FormName
	dwzAjaxValidate_IsValid = false
	dwzAjaxValidate_Redirect = ""
	dwzAjaxValidate_objXml = null
	dwzSubmitForm = false
	dwzAjaxValidate_ClassName = dwzAjaxValidateMaster[instance].ClassName
	
	if(dwzAjaxValidate_LockForm){
		$("#"+dwzAjaxValidate_ProgressDiv).css("background-color", dwzAjaxValidate_BgColor)
		centerPoint = dwzValidate_getCenterScreenPoint()
		sLeft = (centerPoint.x - (dwzAjaxValidate_progressDivWidth / 2)).roundTo(0)
		$("#"+dwzAjaxValidate_ProgressDiv).css("left", sLeft)
		sTop = (centerPoint.y - (dwzAjaxValidate_progressDivHeight / 2)).roundTo(0)
		$("#"+dwzAjaxValidate_ProgressDiv).css("top", sTop)
		$("#"+dwzAjaxValidate_ProgressDiv).css("display","block")
		$("#"+dwzAjaxValidate_ProgressDiv).css("visibility","visible")
		dwzValidate_OpenBackGround()
	}else{	
		dwzAjaxStartValidate()
	}
	
	return false
}

function dwzAjaxStartValidate(){
		
	urlPage = dwzAjaxValidate_StartPath + "dwzAjaxValidator/AjaxProcessor.asp"
	postData = dwzValidate_getPostData()
	
	var objXml
	var html = $.ajax({
		url: urlPage,
		dataType:"xml",
		data: postData,
		type: "POST",
		success:function(xml){objXml = xml;},
		async: false
	}).responseText;
	
	if(objXml){
		dwzAjaxValidate_objXml = objXml
		dwzClientValidate()
	}else{
		win = window.open("")
		win.document.open()
		win.document.write(html)
		win.document.close()
		return
	}	
}

function dwzClientValidate(){
	var isValid = false
	var errMsg = ""
	var summary = ""
	var alertMsg = ""
	var mainMessage = ""
	var summaryHtml = ""
	
	for(x=0; x<dwzAjaxValidate.length; x++){
		isValid = (dwzValidate_getXmlValue(dwzAjaxValidate_objXml, dwzAjaxValidate[x].FieldName, -1, "valid")=="1")?true:false
		errMsg = dwzValidate_getXmlValue(dwzAjaxValidate_objXml, dwzAjaxValidate[x].FieldName, C_DATA)
		if(!isValid){
			divName = "#dwzAjaxValidate_" + dwzAjaxValidate[x].FieldName		
			$(divName).css("display","block")
			$(divName).css("visibility","visible")
			summary += errMsg + "\n"
			summaryHtml += errMsg + "<br>"
			if($(divName).attr("dwzType") == "T"){
				$(divName).html(errMsg)
			}
		}else{
			divName = "#dwzAjaxValidate_" + dwzAjaxValidate[x].FieldName		
			$(divName).css("display","none")
			$(divName).css("visibility","hidden")
		}
	}
	mainMessage = dwzAjaxValidate_MainMessage
	
	if(dwzAjaxValidate_JsAlert != "N" && summary != ""){
		if(mainMessage.length != 0){
			alertMsg = mainMessage + "\n\n" + summary
		}else{
			alertMsg = summary
		}
	}
	
	if(summary != ""){
		mainMessage = "<span class='"+dwzAjaxValidate_ClassName+"' >" + mainMessage + "</span>"
		summary = "<span class='"+dwzAjaxValidate_ClassName+"' >" + summary + "</span>"
		$("#dwzAjaxValidateMainMessage").html(mainMessage + "<br>")
		$("#dwzAjaxValidateMainMessage").css("display", "block")
		$("#dwzAjaxValidateSummary").html(summaryHtml)
		$("#dwzAjaxValidateSummary").css("display", "block")
	}else{
		$("#dwzAjaxValidateMainMessage").html("")
		$("#dwzAjaxValidateMainMessage").css("display", "none")
		$("#dwzAjaxValidateSummary").html("")
		$("#dwzAjaxValidateSummary").css("display", "none")
	}	
	
	window.scroll(0,0); 
	
	if(alertMsg != ""){
		alert(alertMsg)
	}
	
	if(dwzAjaxValidate_LockForm){
		dwzValidate_closeBackGround()
	}else{
		dwzValidate_SubmitForm()
	}
}

function dwzValidate_SubmitForm(){
	isValid = dwzValidate_getXmlValue(dwzAjaxValidate_objXml, "isValid", -1, "valid")
	if(isValid == "1"){
		target = $("#"+dwzAjaxValidate_FormName).attr("target")
		if(target && target.toLowerCase() == "dwzAjaxUploadFrame".toLowerCase()){
			dwzSubmitForm = false
		}else{
			dwzSubmitForm = true
		}
		$("#"+dwzAjaxValidate_FormName).submit()
	}	
}

function dwzValidate_closeBackGround(){
	$("#"+dwzAjaxValidate_ProgressDiv).css("display","none")
	$("#"+dwzAjaxValidate_ProgressDiv).css("visibility","hidden")
	$("#"+dwzAjaxValidate_ProgressDiv).css("left", 1)
	$("#"+dwzAjaxValidate_ProgressDiv).css("top", 1)
	$("#"+dwzAjaxValidate_ProgressDiv).css("width", 1)
	$("#"+dwzAjaxValidate_ProgressDiv).css("height", 1)				
	$("#"+dwzAjaxValidate_BackGround).fadeTo(1500,0,dwzValidate_RemoveBgLayer);
}

function dwzValidate_RemoveBgLayer(){
	$("#"+dwzAjaxValidate_BackGround).width("0")
	$("#"+dwzAjaxValidate_BackGround).height("0")
	$("#"+dwzAjaxValidate_BackGround).css("display","none")
	dwzValidate_SubmitForm()
}

function dwzValidate_OpenBackGround(){
	if (window.innerWidth) {
        W = window.innerWidth;
   	    H = window.innerHeight;
	} else if (document.body.clientWidth) {
		W = document.body.clientWidth;
		H = document.body.clientHeight;
	} else {
		W = body.offsetWidth;
		H = body.offsetHeight
	}
	$("#"+dwzAjaxValidate_BackGround).css("top",0)
	$("#"+dwzAjaxValidate_BackGround).css("left",0)
	$("#"+dwzAjaxValidate_BackGround).css("width",W)
	$("#"+dwzAjaxValidate_BackGround).css("height",W)	
	$("#"+dwzAjaxValidate_BackGround).fadeTo(1500, dwzAjaxValidate_Opacity, timerStartValidate);	
}

function timerStartValidate(){
	setTimeout("dwzAjaxStartValidate()", 1500)
}

function dwzValidate_getPostData(){
	retStr = ""
	retStr += "dwzServerValidate=" + dwzAjaxValidate_FormName
	retStr += "&dwzInstance=" + dwzAjaxValidate_instance
	for(x=0; x<dwzAjaxValidate.length; x++){
		if(dwzAjaxValidate[x].Group != ""){
			list = dwzAjaxValidate[x].Group.split(",")
			for(y=1; y<list.length; y++){
				retStr += "&" + list[y] + "=" + escape(dwzValidate_getFieldValue(list[y]))
			}
		}else{
			retStr += "&" + dwzAjaxValidate[x].FieldName + "=" + escape(dwzValidate_getFieldValue(dwzAjaxValidate[x].FieldName))
		}
	}
	return retStr
}

function dwzValidate_getFieldValue(fieldName){
	value = ""
	field = Dwz_findObj(fieldName)
	if(!field){
		return ""
	}
	if(!field.length){
		switch(field.tagName.toUpperCase()){
		case "INPUT":
			switch(field.type.toUpperCase()){
			case "TEXT":
			case "FILE":
			case "HIDDEN":
				value = field.value
				break
			case "CHECKBOX":
				if(field.checked == true){						
					value = field.value
				}
				break
			case "RADIO":
				if(field.checked == true){
					value = field.value
				}
				break
			}
			break
		case "TEXTAREA":
			value = field.value
			break
		}
	}else if(field.length){
		if(field.tagName && field.tagName.toUpperCase() == "SELECT"){
			for(i=0; i<field.options.length;i++){
				if(field.options[i].selected){
					if(value != ""){
						value += ","
					}
					value = field.options[i].value
				}
			}			
		}else{			
			switch(field[0].tagName.toUpperCase()){
			case "INPUT":
				switch(field[0].type.toUpperCase()){
				case "TEXT":
				case "HIDDEN":
					for(y=0;y<field.length;y++){
						if(value!=""){
							value += ","
						}
						value += field[y].value
					}					
					break
				case "CHECKBOX":
					for(y=0;y<field.length;y++){
						if(field[y].checked){
							if(value!=""){
								value += ","
							}
							value += field[y].value
						}
					}
					break
				case "RADIO":
					for(y=0;y<field.length;y++){
						if(field[y].checked){
							if(value!=""){
								value += ","
							}
							value += field[y].value
						}
					}
					break
				}
				break
			case "TEXTAREA":
				for(y=0;y<field.length;y++){
					if(value!=""){
						value += ","
					}
					value += field[y].value
				}
				break
			}
		}
	}
	return value
}

function dwzValidate_getCenterScreenPoint(){
	var point = new Object()
	if (window.innerWidth) {
        W = window.innerWidth;
   	    H = window.innerHeight;
	} else if (document.body.clientWidth) {
		W = document.body.clientWidth;
		H = document.body.clientHeight;
	} else {
		W = body.offsetWidth;
		H = body.offsetHeight
	}
	
	W = (W / 2).roundTo(0)
	H = 200
	
	point.x = W
	point.y = H
	return point
}

function dwzValidate_getXmlValue(objXml, name, sType, attrName){
	//http://www.w3schools.com/dom/default.asp
	doc = objXml.getElementsByTagName("parameter")
	retStr = ""
	for ( i = 0; i < doc.length; i++ ) 
	{
		if(doc[i].getAttribute("key").toLowerCase() == name.toLowerCase()){
			switch(sType){
			case C_ATTRIBUTE:
				retStr = doc[i].getAttribute("value")
				break
			case C_TEXT:
				retStr = doc[i].text
				break
			case C_DATA:
				if(doc[i].hasChildNodes() && doc[i].childNodes[0]){
					if(doc[i].childNodes[0].data){
						retStr = doc[i].childNodes[0].data
					}else{
						retStr = doc[i].childNodes[0].text
					}
				}
				break
			default:
				retStr = doc[i].getAttribute(attrName)
				break
			}
		}
	}
	return retStr	
}

function Dwz_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function roundTo(decimalpositions)
{
    var i = this * Math.pow(10,decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
}
