/**
 * This method checks whether the specified object exists in the specified array
 * If found this method returns the index of found place otherwise it returns -1
 *
 * @param array of objects
 * @param object to find in above array
 */
function existsInArray(array, object) {
    var result = -1;
    for (var ijklmnop = 0; ijklmnop < array.length; ijklmnop++)
    {
        if (array[ijklmnop] == object.toUpperCase()) {
            result = ijklmnop;
            break;
        }
    }
    return result;
}

/**
 * This method valid email address
 * @param string - the value to validate
 * Author: Adnan Yaqoob
 */
function isEmailValid(string) {
    var validFormatRegExp = /^\w(\.?\w)*@\w(\.?[-\w])*\.[a-z]{2,4}$/i;
    var isValid = validFormatRegExp.test(string);
    return isValid;
}


function isValidEmail(email) {
    var result = false;
    var emailstring = email;
    var ampIndex = emailstring.indexOf("@");
    var afterAmp = emailstring.substring((ampIndex + 1), emailstring.length);
		// find a dot in the portion of the string after the ampersand only
    var dotIndex = afterAmp.indexOf(".");
		// determine dot position in entire string (not just after amp portion)
    dotIndex = dotIndex + ampIndex + 1;
		// afterAmp will be portion of string from ampersand to dot
    afterAmp = emailstring.substring((ampIndex + 1), dotIndex);
		// afterDot will be portion of string from dot to end of string
    var afterDot = emailstring.substring((dotIndex + 1), emailstring.length);
    var beforeAmp = emailstring.substring(0, (ampIndex));
		//old regex did not allow subdomains and dots in names
    var email_regex = /^\w(?:\w|-|\.(?!\.|@))*@\w(?:\w|-|\.(?!\.))*\.\w{2,3}/
		// index of -1 means "not found"
    if ((emailstring.indexOf("@") != "-1") &&
        (emailstring.length > 5) &&
        (afterAmp.length > 0) &&
        (beforeAmp.length > 1) &&
        (afterDot.length > 1) &&
        (email_regex.test(emailstring))) {
        result = true;
    }

    return result;
}

function isValidPhone() {
    // TOOD: Phone format not known

    return false;
}

function trimString(string) {
    var result = "";
    if (string != null) {
        result = string.replace(/^\s+|\s+$/g, '');
    }
    return result;
}