﻿// JScript File
function openReplace(sUrl) {
	var url = sUrl;
	winReplace = window.open(url, "replace","scrollbars=yes, status=yes, menubar=no, width=540, height=350, resizable=yes");
	winReplace.focus();
}

function check() {
    var x = confirm("Are you sure you want to delete?")
    if (x)
        return true;
    else
        return false;
}
function emailCheck(emailStr) {
    var checkTLD = 1;

    var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

    var emailPat = /^(.+)@(.+)$/;

    var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

    var validChars = "\[^\\s" + specialChars + "\]";

    var quotedUser = "(\"[^\"]*\")";

    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

    var atom = validChars + '+';

    var word = "(" + atom + "|" + quotedUser + ")";

    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");

    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");

    var matchArray = emailStr.match(emailPat);

    if (matchArray == null) {
        alert("Email address seems incorrect.");
        return false;
    }
    var user = matchArray[1];
    var domain = matchArray[2];

    for (i = 0; i < user.length; i++) {
        if (user.charCodeAt(i) > 127) {
            alert("Ths email contains invalid characters.");
            return false;
        }
    }
    for (i = 0; i < domain.length; i++) {
        if (domain.charCodeAt(i) > 127) {
            alert("Ths domain name contains invalid characters.");
            return false;
        }
    }

    if (user.match(userPat) == null) {
        alert("The email doesn't seem to be valid.");
        return false;
    }

    /* if the e-mail address is at an IP address (as opposed to a symbolic
    host name) make sure the IP address is valid. */

    var IPArray = domain.match(ipDomainPat);
    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                alert("Destination IP address is invalid!");
                return false;
            }
        }
    }
    var atomPat = new RegExp("^" + atom + "$");
    var domArr = domain.split(".");
    var len = domArr.length;
    for (i = 0; i < len; i++) {
        if (domArr[i].search(atomPat) == -1) {
            alert("The domain name does not seem to be valid.");
            return false;
        }
    }

    if (checkTLD && domArr[domArr.length - 1].length != 2 && domArr[domArr.length - 1].search(knownDomsPat) == -1) {
        alert("The address must end in a well-known domain or two letter " + "country.");
        return false;
    }
    if (len < 2) {
        alert("This address is missing a hostname!");
        return false;
    }
    return true;
}
function checkEmailForm()
{
    var objName = document.getElementById('ctl00_ContentPlaceHolder1_txtname');
    var objEmail = document.getElementById('ctl00_ContentPlaceHolder1_txtForm');
     if(objName.value=="")
    {
        alert("Name should not be left blank");
        objName.focus();
        return false;
    }    
     if(objEmail.value=="")
    {
        alert("Email should not be left blank");
        objEmail.focus();
        return false;    
    }
    if(!emailCheck(objEmail.value))
    {
        objEmail.value="";
        objEmail.focus();
        return false;
    }	
}


function openPopUp(theURL, winName, features) { //v2.0
    mapWin = window.open(theURL, winName, features);
    mapWin.focus();
}

function cleardate(x)
{
document.getElementById(x).value="";
}

// indicates how many characters have been entered in a textfield.
function numCharCount(form, field, length_limit)
 {	
	var length = calculate_msglen(eval("document" + "." + form + "." + field + ".value"));
	document.getElementById("textlimit").innerText = length;
	
	if (length > length_limit) {
		alert("Warning: " + length_limit + " is the max number of characters you can enter for this field. \n Please cut down your text.");
	}
}	
function calculate_msglen(message)
{
	var nbytes = 0;	
	for (i=0; i<message.length; i++) {		
		var ch = message.charAt(i);
		if(escape(ch).length > 4) {
			nbytes += 2;
		} else if (ch == '\n') {
			if (message.charAt(i-1) != '\r') {
				nbytes += 1;
			}
		} else if (ch == '<' || ch == '>') {
			nbytes += 4;
		} else {
			nbytes += 1;
		}
	}
	return nbytes;
}
function checkcomma(str) {
    var pos = document.getElementById(str).value.indexOf(',')
    if (pos >= 0) {
        alert('No commas allowed in the bullet points'); return false;
    }
}

//pass a form name an array of itmes and an array of descriptive names - for the alert messages.
function checkFormItems(form, items, messages) {
	for (var i=0; i<items.length; i++) {				
		formItem = eval(form + "." + items[i]);
		formItemValue = eval(form + "." + items[i] + ".value");
		if (formItemValue == "" || formItemValue == "any") {
			alert("Please fill in the field: " + "\"" + messages[i] + "\"");
			formItem.focus();
			return false;
		}
	}
	return true;
}

