function trim(inputString)
{
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") {
        return inputString;
    }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ")
    { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == "  ")
    { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length-1);
        ch = retValue.substring(retValue.length-1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1)
    { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
}


function showDetails(wishId,wishName){
    //alert(wishId);
    //alert(wishName);
    document.fmWishlist.wishlistId.value = wishId
    document.fmWishlist.wishlistName.value = wishName
    //alert(document.fmWishlist.wishlistName.value)
    document.fmWishlist.opType.value = "details"
    //alert(document.fmWishlist.opType.value)
    document.fmWishlist.action ="/wishlist.do"
    document.fmWishlist.submit()
}

function wishlistOp(op,wishlistId){
    //alert(op)
    //alert(wishlistId)
    document.fmWishlist.opType.value = op
    document.fmWishlist.wishlistId.value = wishlistId
    if(op == 'delete'){
        if(confirm("Are you sure you want to delete this wishlist ?")){
            document.fmWishlist.action ="/updateWishlist.do"
            document.fmWishlist.submit();
        }else{
            return false;
        }
    }else{
        document.fmWishlist.action ="/updateWishlist.do"
        document.fmWishlist.submit();
    }
}

function wishlistValidation(){
    var wishName = trim(document.fmUpdateWishlist.wishlistName.value)
    var qty =  document.fmUpdateWishlist.quantity.value
    var comm = document.fmUpdateWishlist.comments.value
    var eventDate = document.fmUpdateWishlist.eventDate.value
	
    if(wishName == ''){
        alert(" Please Enter wishlist name.")
        document.fmUpdateWishlist.wishlistName.focus()
        return false;
    }else
    // ***************Changes by binita**********
    /*
	if(wishName.indexOf("'") != -1 || wishName.indexOf('"') != -1){
		alert(" Special characters are not allowed in wishlist name.")
		document.fmUpdateWishlist.wishlistName.value = ''
		document.fmUpdateWishlist.wishlistName.focus()
		return false;
    }else
	*/
    if(ValidateDate('eventDate') == false){
        return false;
    }else // check date validity
	    
    if(trim(qty) == '' || isNaN(qty) || qty.indexOf('.') != -1 || trim(qty) <= 0){
        alert(" Please Enter valid number of items.")
        document.fmUpdateWishlist.quantity.value = ''
        document.fmUpdateWishlist.quantity.focus()
        return false;
    }

}

function validateWishlist(){
    var wishName = trim(document.fmUpdateWishlist.wishlistName.value)
    var eventDate = document.fmUpdateWishlist.eventDate.value
	
    if(wishName == ''){
        alert(" Please enter wishlist name.")
        document.fmUpdateWishlist.wishlistName.focus()
        return false;
    }else
    if(isDate(eventDate) == false){
        alert("Please enter the date for your event.")
        document.fmUpdateWishlist.eventDate.focus()
        return false;
    }else // check date validity
    if(document.fmUpdateWishlist.budgetRange.selectedIndex==0)
    {
        alert(" Please enter your budget.")
        document.fmUpdateWishlist.budgetRange.focus()
        return false;
    }

}

function pickupAction(formId,opStr){
    //alert(formId)
    //alert(opStr)
    document.formId.opType.value=opStr
    //optyp= document.formId.opType.value
    //alert(optyp)
    document.formId.submit()
    
}

function removeCheck(){
   
    if(confirm("Are you sure you want to remove this product from wishlist?")){
		
        return true;
    }
    else{
        return false;
    }
}

function pickUpWishlistAction(){
    wishStr = document.fmWishlist.wishlistStr.value
    //alert(wishStr)
    if(wishStr == ''){
        // alert('forwarding updateWishlist.do')
        document.fmWishlist.action = "/updateWishlist.do"
        document.fmWishlist.submit();
    }
}

function checkEmailAddress(){
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789._@";
    emailId = trim(document.fmEmail.emailId.value)
    //alert(emailId)
    if(emailId == ''){
        alert("Please enter email address!")
        document.fmEmail.emailId.focus()
        return false;
    }

    else
    {
        var check1=emailId.lastIndexOf("@");
        var check2=emailId.indexOf(".",check1);
        var check3=check2-check1;
        var check4=emailId.indexOf("@",0);
        if(! ( check1 >= 1 && check2 > 1 && check3 > 1 &&  emailId.length!=check2+1 ) )
        {
            alert("Please eneter a valid email address!")
            document.fmEmail.emailId.focus()
            return false;
        }
    }


}


function checkEmailItem(){
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789._@";
    emailId = trim(document.emailThisItem.emailId.value);
    userEmail = trim(document.emailThisItem.userEmail.value) ;

    if(userEmail == ''){
        alert("Please enter your email address in the From: field to forward this product to a friend.")
        document.emailThisItem.userEmail.focus();
        return false;
    }
    else{
        var check1=userEmail.lastIndexOf("@");
        var check2=userEmail.indexOf(".",check1);
        var check3=check2-check1;
        var check4=userEmail.indexOf("@",0);
        if(! ( check1 >= 1 && check2 > 1 && check3 > 1 &&  userEmail.length!=check2+1 ) )
        {
            alert("Please eneter a valid email address!")
            document.emailThisItem.userEmail.focus();
            return false;
        }
    }
 
 
    if(emailId == ''){
        alert("Please enter email address!")
        document.emailThisItem.emailId.focus();
        return false;
    }

    else
    {
        var check1=emailId.lastIndexOf("@");
        var check2=emailId.indexOf(".",check1);
        var check3=check2-check1;
        var check4=emailId.indexOf("@",0);
        if(! ( check1 >= 1 && check2 > 1 && check3 > 1 &&  emailId.length!=check2+1 ) )
        {
            alert("Please enter a valid email address!")
            document.emailThisItem.emailId.focus();
            return false;
        }
    }



}

function checkRequestEmail(){
    name = trim(document.emailThisItem.emailName.value)
    phone = trim(document.emailThisItem.phoneNumber.value)
    userEmail = trim(document.emailThisItem.userEmail.value)

    if(userEmail == ''){
        alert("Please enter your email address in the email field.")
        document.emailThisItem.userEmail.focus();
        return false;
    } else{
        var check1=userEmail.lastIndexOf("@");
        var check2=userEmail.indexOf(".",check1);
        var check3=check2-check1;
        if(! ( check1 >= 1 && check2 > 1 && check3 > 1 &&  userEmail.length!=check2+1 ) )
        {
            alert("Please eneter a valid email address!")
            document.emailThisItem.userEmail.focus();
            return false;
        }
    }


    if(name == ''){
        alert("Please enter your name!")
        document.emailThisItem.emailName.focus();
        return false;
    }

    if(phone == ''){
        alert("Please enter your phone number!")
        document.emailThisItem.phoneNumber.focus();
        return false;
    }




    return true;
}

function goToEmail(){
    document.hiddenForm.hiddenText.style.visibility = "visible"
    document.hiddenForm.hiddenText.focus();
    document.hiddenForm.hiddenText.style.visibility = "hidden"
    document.fmEmail.emailId.focus();

}

function removeCalendarPopup(){
    document.getElementById('dateDiv').style.visibility='hidden'
}


function checkContactEmailItem(){
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789._@";
    emailId = trim(document.frmContactus.emailId.value)
    if(emailId == ''){
        alert("Please enter email address!")
        document.frmContactus.emailId.focus();
        return false;
    }

    else
    {
        var check1=emailId.lastIndexOf("@");
        var check2=emailId.indexOf(".",check1);
        var check3=check2-check1;
        var check4=emailId.indexOf("@",0);
        if(! ( check1 >= 1 && check2 > 1 && check3 > 1 &&  emailId.length!=check2+1 ) )
        {
            alert("Please enter a valid email address!")
            document.frmContactus.emailId.focus();
            return false;
        }
    }
    return true;

}



function checkRequestInfo() {

    var emailName = trim(document.emailThisItem.emailName.value);
    var userMail = trim (document.emailThisItem.userEmail.value);
    var phoneNumber = trim (document.emailThisItem.phoneNumber.value);
    var requestInfo = trim (document.emailThisItem.requestInfo.value);

    if (emailName == ''){
        alert("Please enter your name!");        
        return false;
    }


    if (userMail == ''){
        alert("Please enter your email address.");
        return false;
    } else{
        var check1=userMail.lastIndexOf("@");
        var check2=userMail.indexOf(".",check1);
        var check3=check2-check1;
        var check4=userMail.indexOf("@",0);
        if(! ( check1 >= 1 && check2 > 1 && check3 > 1 &&  userMail.length!=check2+1 ) )
        {
            alert("Please eneter a valid email address!");
            
            return false;
        }
    }


    if (phoneNumber == '') {
        alert("Please enter a phone number!");
        
        return false;
    }

    if (requestInfo == '') {
        alert("Please enter a comment");
        
        return false;
    }

    return true;
}