// used in propertyAdd.asp propertyEdit
function chkPropertyFormItems()
 {
    
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine1');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine2');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine3');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine4');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine5');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine6');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine7');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine8');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine9');
    checkcomma('ctl00_ContentPlaceHolder1_txtTextLine10'); 

        var arrayFormItem = new Array('ctl00_ContentPlaceHolder1_ddlAgent1','ctl00_ContentPlaceHolder1_ddlStatus', 'ctl00_ContentPlaceHolder1_ddlProptype','ctl00_ContentPlaceHolder1_txtExpiryDate', 'ctl00_ContentPlaceHolder1_txtRoadNum', 'ctl00_ContentPlaceHolder1_txtRoadName','ctl00_ContentPlaceHolder1_ddlState', 'ctl00_ContentPlaceHolder1_ddlSuburb', 'ctl00_ContentPlaceHolder1_ddlPriceOption',  'ctl00_ContentPlaceHolder1_txtShortDescription', 'ctl00_ContentPlaceHolder1_txtDescriptionLong')
        var arrayFormItemDescription = new Array('listing agent 1','business status', 'business type','expiry date','rd num', 'rd name','state', 'suburb', 'price option',  'short description','long description')
        var arrayCurrencyFormItem = new Array('ctl00_ContentPlaceHolder1_txtPrice', 'ctl00_ContentPlaceHolder1_txtPricelow', 'ctl00_ContentPlaceHolder1_txtPriceHigh');
        var arrayCurrencyFormItemDescription = new Array('price', 'price low', 'price high');  

    if (checkFormItems('aspnetForm', arrayFormItem, arrayFormItemDescription))
     {        
        var strPriceOption = document.getElementById("ctl00_ContentPlaceHolder1_ddlPriceOption").value;
        switch (strPriceOption) {
            case "Range":              
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtPricelow.value == "") {
                    alert("Please fill in the field: " + "\"from\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtPricelow.focus();
                    return false;
                }
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtPriceHigh.value == "") {
                    alert("Please fill in the field: " + "\"to\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtPriceHigh.focus();
                    return false;
                }
                break;
            case "Price":
            
                aspnetForm.ctl00_ContentPlaceHolder1_txtPricelow.value = "";
                aspnetForm.ctl00_ContentPlaceHolder1_txtPriceHigh.value = "";
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtPrice.value == "") {
                    alert("Please fill in the field: " + "\"price\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtPrice.focus();
                    return false;
                }
                break;
            case "PriceFrom":

                aspnetForm.ctl00_ContentPlaceHolder1_txtPricelow.value = "";
                aspnetForm.ctl00_ContentPlaceHolder1_txtPriceHigh.value = "";
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtPrice.value == "")
                 {
                    alert("Please fill in the field: " + "\"price\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtPrice.focus();
                    return false;
                }
                break;
            case "EOI":  
                          
                aspnetForm.ctl00_ContentPlaceHolder1_txtPricelow.value = "";
                aspnetForm.ctl00_ContentPlaceHolder1_txtPriceHigh.value = "";
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtPrice.value == "") {
                    alert("Please fill in the field: " + "\"price\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtPrice.focus();
                    return false;
                }
                break;
          }
                //for status
        var strStatus = document.getElementById("ctl00_ContentPlaceHolder1_ddlStatus").value;
         switch (strStatus)
          {
            case "Sold":              
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtSoldPrice.value == "") 
                {
                    alert("Please fill in the field: " + "\"sold price\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtSoldPrice.focus();
                    return false;
                }
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtsolddate.value == "") 
                {
                    alert("Please fill in the field: " + "\"sold date\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtsolddate.focus();
                    return false;
                }
                break;
            case "Under Offer":                
                if (aspnetForm.ctl00_ContentPlaceHolder1_txtofferdate.value == "")
                 {
                    alert("Please fill in the field: " + "\"offer date\"");
                    aspnetForm.ctl00_ContentPlaceHolder1_txtofferdate.focus();
                    return false;
                }
                break;
           }   
        
        return (checkIsNumber('aspnetForm', arrayCurrencyFormItem, arrayCurrencyFormItemDescription))
    }
    else
        return false;
    return true;
}

function checkloginForm()
{ 

         if (document.getElementById("ctl00_ContentPlaceHolder1_txtUserName").value == "")
            {
                alert('Please enter user name.');
                document.getElementById("ctl00_ContentPlaceHolder1_txtUserName").focus();
                return false;
            }
            
             if (document.getElementById("ctl00_ContentPlaceHolder1_txtPassword").value == "")
            {
                alert('Please enter password.');
                document.getElementById("ctl00_ContentPlaceHolder1_txtPassword").focus();
                return false;
            }
}

function myShow(myDiv) 
{
    mySetStyleDisplay(myDiv, '');
}

function myHide(myDiv) 
{
    mySetStyleDisplay(myDiv, 'none');
}
	
function mySetStyleDisplay(myDiv, myStyle) {
    if (document.all) {
        obj = document.all(myDiv);
        obj.style.display = myStyle;
    } else if (document.getElementById) {
        obj = document.getElementById(myDiv);
        obj.style.display = myStyle;
    } else { alert(Err_Browser); }
}
